简体   繁体   English

蓝牙LE设备发现服务

[英]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. 在实施大量使用Service的Android应用程序方面,我经验不足,我找不到任何好的建议或示例来帮助我在SO或github上(或通过谷歌搜索),所以我决定问我自己的问题。

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. 启动我的应用程序后,它需要开始发现附近的Bluetooth LE信标设备,并且至少要继续这样做,直到到达后台为止。 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. 这些信标唯一需要的就是获取信号最强的信标的基本数据(如UUID)。

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. 目前,我只有一个常规Service ,只要绑定了任何东西,它就会在BluetoothAdapter上运行startLeScan()方法。 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. 但是,我在应用程序中有多个Activity ,我认为绑定到Service并在其中每个绑定中解除绑定不是最好的做法。 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. 我曾考虑过启动Service而不是绑定Service ,但是再次停止服务可能涉及很多编码-确定何时将Android应用程序放入后台afaik并不容易。

Any hints and advice could be useful. 任何提示和建议都可能有用。

Sounds like you are using iBeacons. 听起来好像您正在使用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 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..). 库中包括用于发现新信标并轻松获取信息(UUID,主要,次要等)的方法。 They even have examples showing how to do it. 他们甚至有展示如何做的例子。

In addition to other answers, I recommend this iBeacon scanning app. 除了其他答案之外,我还建议使用此iBeacon扫描应用程序。

BeaconBox for Android 适用于Android的BeaconBox

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. startLeScan() -我没有意识到startLeScan()stopLeScan()方法都带有BluetoothAdapter.LeScanCallback参数,并且此回调实际上包含一个名为onLeScan()的方法,该方法在扫描BluetoothLE设备时被异步调用。

That means that no Service is necessary at all - it's enough to actually implement the onLeScan() method and it should do. 这意味着根本不需要任何Service -实际上实现onLeScan()方法就足够了,它应该这样做。 I also put all of that in a singleton class instead so it's accessible from everywhere in the app. 我也将所有这些都放在一个singleton类中,以便可以从应用程序中的任何位置访问它。

I'll mark my answer as the correct one for now but do answer if you've got a better solution. 现在,我会将答案标记为正确的答案,但是如果您有更好的解决方案,请回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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