BluetoothDevice always returns null on getName()

2019-03-03 06:46发布

问题:

I'm facing this issue consistently in Android 4.4, 5 and 6.

I'm performing a BLE Bluetooth devices scan and after it I can access their address (getAddress()), and other data. But if I call device.getName() it returns null EVERY TIME.

This documentation is not helpful, it only says that returns null if "there was a problem" : https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()

Other posts that I visited unsuccessfully :

https://code.google.com/p/android/issues/detail?id=34411

android BluetoothDevice.getName() return null

ble device bluetoothdevice.getname() returns null

PS: I'm using non-deprecated methods in Android API level 21+, so that's not related to the issue.

Any idea about what could be going wrong here?

回答1:

It could be that your BLE device isn't broadcasting a name. The broadcast payload is limited to 31 bytes and some manufacturers omit a name to save room.



回答2:

I'm not using getName() anymore since it seems undefined when it returns a real name (and if so how old is this cached name?) and when it is null. Just parse the scanRecord in the advertisement data and get the name from there OR read the name characteristic in the GATT DB if you are connected.