0

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

4

2 回答 2

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);
于 2013-12-10T13:49:33.777 回答
0

Discovery 过程在某种意义上是概率性的,因此如果您无法在两次连续调用中获得相同的列表,那也没关系。我想知道您是否能够在随后的通话中枚举任何其他 BT 设备。如果答案总是“是”,并且您在使用该唯一设备时遇到问题,请检查它是否正常运行。如果在第一次成功调用后您无法枚举设备 - 您的代码不正确,或者您用于调试的设备运行不佳(这在 Android 世界中经常发生)。

于 2014-03-20T14:34:03.460 回答