BluetoothDevice always returns null on getName()

痴心易碎 提交于 2019-12-31 03:56:25

问题


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.



来源:https://stackoverflow.com/questions/38792785/bluetoothdevice-always-returns-null-on-getname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!