Android蓝牙重新扫描导致蓝牙丢失

时间:2013-12-10 13:14:21

标签: android bluetooth android-bluetooth network-scan

我正在开发与蓝牙相关的应用程序,我向用户提供了附近蓝牙设备的列表,并且我还提供了一个重新扫描按钮来重新启动扫描过程。当用户访问此视图时,应用程序启动发现过程,如果应用程序找到设备,则会显示在列表中。但是如果用户按下重新扫描按钮,首先应用清除列表然后重新开始扫描过程,则应用程序无法再次列出同一设备。 我不知道为什么应用程序无法重新扫描同一设备。

2 个答案:

答案 0 :(得分:0)

查看下面的代码:

开始搜索

mBluetoothAdapter.startDiscovery(); 
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

查找设备

if (BluetoothDevice.ACTION_FOUND.equals(action)) 
{
    // Get the BluetoothDevice object from the Intent
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    // Add the name and address to an array adapter to show in a ListView
   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};

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

答案 1 :(得分:0)

发现程序在某种意义上是概率性的,所以如果你不能在两个顺序调用中获得相同的列表,那就没问题了。我很想知道你能否在后续通话中枚举任何其他BT设备。如果答案总是“是”,并且您遇到只有该设备的问题,请检查它是否正常运行。如果在第一次成功通话后你无法枚举这些设备 - 要么你的代码不正确,要么你用于调试的设备工作得不是很好(这种情况在Android世界中经常发生)。