如何prevent名称缓存和检测上发现蓝牙名称变更 [英] How to prevent name caching and detect bluetooth name changes on discovery
问题描述
我正在写一个Android应用程序从一个蓝牙设备接收信息。我们的客户建议,蓝牙设备(他们生产)将根据一定的条件下改变其名称 - 最简单的例子它的名字有时会XXX-ON,有时XXX-OFF。我的应用程序只是应该寻求这种BT发射器(我用BluetoothAdapter.startDiscovery()),并做取决于它找到的名称不同的事情。我不与蓝牙设备配对(虽然我想这是可能的,应用程序应该是最终与多台Android设备和多个BT发射机工作,所以我不知道这将是一个不错的主意)。
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 depending on certain conditions - for the simplest example its name will sometimes be "xxx-ON" and sometimes "xxx-OFF". My app is just supposed to seek this BT transmitter (I use BluetoothAdapter.startDiscovery() ) and do different things depending on the name it finds. I am NOT pairing with the Bluetooth device (though I suppose it might be possible, the app is supposed to eventually work with multiple Android devices and multiple BT transmitters so I'm not sure it would be a good idea).
我的code正常工作,检测设备BT,找到自己的名字。另外,如果设备熄灭,我可以检测下一次我求,这是不存在的。但似乎如果它是存在的,它改变了名字,我拿起旧名 - presumably被缓存的地方。即使蓝牙设备熄灭,我们注意到,下一次我发现了,我仍然看到旧名称。
My code works fine to detect BT devices and find their names. Also, if the device goes off, I can detect the next time I seek, that it is not there. But it seems that if it is there and it changes name, I pick up the old name - presumably it is cached somewhere. Even if the bluetooth device goes off, and we notice that, the next time I detect it, I still see the old name.
我发现在谷歌code这个问题:这里但它给我不清楚,甚至如何使用给出的解决方法(尝试连接)。有没有人这样做,任何运气?你可以分享code?
I found this issue in Google Code: here but it was unclear to me even how to use the workaround given ("try to connect"). Has anyone done this and had any luck? Can you share code?
有没有一种简单的方法,直接删除缓存的名字并重新搜索,所以我总能找到最新的名字呢?即使是一个非简单的方法将是很好(我写一个根深蒂固的设备)。
Is there a simple way to just delete the cached names and search again so I always find the newest names? Even a non-simple way would be good (I am writing for a rooted device).
感谢
推荐答案
我建议'fetchUuidsWithSdp()。它的意义在于,不同的是类似getUuids()方法,fetchUuidsWithSdp导致设备更新有关远程设备的缓存信息。我相信这包括远程名称以及社民党。
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.
请注意,这两个我提到的方法是隐藏之前4.0.3,所以你的code会看升艾克这样的:
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." );
}
}
}
}
您会再需要从它侦听BluetoothDevice.ACTION_NAME_CHANGED意图,并提取BluetoothDevice.EXTRA_NAME。
You'll then need to listen for the BluetoothDevice.ACTION_NAME_CHANGED intent, and extract BluetoothDevice.EXTRA_NAME from it.
让我知道是否有帮助。
这篇关于如何prevent名称缓存和检测上发现蓝牙名称变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!