简体   繁体   中英

Service for Bluetooth LE device discovery

I'm not very experienced when it comes to implementing Android apps which make a lot of use of Service s and I couldn't find any good advice or examples to help me on SO or github (or by just googling) so I decided to ask my own question.

When my app is started, it needs to start discovering nearby Bluetooth LE beacon devices and it has to continue doing so at least until it's gone to the background. The only thing I need from these beacons is to get the basic data (like the UUID) of the one whose signal is the strongest.

What is the best approach to achieve this?

Currently I've got only got a regular Service which runs the startLeScan() method on BluetoothAdapter as soon as anything is bound to it. However, I've got more than 1 Activity in the app and I don't think binding to the Service and unbinding from it in each of them is the best thing to do. I thought about starting the Service instead of binding to it but then again stopping it could involve a lot of coding - it isn't exactly easy to determine when an Android app is put into background afaik.

Any hints and advice could be useful.

Sounds like you are using iBeacons. Check out radius networks beacon library. It has a bunch of examples and instructions for how to download. Here is the link:

http://developer.radiusnetworks.com/ibeacon/android/pro/download.html

Included in the library are methods for discovering new beacons and easily getting there info (UUID, Major, minor, etc..). They even have examples showing how to do it.

In addition to other answers, I recommend this iBeacon scanning app.

BeaconBox for Android

It is useful for you to testing your beacons on smartphone.

I hope that you reach your goal.

Right, I don't consider myself an expert so feel free to correct me whenever I'm wrong.

I'm dumb - I didn't realise that the startLeScan() and stopLeScan() methods both take a BluetoothAdapter.LeScanCallback parameter and that this callback actually contains a method named onLeScan() called asynchronously upon a BluetoothLE device being scanned.

That means that no Service is necessary at all - it's enough to actually implement the onLeScan() method and it should do. I also put all of that in a singleton class instead so it's accessible from everywhere in the app.

I'll mark my answer as the correct one for now but do answer if you've got a better solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM