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

后端 未结 1 610
既然无缘
既然无缘 2021-01-07 07:52

I\'m writing an Android app which receives information from a Bluetooth device. Our client has suggested that the Bluetooth device (which they produce) will change its name

1条回答
  •  一生所求
    2021-01-07 08:15

    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.

    0 讨论(0)
提交回复
热议问题