How to prevent name caching and detect bluetooth name changes on discovery

独自空忆成欢 提交于 2019-12-01 01:47:28

I would suggest 'fetchUuidsWithSdp()'. It's significance is that, unlike the similar getUuids() method, fetchUuidsWithSdp causes the device to update cached information about the remote device. And I believe this includes the remote name as well as the SPD.

Note that both the methods I mentioned are hidden prior to 4.0.3, so your code would look l ike this:

public static void startServiceDiscovery( 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." );
            }               
        }
    }
}

You'll then need to listen for the BluetoothDevice.ACTION_NAME_CHANGED intent, and extract BluetoothDevice.EXTRA_NAME from it.

Let me know if that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!