本文介绍了Android-成功连接后,无法在onScanResult回调中找到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用手机扫描BLE设备.每当我收到 onScanResult 的回调时,我都会检查外围设备是否为具有特定minorId的iBeacon,如果是,则进行设备连接.问题是,成功连接后, onScanResult 会返回我周围的其他设备,但这些设备都不是我刚刚连接的设备.在 onScanResult 中找到连接的设备对我来说至关重要.这是正常行为吗?是什么原因造成的?只有在我连接到设备时才会发生这种情况.

I'm scanning for BLE devices with my mobile. Whenever I get a callback of onScanResult I check if peripheral is iBeacon with specific minorId, if it is, I do device connection. Problem is that after successful connection onScanResult returns different devices around me, but none of them are the ones I just connected to. It's crucial for me to find connected devices in onScanResult. Is this normal behaviour? What could cause this? This happens when I connect to device - and only then.

推荐答案

根据蓝牙规范,这是默认行为.对于低功耗蓝牙设备,有6种预定义状态:-

This is the default behaviour according to the Bluetooth specification. For Bluetooth Low Energy devices, there are 6 predefined states:-

  • 待机状态
  • 广告状态
  • 扫描状态
  • 发起国
  • 连接状态
  • 同步状态

在规范中声明:链路层状态机一次只允许一个状态处于活动状态".在下面的状态机中将对此进行说明:-

In the spec, it is stated: "The Link Layer state machine allows only one state to be active at a time". This is demonstarated in the state machine below:-

这就是为什么一旦您连接到广告设备,它就会将状态切换为连接"并保持该状态.如果您有权访问设备的代码,则可以强制其在连接后进行播发,但并非所有设备都支持此功能.

This is why once you are connected to the advertising device, it switches state to "Connection" and remains in that state. If you had access to the device's code you could force it to advertise after connection, but not all devices support this feature.

有关更多信息,请检查蓝牙规范 v5.1,第6卷,B部分,第1节:链接层状态.

For more information, check the Bluetooth Specification v5.1, Vol 6, Part B, Section 1: Link Layer States.

这篇关于Android-成功连接后,无法在onScanResult回调中找到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:31