Android 4.3:BLE:startLeScan() 的过滤行为 [英] Android 4.3: BLE: Filtering behaviour of startLeScan()

查看:33
本文介绍了Android 4.3:BLE:startLeScan() 的过滤行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个 BluetoothLE 传感器设备,为此我需要形成一对多的数据广播.根据规范,外围设备可能只有一个主设备,并且由于我正在设计的芯片和堆栈的限制,一个主设备只能有三个从设备.据我了解,Android 无论如何都无法成为 BLE 从设备,因此无法将我的设备作为主设备.

I'm working on a BluetoothLE sensor device, for which I need to form a one-to-many broadcast of data. As per the spec, peripherals may only have a single master, and due to limitations of the chip and stack I'm designing on, a master can only have three slaves. From what I understand, Android cannot become a BLE slave anyway, so having my device as a master is not an option.

BT4 规范和制造商文档都谈到了另一种操作模式,称为广播模式.在广播模式下,永远不会建立连接,应用程序数据作为广告数据包的一部分传输.这将完全符合我的需求,因为许多 Android/iOS 手机可以同时扫描每个数据包.一个广告数据包以突发方式传输多次,所以我怀疑数据的接收是最可靠的.如果一个数据包在这里和那里丢失,这是可以容忍的.

Both the BT4 spec and manufacturer documentation talk about another mode of operation, referred to as Broadcast mode. In broadcast mode, a connection is never made, and the application data is transmitted as part of the advertising packet. This will exactly fit my needs, as many Android/iOS phones can simultaneously scan down each packet. An advertising packet is transmitted multiple times in bursts, so I suspect reception of data to be mostly reliable. If a packet is lost here and there, it can be tolerated.

有趣的是,我希望这些数据包携带实时传感器数据,以 10-20Hz 的速率更新.从我在网上找到的例子来看,这种模式下的 BLE 主要用于iBeacon"类型的实现,它们广播静态数据.我找不到有关如何在 Android 堆栈中过滤广告数据包的任何信息.可能是每个蓝牙硬件地址返回一个结果,也可能是地址和数据的唯一组合.第二个选项适用于此应用程序.如果开始和停止扫描会重置过滤器,我也可以使某些东西正常工作.

Where this gets interesting, is that I want these packets to carry live sensor data, that updates at a rate of 10-20Hz. From examples I've found on the web, BLE in this mode is mostly being used for "iBeacon" type implementations, where they are broadcasting static data. I cannot find any information on how advertising packets are filtered within the Android stack. It could be that they return one result per Bluetooth hardware address, or it could be the unique combination of address and data. The second option would work for this application. If starting and stopping the scan resets the filter, I can make something work as well.

Android 文档没有提及扫描方法中设备过滤的工作原理.我已经能够在网上找到一篇试图解决同样问题的帖子,但没有得到解决:BLE:扫描期间多次发现同一外设.在 iOS 中,我的同事告诉我,有一个参数可以传递给使这成为可能的 scan 函数.

The Android documentation mentions nothing about how device filtering in the scan method works. I've been able to find one post on the net attempting to solve this same problem, which has an unresolved response: BLE: Multiple discovery of the same peripheral during scan. In iOS, my colleague informs me that there is a parameter that can be passed to the scan function that makes this possible.

我试图从 Android 源代码中的 startLeScan() 调用追溯代码,但是代码相当复杂,并且抽象的使用使得很难识别包含它的对象的实现.我得到的最远的是从 BluetoothManagerService 类方法 getBluetoothGatt() 返回的 IBluetoothGatt 对象.该对象接收开始扫描的请求.它正在围绕 line 实例化github 上当前修订版的 BluetoothManagerService.java .该对象是根据消息的结果进行转换的,所以我怀疑结果可能是特定于电话/驱动程序的.能够进一步追踪它超出了我的专业知识.

I've attempted to trace the code back from the startLeScan() call in the Android source, but the code is quite complex, and the use of abstraction has made it difficult to identify the implementation of the object that contains it. The farthest I've gotten is to an IBluetoothGatt object returned from the BluetoothManagerService class method getBluetoothGatt(). This object receives the request to start scanning. It is being instantiated around line 790 of BluetoothManagerService.java on the current revision live on github. The object is being cast from the result of a message, so I suspect maybe the result is phone/driver specific. It is beyond my expertise to be able to trace it any further.

我想解决的另一个问题是打开和关闭扫描的速度有多快.扫描是一项耗电量大的操作,但数据的广播将在相当精确的实时计时器上定期发生.因此,如果可以打开和关闭扫描,使广播和扫描同步,而扫描仪在其他 90% 以上的时间里关闭,那将是一个很好的优化.这可能需要进行实验测试.

Another question I would like to resolve is how rapidly the scanning can be switched on and off. Scanning is a power intensive operation, yet the broadcast of data will happen periodically on a fairly precise, real time timer. As a result, it would be a great optimization if the scan can be switched on and off, such that the broadcast and scan are synchronized, with the scanner shut down the other 90%+ of the time. This will likely need to be tested experimentally.

我仍在进行可行性研究,看看我们的 Android 配件是否可行.我现在的手机还不能运行 4.3 版本,所以我无法通过实验测试/破解这个.

I'm still doing feasibility research to see if this is possible for our accessory for Android. My present phone cannot yet run version 4.3, so I have no way of testing/hacking this experimentally.

推荐答案

Android 4.3 和 4.4 到目前为止,它似乎是一团糟:某些设备调用onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) 在一次扫描中为一台设备多次扫描,有些则没有.无法像在 iOS 中那样配置过滤(请参阅阿尔卡迪乌什·科尼奥尔).所以,我现在开始一个列表,因为我不能问我的用户关于他们的设备这样的问题.

With Android 4.3 and 4.4 so far, it appears to be a mess: Some devices call onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) multiple times for one device in one scan, some don't. There is no way to configure the filtering like in iOS (see answer of Arkadiusz Konior). So, I now start a list, because I can't ask my users such a question about their device.

但是,在不过滤"设备上重新启动扫描也没有问题.所以,我现在在每台设备上重新开始扫描.

However, restarting scaning is also no problem on "not filtering" devices. So, I restart scaning on every device now.

不过滤(不断调用onLeScan())

  • 使用三星 BLE sdk 的 4.2.2 三星 Galaxy S4(我拥有该设备)
  • Nexus 5 with 4.4(由 [vegarwe] 添加.设备会在扫描时连续为附近设备提供扫描记录)
  • 使用 4.3 的三星 Galaxy S3(JSS15J.I9300XXUGMK6,我正在该设备上进行测试)
  • 使用 Android SDK 的 4.3 和 4.4.2 三星 Galaxy S4(由 arnaud.b 添加,无构建提供)
  • HTC One with 4.4.2(由 arnaud.b 添加,未提供版本号)
  • Samsung Galaxy S4 with 4.2.2 using Samsung BLE sdk (I owned that device)
  • Nexus 5 with 4.4 (added by [vegarwe]. The device will give scan records continuously for nearby devices while scanning)
  • Samsung Galaxy S3 with 4.3 (JSS15J.I9300XXUGMK6, I was testing on that device)
  • Samsung Galaxy S4 with 4.3 and 4.4.2 using Android SDK (added by arnaud.b, no build provided)
  • HTC One with 4.4.2 (added by arnaud.b, no build number provided)

过滤设备(适用于标准)

  • Nexus 4 与 4.3、4.4(我拥有该设备)
  • Nexus 7 2013 4G 和 4.4.2(KOT49H,我在那个设备上测试)
  • Samsung Galaxy S4 mini 4.2.2(我在这台设备上测试过)
  • Motorola Moto X(由 user1603602 添加,未提供有关 android 版本的信息)
  • 带有 4.3 的摩托罗拉 Moto G(falcon_umts,我的测试设备)
  • 采用 Android 4.3 的 Sony Xperia Tablet Z Wifi(版本 10.4.B.0.577,型号 SGP311,我的测试设备)
  • 带有 5.0.1 和 5.1.1 (Cyanogen 12.1) 的 OnePlus One
  • Nexus 4 with 4.3, 4.4 (I own that device)
  • Nexus 7 2013 4G with 4.4.2 (KOT49H, I was testing on that device)
  • Samsung Galaxy S4 mini with 4.2.2 (I was testing on this device)
  • Motorola Moto X (added by user1603602, no information about android version provided)
  • Motorola Moto G with 4.3 (falcon_umts, My testing device)
  • Sony Xperia Tablet Z Wifi with Android 4.3 (Build 10.4.B.0.577, Model SGP311, my testing device)
  • OnePlus One with 5.0.1 and 5.1.1 (Cyanogen 12.1)

未知过滤行为(请帮助将设备关联到某个组)

  • Nexus 7 2013(报告了不同的行为,例如 此处.但我阅读了更多关于它属于第一组的报告.)
  • 其他 SAMSUNG、HTC、Motorola 等设备
  • Nexus 7 2013 (Different behavior is reported like here. But I have read more reports that it's belonging to first group.)
  • Other SAMSUNG, HTC, Motorola, ..., devices

这篇关于Android 4.3:BLE:startLeScan() 的过滤行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆