【问题标题】:BluetoothDevice getName() method returns null in Android 12BluetoothDevice getName() 方法在 Android 12 中返回 null
【发布时间】:2021-11-09 07:37:52
【问题描述】:

我正在开发一个与 BLE 相关的应用程序。它在 android 12 下运行良好,但在 android 12 中,BluetoothDevice getName() 方法返回 null。我在清单中添加了新的用户权限,如下所示,还获得了BLUETOOTH_SCANBLUETOOTH_CONNECT 的运行时权限。并且还成功将BLE设备与应用程序连接,但没有得到名称。

    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
                     android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
                     android:maxSdkVersion="30" />

    <!-- Needed only if your app looks for Bluetooth devices.
         If your app doesn't use Bluetooth scan results to derive physical
         location information, you can strongly assert that your app
         doesn't derive physical location. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app makes the device discoverable to Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

    <!-- Needed only if your app communicates with already-paired Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <!-- Needed only if your app uses Bluetooth scan results to derive physical location. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>

通过以下方法获取BLE设备名称为null:

    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
        Log.e("prepareCallSMSData", "onCharacteristicChanged BluetoothGatt " + gatt.getDevice().getName());
        Log.e("prepareCallSMSData", "device to app : " + MokoUtils.bytesToHexString(characteristic.getValue()));
        mMokoResponseCallback.onCharacteristicChanged(gatt.getDevice().getName() == null ? gatt.getDevice().getAddress() : gatt.getDevice().getName(), characteristic, characteristic.getValue());
    }```

【问题讨论】:

    标签: android android-studio bluetooth bluetooth-lowenergy android-12


    【解决方案1】:

    看起来您在权限方面所做的一切都是正确的,但我建议您再看看changes in Android 12,以防万一。还请查看下面的答案,因为它有一些解决此问题的建议:-

    android BluetoothDevice.getName() return null

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      相关资源
      最近更新 更多