Android bluetooth sends data swapped when writing after long pause - android

My app is sending some data via bluetooth socket using Serial Port Profile, and the other device is Microchip's BM78 module.
Everything is working fine except one very particular case when testing with Huawei NEM-L21 phone. The data delivered correctly right after connection is made, or if data frames are sent frequently. But if there's a pause of 5-10 seconds between transfers, then the next transfer will get the beginning of the data blob altered. The first two blocks of approximately 650 bytes each is swapped, like if it was buffered before sending, and the second buffer gets transferred before the first one. The rest of the blob is ok.
I have no explanation for that. I've excluded BM78 issues, it's working totally ok with windows (virtual com port for bluetooth), and with a bunch of other android devices. It's only this particular phone that shows this issue.
So, is that just a bug in the phone's firmware, or there's something about bluetooth that's missed my attention? Like maybe power management options I can tweak in the phone? Anything related to Bluetooth in general?

Related

Google Nearby Connections 2.0 capabilities

I am evaluating Google Nearby connections2.0 more specifically evaluating the synergy effect of it. For this I am evaluating it against Wifi, Bluetooth and BLE in totally offline scenario, without any router.
Scenario
One device is advertising, all others (8 devices in total) are discovering. On successful connection, I am sending simple Files of 20B, 200B and 33KB sizes for 30 secs straight to each connected device.
I am using android Samsung S6 SM-G920F devices with android version: 6.0.1 and playservices version 12.8.74
I have following issues/questions
Q1: First of all at max 3 to 4 devices could be connected simulatenously more than this results into disconnect event of other devices. Even if only 3 devices are connected, and I am continuously sending message for 30seconds, one of them disconnected ? In simpler words, cannot sustain connectivity with any device for more than 45 secs. usually disconnection occur between 25 - 45 secs
Q2: I cannot send message/file continuously for 30 seconds like we can do with the Wifi like this
While(30sec){
bluetoothSocket.outputStream.write(bytes)
}
Because if I try to do this then I got the exception of too much work.I have to wait for the the callback in onTransferPayLoadUpdate()
Q3: If I try to send the file of 1MB or more to other peers, peer received the file successfully in onPayloadReceived callback but server/sender receive the successful status after too much delay. In my case it's between 1 mins to 5 mins after client callback. And I cannot send new file until I got the success callback on server. If I try to send it before getting the callback, nothing happens. Literally nothing. So In essence I can only send file of 1MB once then I have to resent both the devices to send another file.
This should be broken up into 3 separate questions. It helps future developers search easier. So if you get the time to do that, let me know and I'll split up my answer as well. But anyway, let's get into it!
A1: Nearby Connections has 3 separate strategies. The more limited the strategy, the more types of mediums we can use. So with that in mind, and with no router involved, P2P_CLUSTER will only use Bluetooth. It's the most general strategy, so it has the fewest mediums available.
All Android devices use mobile Bluetooth chips, which are unfortunately weak (but small and power sensitive), and that causes them to have a theoretical 7 device limit but a practical 3~4 device limit. To make things worst, that limit is eaten up by smart watches and paired headphones as well. That's why you're running into problems.
P2P_STAR and P2P_POINT_TO_POINT are both much more limited, because you can't connect in any direction. You need to choose who the host is beforehand and have everyone scan for and connect to that host. But you get the added benefit of WiFi hotspots, which have higher bandwidth and a larger number of simultaneous devices supported. I've seen 7 devices happily connected to a Lollipop device.
If you want to go beyond that, into the 10s and 100s, and a router isn't available, you'll have to build a mesh network. I can link you to examples of how to do that if you're interested. We don't offer support for that within Connections, but others have built meshes on top of us so we can point you in the right direction.
A2: Can you include a stack trace of the error you're seeing? Payload.Type.STREAM was built for continuously sending data. The other payload types should also work too, baring some rare but potential issues like BYTE payloads filling up the phones RAM.
A3: Both devices need to wait for onPayloadTransferUpdate(SUCCESS). onPayloadReceived is only a header, and means that there's an incoming file or stream but that the data hasn't been received yet. For byte payloads, we actually send the full byte payload inside the header so that's the only time data is immediately available.

BLE loss of Data packets in smartphone

Am working on a BLuetooth Low energy module embedded in a custom board . whenever i press an interrupt (push button) the ble module advertises some number once and goes to sleep ,again when interupt given it advertises the next number(incremented value) once and goes to sleep .These values are scanned in a custom android app running in nexus 5.Though the phone is scanning most of the packets ,sometimes it fails to capture some datapackets.the custom board advertises once and goes to sleep ,the phone must be able to capture this advertisement packets,but some times it miss the data packets. So my question is Is it normal that the BLE compatible phones cannot capture all datapackets from a pheriperal device i.e there may be loss of some data packets.Or is it the problem with smart phone nexus 5 ble hardware.
BLE is transmitting with low power on in a crowded spectrum. Some packet loss is to be expected (though I agree it is more than I'd like).
A higher level protocol will detect packet loss and request retransmission. In the case of BLE, instead of using advertisments for data you can actually connect to the device and then use notifications. These are far more reliable and shouldn't be dropped. The downside is most devices only support connecting to a handful of peripherals, usually 7. Listening for advertisments doesn't require connection and therefore there's no fixed limit to the number of advertisers you can listen to.
The packet loss also depends on what is listening. A nRF51822 trasmitting to my Nexus 4 with Android 5.0 at zero range gets >50% PLOS. When transmitting to another nRF51822 it gets around 20% PLOS. I guess the Nexus 4 tries to save power so maybe it doesn't listen continously.
I had a similar problem, that only some data packets were arriving. It seems that enabling the SCAN_MODE_LOW_LATENCY mode with ble scanner helps - your device will keep listening continously, so no data will be missed (unless there is clutter, bad range etc. like Timmmm suggested).
https://developer.android.com/reference/android/bluetooth/le/ScanSettings
hope this helps

Android USB bulkTransfer missing data

I'm using the USB host abilities of Android to communicate with my hardware. My communication is done through a CP210X usb to serial chip, and I'm using the driver provided here. My device is a Motorola Xoom running Android 4.0.3.
Everything for the most part works fine. I'm able to send any amount of arbitrary data and the device on the other side of the CP210X chip gets it just fine. We use a request/response protocol, and rarely send any more than about 200 bytes in a second. The other (device) side is able to send me data, and everything works until the amount of data it sends me is longer than 20 bytes. If it sends me 20 or fewer bytes, I receive it normally. However, as soon as the amount of data that it sends me is 21 bytes or greater, the packet never gets to me. UsbDeviceConnection's bulkTransfer never reports anything different happening, as it's return value is always -1 on timeouts anyways. My bulkTransfer timeout is 100ms, which should be plenty of time to transfer such a small amount of data.
In an effort to see if it was an issue with my CP210X chip, I swapped it with an FTDI serial to usb, and experienced the same issues. That really makes me worry that the issue I'm facing might be in the usb drivers on my device. However, I also noticed the behavior when trying it out on a Samsung Galaxy S3, so that's got me very confused.
Is there anything I'm missing or doing wrong?

Android Bluetooth file transfer

I am trying to implement file transfer through bluetooth and i am using the Bluetooth -chat example that comes with the android sdk.
When i try connecting two android phones, they seem to always say, the phones are paired but not connected. This is tedious as i have tried to connect with 3 different pairs of phones and all of them say the same thing.
Is it required for the devices to be rooted or something?? How can i connect two android phones through bluetooth.
(This isn't strictly an answer, but I am yet unable to post comments.)
I've personally had good success with the Bluetooth Chat example code, and I've used it to create an application that interfaces with a Bluetooth/RS232 dongle to obtain lots of measurement data from a remote hardware device. I do confess, however, that I've only ever used the host Bluetooth code to initiate a connection to the Bluetooth SPP dongle; I've not attempted to connect to the slave code on another phone.
For reference, the two devices I've used my app on with success are my HTC Desire (2.2) and my Archos 101 (2.2). They are not rooted.
One thing that I think is worth me mentioning is that the Bluetooth Chat example appears to have a bug in the code where it accepts an array of received bytes out of the socket and presents them into the ListActivity. If lots of characters are sent from the remote device at anything much faster than typing speed, what is shown in the ListActivity on the phone shows the occasional character being corrupted. It's because (I think) the ArrayAdapter is still consuming bytes from that array to print them on the screen when the connected thread starts to fill that same array with even newer bytes. So, I had to fix that before I could use the Bluetooth Chat example code to reliably receive relatively large volumes of data from the external device.
On either the HTC Desire or Archos 101 I don't seem to have any problems with connection. I can happily have the Bluetooth connection exist reliably even over poor signal conditions (e.g. the remote device is upstairs in the house while I'm downstairs with the Android device) while my app happily polls measurement data reliably at a rate of about 250bytes a second.
Generate UUID of your phone by using adb shell, sdptool browse command

Problem with Android Multicast Sockets

I'm running some code on the nexus one - and I am having a very strange issue. The "server" sends UDP packets out on the multicast socket that the phone is joined to. The phone receives and sends stuff to the multicast group fine, but it appears that the phone chokes when it receives a large number of packets at one time.
For example, the server will occasionally send out 80 or so 512 byte packets in one go around. The phone starts receiving the packets, but then at the 31st packet (~16KB of data) the receive hangs (indicating that there is nothing on the socket anymore.) I've done wireshark testing and stuff so I know for sure that the packets are not being lost. Even if some are being lost, it is very strange that EVERY test produces the same result --- a large amount of data gets jammed onto the socket at one time, but the socket only allows me to socket.receive() for 16KB worth of data.
I have tried socket.setReceiveBufferSize() with a variety of values (up to 1MB) -- but that doesn't seem to work. Interestingly, a call to getReceiveBufferSize() tells me that the socket should hold roughly 100KB worth of data.
If I tell the server to slow down its send method (which I cannot deploy, I can only do that for testing purposes) - everything works fine, presumably because the packets aren't jammed onto the phone's socket all at one time (ie. I tell the server to wait 1/2 a second between packets.)
Has anyone else experienced this - if so - how did you get around it? I cannot switch the application to TCP.
Thanks!
Dan
PS. The Java code works fine if I run it on a regular machine...
Nevermind - I fixed it by moving onto a closed network where there is 0 possibility for loss and weird things -- not sure what was up with the network I was on (which was supposed to be goo) - but at least it works!
Dan

Categories

Resources