1

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?

Community
  • 1
  • 1
agustinaliagac
  • 829
  • 10
  • 24
  • I'm sure it's probably standard code but you should add some of what you are using and maybe someone will notice something wrong. Is it working on some devices? I've never ran into this problem. – codeMagic Aug 05 '16 at 16:31
  • @codeMagic , thanks for the answer. I'll add some code as soon as possible, but it really is a very standard implentation. And it doesn't work in at least 4 devices where I could test it. – agustinaliagac Aug 05 '16 at 19:10

2 Answers2

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.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • The BLE device is indeed broadcasting a name, and there is an equivalent native iOS app for the project from which I can get the device name. – agustinaliagac Aug 05 '16 at 15:42
  • It's been a long time but I could finally check this issue. This is the correct answer. The iOS app had a different behaviour for reasons I don't know. My BLE device was not advertising it's name because it was too long. Making the name shorter allowed my app to read it with no problems. – agustinaliagac Nov 18 '16 at 19:59
  • hi ,I am facing this issue now but my BLE broadcast the name i get the name on few android device but on other device the name is still null.Is there any way to throw alert based on bluetooth version my post https://stackoverflow.com/q/46968687/4111151 – prasanthMurugan Oct 27 '17 at 06:25
1

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.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • Could you please provide a quick example of the first suggestion? I'll try to implement both options and post my results here. Thanks ! – agustinaliagac Aug 05 '16 at 19:12
  • The advertisement data is built up of a list of "length, type, value", where length and type is one byte each (length includes type and value). There are types for complete name (0x09) and for shortened name (0x08). See https://android.googlesource.com/platform/frameworks/base/+/414a486e4c721f0f8f9f86823a05422acb1c509f/core/java/android/bluetooth/BluetoothLeAdvertiseScanData.java#540 for a full parser. – Emil Aug 05 '16 at 20:21