更新:添加了包含蓝牙权限逻辑的主活动代码
我尝试利用 Android 的CompanionDeviceManager API在运行 Android 13 的 Pixel 5 上查找附近的蓝牙(非 LE)设备,但它似乎只能找到附近的 WiFi 网络。我怀疑它deviceFilter无法正常工作。
最初,我的配置代码BluetoothDeviceFilter如下所示:
private val deviceFilter: BluetoothDeviceFilter = BluetoothDeviceFilter.Builder()
// Match only Bluetooth devices whose name matches the pattern
.setNamePattern(Pattern.compile("(?i)\\b(Certain Device Name)\\b"))
.build()
private val pairingRequest: AssociationRequest = AssociationRequest.Builder()
// Find only devices that match our request filter
.addDeviceFilter(deviceFilter)
// Don't stop scanning as soon as one device matching the filter is found.
.setSingleDevice(false)
.build()
Run Code Online (Sandbox Code Playgroud)
然而,使用此代码,系统生成的“配套设备配对”屏幕中不会出现任何设备。旋转器旋转直至超时
考虑到我的正则表达式可能无意中过于严格,我将过滤器更改为使用允许一切的正则表达式,如下所示:
.setNamePattern(Pattern.compile(".*"))
但即使这个过滤器也无法允许任何附近的蓝牙设备出现在配对屏幕中。 …
android bluetooth bluetooth-lowenergy android-bluetooth android-companion-device