How to programmatically clear the Bluetooth name cache in Android?

I noticed that when a paired Bluetooth device has a name change, my Android device does not always register this name change. It continues to display the old device name ... This is not a problem for unpaired devices, so I assume that Android caches paired device names somewhere.

Looking around, I found that if I disconnect the device and manually clear the cache stored in the Android application “Bluetooth Share”, this problem disappears. Of course, the problem will probably come back after I reconnect the device to my Android.

TL DR How to make Android always show the last name of the Bluetooth device?

I heard something about the fetchUuidsWithSdp method, but I'm not sure how to use it.

+6
source share
2 answers

Yes, fetchUuidsWithSdp () is a good idea because, unlike getUuids (), it makes the device try to connect to the target device and update its information about it.

The official support for fetchUuidsWithSdp was just added in 4.0.3, but it was available before that using reflection.

public static void startFetch( BluetoothDevice device ) { // Need to use reflection prior to API 15 Class cl = null; try { cl = Class.forName("android.bluetooth.BluetoothDevice"); } catch( ClassNotFoundException exc ) { Log.e(CTAG, "android.bluetooth.BluetoothDevice not found." ); } if (null != cl) { Class[] param = {}; Method method = null; try { method = cl.getMethod("fetchUuidsWithSdp", param); } catch( NoSuchMethodException exc ) { Log.e(CTAG, "fetchUuidsWithSdp not found." ); } if (null != method) { Object[] args = {}; try { method.invoke(device, args); } catch (Exception exc) { Log.e(CTAG, "Failed to invoke fetchUuidsWithSdp method." ); } } } } 

Usually, the android.bluetooth.device.action.UUID file is usually registered, but you may want to register instead to change the name.

Please note that if you decide to register for the UUID action, it was written with an error before API 15 as "android.bleutooth.device.action.UUID" (e and u in bluetooth are replaced).

+6
source

to remember the last device that I connect, I always save the MAC address in a file

0
source

Source: https://habr.com/ru/post/916876/


All Articles