2010-03-22 91 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

只是因爲發現任務有一個超時,以防止無休止的搜索。所以,如果沒有可用的設備周圍,你會得到你「未找到設備」 ......