在 Android 14 中,取消第一次配对请求后重试配对时,BluetoothDevice#getName() 返回 null

问题描述 投票:0回答:1

BluetoothDevice#getName() 在 Android 14 上返回 null

尝试按照以下步骤连接时,获取BluetoothDevice#getName()的空值

  1. 扫描蓝牙设备
  2. 取消配对请求
  3. 重试配对
  4. BluetoothDevice#getName() 在重试时返回 null

注意:特定于 Android 14 的问题。在 Android 13 之前工作正常。

android bluetooth-lowenergy android-bluetooth bluetooth-gatt android-14
1个回答
0
投票

GATT 服务器将通过发送广告数据包来广播其服务。 由于设备名称在 AdvertiseData 中是可选的,因此您可能需要在代码中包含如下所示的控件。 参考[bignerdranch.com](https://bignerdranch.com/blog/bluetooth-low-energy-on-android-part-1/ 和https://bignerdranch.com/blog/bluetooth-low-energy-on-android-part-2/) 作者:安德鲁·伦斯福德

override fun onScanResult(callbackType: Int, result: ScanResult) {
            val device: BluetoothDevice = result.getDevice()
            // ...do whatever you want with this found device

}

fun getDeviceName () : String? {
        return device.name
}

if (getDeviceName() != null)
        { deviceName = getDeviceName() 
    // do whatever you want with this device name
    }
        else
        { 
    // code for the case of device.name is null
    }    
© www.soinside.com 2019 - 2024. All rights reserved.