且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式清除Android的蓝牙名称缓存?

更新时间:2021-09-24 22:25:47

是的,fetchUuidsWithSdp()是一个好主意,因为它不像getUuids()它迫使设备尝试连接到目标设备并更新它的它的相关信息

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.

有关fetchUuidsWithSdp的官方支持刚添加的4.0.3,但它是可用的使用反射了。

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

人们通常会再注册android.bluetooth.device.action.UUID,但你可能要注册的名称变化行动吧。

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

需要注意的是,如果你决定报名参加UUID的行动,这是前15 API为android.bleutooth.device.action.UUID(电子和u蓝牙互换)拼写错误。

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).