I finally got myself a device that supports BLE advertising. First issue on getting one was to figure out what devices are actually supporting the feature. Even though there are claims that some Android devices can be used as BLE peripheral while having only Bluetooth 4.0 chip on them, I would strongly suggest getting a device that supports Bluetooth 4.1. Currently only confirmed 4.0 device I know would be Samsung S5, though I have not used it myself. Nexus 5 also was claimed to work with earlier preview versions of Lollipop, but I also found discussions suggesting that while it was used to advertise, it could not be used for scanning same time, supposedly that was the main reason it’s not supporting the feature with official Lollipop versions.
So from API perspective the requirement is Lollipop support, and from hardware it would be Bluetooth 4.1 support, and with quick search my list of supporting device got to look like this:
- Nexus 6,
- Nexus 9,
- LG G4
- LG G Flex 2
- HTC One M9
- Samsung Galaxy S6
- Sony Xperia™ M4 Aqua
The first device I got was Nexus 6, for historical reasons I have several Nexus 4 & 5 devices, so it makes nice continuity to have one 6 as well. I should be getting Sony XPeria M4 Aqua also soon, and as the device is loads cheaper than Nexus, as well as even its having smaller battery, it does have double standby time ( according to ubergizmo:). It also has less RAM, and slower CPU, thus it likely make nice device to do tests and compare the results with the Nexus.
And to get started I had to get some codes. I was surprised that I couldn’t really find any nice full code samples that would do scanning (and parsing the scan info), connecting & character/descriptor read and write. Thus I made my own full example to create & tests helper classes I can re-use in later tasks. The resulting example is stored at DrJukka/BLETestStuff/MyBLETest . The implementation is still work on progress, and current limitations include for example, that only string-type values are supported, and that only non-reliable write is implemented.
I alsready used the classes developed with the example in other project DrJukka/BLETestStuff/BleGgOne. This project was used to test that we can run the app in background and still do advertisement & scanning. I also made second version of it, to test that we can have more than one background app doing the advertising.
The result was that Yes, we can do advertising in the background, and Yes we can do scanning in the background as well. Then as I only have one device to test, I did not get to see whether I could do both same time. I’ll need to wait my XPeria device to finalize the testing.
And for the question that “can we have multiple services advertising in background” the answer is also Yes.
As with the BLE, each time you start advertising, the device is advertised with new virtual-mac-address. So if you stop and start a service, it will get new address which the client device must use (using the old one will cause 0x85 error to appear)
When using two services same time, these is something similar happening, basically a new device advertisement with new virtual-mac-address will be visible to the clients. But also the first services virtual-mac-address is still visible, and in this case both of them are valid and can be used for connecting to the device.
But when you connect either one of them, you will be getting the all services from the device. So it does not matter which one you would be using for connecting, you would still get service advertised by both of them.