Android - 我可以使用什么 API 来查看设备是否支持 BLE 中央模式?

Android - What API can I use to see if a device supports BLE central mode?

https://developer.android.com/guide/topics/connectivity/bluetooth-le#roles

Central vs. peripheral. This applies to the BLE connection itself. The device in the central role scans, looking for advertisement, and the device in the peripheral role makes the advertisement.

要检查设备是否支持 "peripheral"/广告模式,我似乎可以使用 getBluetoothLeAdvertiser()

getBluetoothLeAdvertiser

added in API level 21 public

BluetoothLeAdvertiser getBluetoothLeAdvertiser () Returns a BluetoothLeAdvertiser object for Bluetooth LE Advertising operations. Will return null if Bluetooth is turned off or if Bluetooth LE Advertising is not supported on this device.

Use isMultipleAdvertisementSupported() to check whether LE Advertising is supported on this device before calling this method.

现在我听到一些谣言说有些设备实际上可以给你一个 BluetoothLeAdvertiser,但是 return 在 isMultipleAdvertisementSupported() 上是错误的,这本身是另一个问题,但在事情的中心方面,文档什么都没说

我错过了什么吗? https://developer.android.com/reference/android/bluetooth/BluetoothAdapter 没有提到中央模式。我在这里缺少一些基本的东西吗?谢谢你的帮助。我听说蓝牙在 Android 上很麻烦,这是我探索这些 API 的第一天。

简答

正如 DigitalNinja 所指出的,Android phone 将始终默认支持中央角色功能,因此如果您的 phone 支持 BLE,那么它肯定可以在中央角色中运行

长答案

你是对的,API 可能不是直截了当的,但这取决于低功耗蓝牙 (BLE) API 是如何添加到 Android 甚至更低的BLE 技术本身的历史。

当 BLE 首次推出时,它的目标是仅用于传感器(例如温度计、心率、接近度等)以及与这些传感器通信的设备。在这种情况下,传感器是 外围设备 ,与这些传感器通信的设备是 中央设备 。外围设备是真正的低能耗 (LE) 设备,因为它们只会偶尔发布广告和发送数据。另一方面,Central 的能效不是很高,因为它们必须不断扫描设备、连接到这些设备,并继续负责维护和监控该连接,这意味着与此相比,无线电的开启时间要长得多到外设。

当低功耗蓝牙 (BLE) API 添加到 Android 时,它仅支持中央角色。换句话说,您可以编写一个 Android 应用程序来扫描并连接到外围设备(传感器),但是 Android 设备本身不能充当外围设备(因为假设您不需要Android 设备充当传感器)。这是在 Android 4.3 (API 18).

中完成的

随着人们开始更频繁地使用 BLE 和技术的成熟,它开始用于各种不同的应用程序(例如虚拟串行端口、数据传输、信标等)。此外,独立的中央设备开始出现在市场上,需要将它们与 phone 一起使用,或者至少在开发阶段针对 phones/tablets 进行测试。因此,中央和外围之间的区别开始变得越来越模糊,并且需要 Android 开始支持外围角色。这是 BLE API 更新以引入外围角色功能的时间,这发生在 Android 5.0 (API 21) 中。

所以回答你的问题,如果 Android 设备支持 BLE,那么可以肯定地说它将默认支持中心角色,因为这是 [=46] 的 BLE 功能的基础=].但是后来引入了API来支持外设角色,这也是为什么不是所有支持BLE的Android设备都会支持外设角色的原因。

最后,请注意 isMultipleAdvertisementSupported 是一项不同的功能,它指示您的设备是否支持同时发送多个广告。一些设备支持同时发送不同的广告,而另一些则不支持。但是,这并不意味着他们完全不支持广告。支持isMultipleAdvertisementSupported的设备一定会支持BluetoothLeAdvertiser,反之则不一定。

总而言之,检查您的设备是否支持中央 and/or 外围角色的最安全方法是通过 Android 版本和使用的 API 级别,如两个链接所示多于。在Android5(API21)以后,你可以编写支持中心和外围角色的应用程序,而在此之前你只能编写中心角色的应用程序。

希望对您有所帮助。