简体   繁体   English

如何防止Android蓝牙缓存设备名称? (服务器端)

[英]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(). 当我调用accept()时,将返回一个蓝牙套接字,该蓝牙套接字用于通过调用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". 假设有一个客户端连接到服务器,并且该客户端的蓝牙设备名称为“ A”。 It connects to the server, and the server will output that device "A" has been connected. 它连接到服务器,服务器将输出设备“ A”已连接。 However, I decide to change the name of the client's Bluetooth device name to "B". 但是,我决定将客户端的蓝牙设备名称更改为“ B”。 When I connect to the server again, the server still thinks that the client's Bluetooth device name is: "A". 当我再次连接到服务器时,服务器仍然认为客户端的蓝牙设备名称为:“ 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. 我已经看到有些人使用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: 当然,在onCreate中,我已按以下方式注册了接收者:

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). 在客户端(发现)端,即使没有fetchUuidsWithSdp()(客户端不缓存服务器的蓝牙设备名称),一切也可以正常工作。 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 代替ACTION_UUID注册

BluetoothDevice.ACTION_NAME_CHANGED BluetoothDevice.ACTION_NAME_CHANGED

This action always contains the extra fields EXTRA_DEVICE and EXTRA_NAME. 此操作始终包含额外的字段EXTRA_DEVICE和EXTRA_NAME。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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