简体   繁体   English

Android蓝牙适配器未发出ACTION_DISCOVERY_FINISHED

[英]Android bluetoothadapter not emitting ACTION_DISCOVERY_FINISHED

I have a class with a broadcast receiver setup. 我有一个带有广播接收器设置的课程。 The receiver listens for the following actions: 接收器侦听以下操作:

ACTION_FOUND
ACTION_DISCOVERY_STARTED
ACTION_DISCOVERY_FINISHED

The found and started actions get emitted as expected, however, the documentation for the BluetoothAdapter specifies that a discovery scan should take ~12 seconds and is then followed by a ACTION_DISCOVERY_FINISHED . 找到并开始的动作将按预期发出,但是,BluetoothAdapter的文档指定发现扫描应花费约12秒,然后执行ACTION_DISCOVERY_FINISHED This does not seem to be the case, since I can only manually trigger an emit of the finished action by making a call to cancelDiscovery() . 似乎并非如此,因为我只能通过调用cancelDiscovery()来手动触发完成动作的发出。 If I leave it running long enough, sometimes the event is emitted (but this can take upwards of 1-2 minutes). 如果我让它运行足够长的时间,有时会发出该事件(但这可能需要1-2分钟的时间)。 Sometimes, that doesn't even happen. 有时,甚至没有发生。 If I proceed to force the scan to end, by disabling Bluetooth, the action is emitted as expected. 如果我继续通过禁用蓝牙来强制扫描结束,则会按预期发出操作。

Is the scan supposed to take this long, and am I supposed to manually control the scanning process, ie ending it myself after a 12 second countdown? 扫描应该花费这么长时间吗,我是否应该手动控制扫描过程,即在倒数12秒后自己结束扫描? Or am I missing something in the docs. 或者我在文档中缺少什么。 The broadcastreceiver is configured correctly, as evidenced by the arrival of all the actions. 所有动作的到来证明了广播接收器的配置正确。

After upgrading the Nexus 5 to android version 6.0.1 as well, I seem to be getting a little more consistent results with the ACTION_DISCOVERY_FINISHED broadcast. 将Nexus 5也升级到Android 6.0.1版后,我似乎在ACTION_DISCOVERY_FINISHED广播中获得了更加一致的结果。 It also came to light that my other android phone (Xperia Z3) wasn't finding any devices as per the changes in https://developer.android.com/about/versions/marshmallow/android-6.0-changes.htm , which means that it now also requires the ACCESS_COARSE_LOCATION permission, which you need to ask for during runtime. 还发现我的另一部Android手机(Xperia Z3)根据https://developer.android.com/about/versions/marshmallow/android-6.0-changes.htm中的更改未找到任何设备。意味着它现在还需要ACCESS_COARSE_LOCATION权限,您需要在运行时请求该权限。 If this is not done, the broadcast will not be received by your application. 如果不这样做,您的应用程序将不会接收广播。 I hope someone else finds this useful. 我希望其他人觉得这很有用。

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

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