简体   繁体   中英

How to prevent Android bluetooth caching device names? (Server side)

I have a Bluetooth server socket that accepts incoming connections. When I call accept(), a Bluetooth-Socket is returned which I use to get information about the device that is connecting with the server by calling getRemoteDevice(). This all works OK, however, when I get the name of the remote device, it displays a "cached" name. Let's say have a client connecting to the server and the client has Bluetooth device name: "A". It connects to the server, and the server will output that device "A" has been connected. However, I decide to change the name of the client's Bluetooth device name to "B". When I connect to the server again, the server still thinks that the client's Bluetooth device name is: "A".

Is there anyway for me to prevent this caching? Or, at least, is there a way for me to get the latest, updated device name?

I've seen that some people use fetchUuidsWithSdp. So, I've tried using it as follows:

private void manageConnectedSocket(BluetoothSocket socket) {
    // Manage the socket returned by BluetoothSeverSocket.accept()
    final BluetoothDevice device = socket.getRemoteDevice();
    // System.out.println("\nAdded: " + device.getName() + " " + device.getAddress());
    device.fetchUuidsWithSdp();
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_UUID.equals(action)) {
            // Get the device
            final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println("\nAdded: " + device.getName() + " " + device.getAddress());
        }
    }
};

And of course, in onCreate, I have registered the receiver as follows:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_UUID);
registerReceiver(mReceiver, filter);

This does not work, and still the "cached" Bluetooth device name is outputted. On the client (discovery) side, everything works fine even without the fetchUuidsWithSdp() (the client does not cache the server's Bluetooth device name). Anyone have any ideas on how to get the most up-to-date name from the client Bluetooth device? My project is at a standstill because of this ONE issue.

instead of ACTION_UUID register for

BluetoothDevice.ACTION_NAME_CHANGED

This action always contains the extra fields EXTRA_DEVICE and EXTRA_NAME.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM