【发布时间】:2021-11-09 07:37:52
【问题描述】:
我正在开发一个与 BLE 相关的应用程序。它在 android 12 下运行良好,但在 android 12 中,BluetoothDevice getName() 方法返回 null。我在清单中添加了新的用户权限,如下所示,还获得了BLUETOOTH_SCAN 和BLUETOOTH_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