Android-BLE-Library icon indicating copy to clipboard operation
Android-BLE-Library copied to clipboard

Getting the device name after connecting the device on Android9.0 and above is null

Open Fly-Felix opened this issue 3 years ago • 6 comments

When I run on Android 9.0 and above, the scanned device can get the name, but once I click on the device to select the connection, the device name immediately becomes null after the connection is successful, and I have not found similar problems after checking a lot of information. come here to try your luck

Fly-Felix avatar Apr 29 '22 07:04 Fly-Felix

Everything works fine when scanning the device, getName() device name returns null after connecting the device

Fly-Felix avatar Apr 29 '22 08:04 Fly-Felix

The BluetoothDevice.getName() is obtained from Device Name characteristic after connection, and cached by the system. I actually recommend getting the name from the advertising data instead, and keeping it together with the BluetoothDevice object. When you, for example, flash a new fw on a DK, with a different application, which is advertising a different name, the getName() still returns the old, cached one.

philips77 avatar Apr 29 '22 08:04 philips77

I seem to know, I just checked the source code of ExtendedBluetoothDevice in Android-nRF-Toolbox, and found that the constructor ExtendedBluetoothDevice(@NonNull final ScanResult scanResult) was called during scanning and assigned to the name, and there was no name change in it. The subsequent list update, This can achieve the effect of a cache name, I understand it right? , my list adapter in the update directly used the bluetooth device update list for the convenience of use, and reassigned the name, it should be this problem, I will try in this direction, thank you.

Fly-Felix avatar Apr 29 '22 09:04 Fly-Felix

Good morning, I tried it yesterday, and the name is temporarily saved successfully, but when I reopen the page, I get the list of connected devices through List<BluetoothDevice> devices = manager.getConnectedDevices(BluetoothProfile.GATT); It returns a BluetoothDevice, so the name still cannot be Get , the connected bluetooth device will no longer broadcast, and of course will not enter onScanResult, so the ScanResult and name cannot be obtained.

Fly-Felix avatar Apr 30 '22 01:04 Fly-Felix

I have experimented with the latest results. After I went to the system to connect the BLE device once, BluetoothDevice.getName() all returned to normal.

Fly-Felix avatar Apr 30 '22 02:04 Fly-Felix

When a third-party APP name is connected to a Bluetooth device, the system does not seem to cache Android 9.0 and above

Fly-Felix avatar Apr 30 '22 02:04 Fly-Felix

Sorry for the ling response time. Please, check the name returned by Device Name characteristic in nRF Connect for Mobile. This is the name returned from BluetootDevice.getName(). I'm closing the issue due to long time of inactivity.

philips77 avatar Sep 30 '22 14:09 philips77