2010-03-22 53 views
0

我通过执行任务来实现蓝牙支持,该任务在后台搜索设备并在搜索完成时提供列表。但是,此列表偶尔会包含No devices found条目(仅当收到ACTION_DISCOVERY_FINISHED且列表中没有设备时才添加),然后再列出其他设备!安卓蓝牙发现:ACTION_FOUND发生在ACTION_DISCOVERY_FINISHED后

private BroadcastReceiver mBlueToothInfoDiscoveryListener = new BroadcastReceiver() { 
    /** 
    * This is an overridden function called by the framework whenever there 
    * is some broadcast message for Bluetooth info discovery listener 
    */ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    // When discovery finds a device 
    if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
     // Get the BluetoothDevice object from the Intent 
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     // If it's already paired, skip it, because it's been listed 
     // already 
     if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
     mNewBtDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
     } 
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
     // When discovery is finished, change the Activity title 
     setProgressBarIndeterminateVisibility(false); 
     setTitle("device list"); 
     if (mNewBtDevicesArrayAdapter.getCount() == 0) { 
     String noDevices = "No devices found"; 
     mNewBtDevicesArrayAdapter.add(noDevices); 
     } 
    } 
    } 
}; 

我不希望ACTION_DISCOVERY_FINISHED到以往任何时候都一个ACTION_FOUND事件,那么,为什么在No devices found字符串添加到列表中的设备位于前?

+0

我有同样的问题,我的答复是在这里: http://stackoverflow.com/ a/19433923/2701314 关于 – 2013-10-17 18:06:11

回答

2

只是因为发现任务有一个超时,以防止无休止的搜索。所以,如果没有可用的设备周围,你会得到你“未找到设备” ......