Android 低功耗蓝牙特性通知计数限制:这是否因设备而异? [英] Android Bluetooth Low Energy Characteristic notification count limit: does this vary by device?

查看:28
本文介绍了Android 低功耗蓝牙特性通知计数限制:这是否因设备而异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个面向 5.0+ 的 Android 应用程序,它使用 BLE 连接到多个外围设备.这些外设中的每一个都有许多(~10)个特性.理想情况下,我想订阅这些特征中每一个的更改通知.

I am building an Android app targeting 5.0+ that uses BLE to connect to multiple peripherals. Each of these peripherals has many (~10) characteristics. Ideally, I would like to subscribe to notifications of changes for every single one of these characteristics.

但是,我从阅读中了解到,Android 的蓝牙实现对可以激活的同时通知的数量实施了硬限制.这个问题确定了限制在 Android 蓝牙中定义的位置 实施,并说明限制如何随时间变化:

However, my understanding from reading around is that Android's Bluetooth implementations enforce a hard limit on the number of simultaneous notifications that can be active. This question identifies where the limit is defined within the Android Bluetooth implementation, and also notes how the limit has changed over time:

最大并发活动通知(BTA_GATTC_NOTIF_REG_MAX):

Max concurrent active notifications (BTA_GATTC_NOTIF_REG_MAX):

  • Android 4.3 上的 4
  • Android 4.4 上的 7
  • 15(Android 5.0+)

Dave Smith 在此视频中确认了这些数字还建议:

These numbers are confirmed by Dave Smith in this video where it is also suggested that:

  • 这些限制对设备来说是全局的(即,如果某个其他应用订阅了 2 个通知,我的应用可用的数量会减少 2);
  • 这些限制不应因 Android 操作系统级别以外的任何其他因素而有所不同(即,它们应独立于制造商、实际硬件功能等)

然而,在 5.0+ 设备上的测试中,我发现我显然能够成功订阅超过 15 个通知.到目前为止,我已经观察到这一点:

In testing on 5.0+ devices, however, I have found that I am apparently able to successfully subscribe to more than 15 notifications. So far I have observed this on:

  • 运行 7.1.1 的 Pixel XL
  • 运行 6.0.1 的 Galaxy S6
  • 运行 5.X 的 Nexus 5

这些通知订阅的成功体现在两个方面:

These notification subscriptions are successful by two measures:

  1. GATT 操作状态为 GATT_SUCCESS;
  2. 该应用能够接收来自所有目标特征的特征变化通知.

这是喜忧参半的消息.一方面,更多真实"通知 ==> 更少的手动轮询 ==> 更好的用户体验.另一方面,无法创建导致真实"通知设置失败的条件意味着我无法轻松编写或测试此应用程序发布给真实用户后​​肯定(?)需要的手动回退代码.

This is mixed news. On the one hand, more "real" notifications ==> less manual polling ==> better user experience. On the other hand, being unable to create conditions that cause "real" notification setup to fail means I can't easily write or test the manual fallback code that will surely(?) be required once this app is released to real users.

  • 这种无视限制的行为是否符合预期?(我在别处找不到它的描述.)
  • 是否有任何已知的设备可以最多接收 15 条通知,我可以用它来测试不愉快的路径?

推荐答案

API 的设计非常糟糕.这个实现使用固定大小的数组而不是动态数组这一事实可能是库用 C 编写时遗留下来的.

The APIs are quite badly designed. Just the fact that this implementation uses a fixed size array rather than a dynamic array is probably a legacy from when the library was written in C.

内部 C++ 库实际上 return分配通知条目失败时的错误.不幸的是,该错误只会被记录下来,不会传播到消费应用程序的 Java 层(source).这意味着开发人员无法确定(在代码中)何时达到限制.

The internal C++ library actually returns an error when it fails to allocate a notification entry. Unfortunately that error is only logged and is not propagated to the Java layer of the consuming application (source). This means that developers have no way to determine (in code) when they hit the limit.

无论如何,BTA_GATTC_NOTIF_REG_MAX 限制是每个 BluetoothGatt 对象,因此如果连接了另一个应用程序,它不应干扰您的通知注册.您甚至可以在同一应用程序中将两个 BluetoothGatt 对象连接到同一设备,这样可以获得双倍的通知注册槽.

Anyway, the BTA_GATTC_NOTIF_REG_MAX limit is per BluetoothGatt object, so if another app is connected it should not interfere with your notification registrations. You can even have two BluetoothGatt objects in the same app connecting to the same device, and that way get double as many notification registration slots.

定义了最大插槽数这里.操作系统级别是唯一定义此值的内容是不正确的,因为制造商可以自由更改此值.例如,我知道三星在他们的一些设备中增加了最大连接数(否则硬编码为 7),所以他们也可能增加了这个值.

The max number of slots is defined here. It's incorrect that OS level is the only thing that defines this value, as manufacturers are free to change this value. For example I know that Samsung has in some of their devices increased the maximum number of connections (that is otherwise hardcoded to 7), so they might also have increased this value.

我无法解释您是如何在 Google 手机上成功订阅超过 15 条通知的.您是在一台设备上还是在多台设备上配置了超过 15 个通知?

I can't explain how you succeeded in subscribing to more than 15 notifications on the Google phones. Did you configure more than 15 notifications on a single device or over many devices?

这篇关于Android 低功耗蓝牙特性通知计数限制:这是否因设备而异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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