如何防止名称缓存并检测发现时的蓝牙名称更改

如何防止名称缓存并检测发现时的蓝牙名称更改

本文介绍了如何防止名称缓存并检测发现时的蓝牙名称更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个从蓝牙设备接收信息的 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).

我的代码可以很好地检测 BT 设备并找到它们的名称.此外,如果设备关闭,我下次搜索时可以检测到它不在那里.但似乎如果它在那里并且更改了名称,我会选择旧名称 - 大概它被缓存在某个地方.即使蓝牙设备关闭,我们注意到,下次我检测到它时,我仍然看到旧名称.

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.

我在 Google 代码中发现了这个问题:这里但是我什至不清楚如何使用给定的解决方法(尝试连接").有没有人做过这个并且有任何运气?可以分享代码吗?

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 会导致设备更新有关远程设备的缓存信息.我相信这包括远程名称以及 SPD.

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 之前都是隐藏的,所以你的代码看起来像这样:

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.

如果这有帮助,请告诉我.

Let me know if that helps.

这篇关于如何防止名称缓存并检测发现时的蓝牙名称更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:44