Reputation: 79
I am working on a BLE-related app. It is working fine below android 12 but in android 12, BluetoothDevice getName() method returns null. I have put new user permission in the manifest as below and also get runtime permission for BLUETOOTH_SCAN
and BLUETOOTH_CONNECT
. And also successfully connected the BLE device with the app, but did not get the name.
<!-- 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>
Getting BLE device name as null in the method below:
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());
}```
Upvotes: 4
Views: 1925
Reputation: 13285
It looks like you're doing everything correct in terms of permissions, but I suggest having another look at the changes in Android 12 in case something jumps at you. Also have a look at the answer below as it has a few suggestions to overcome this issue:-
android BluetoothDevice.getName() return null
Upvotes: 1