Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 doesn't always register that name change. It continues to display the old name of the device... This isn't a problem for unpaired devices, so my natural guess is that Android caches the names of paired devices somewhere.

After looking around, I found that if I unpair the device and manually clear the cache stored in my Android's "Bluetooth Share" app, this problem goes away. Of course, the problem will probably come back after I pair the device again to my Android.

TL;DR How do I force Android to always show the latest name of a Bluetooth device?

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

like image 977
user1408996 Avatar asked May 29 '12 05:05

user1408996


1 Answers

Yes, fetchUuidsWithSdp() is a good idea because, unlike getUuids() it forces the device to attempt to connect to the destination device and update it's information about it.

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." );
            }               
        }
    }
}

One would normally then register for android.bluetooth.device.action.UUID, but you might want to register for the name change action instead.

Note that, if you do decide to register for the UUID action, it was misspelled prior to API 15 as "android.bleutooth.device.action.UUID" (the e and u in bluetooth are swapped).

like image 172
Tom Avatar answered Oct 06 '22 14:10

Tom