android蓝牙发现:在ACTION_DISCOVERY_FINISHED之后发生ACTION_FOUND

时间:2010-03-22 09:22:03

标签: java android bluetooth

我通过实施在后台搜索设备并在搜索完成时提供列表的任务来实现蓝牙支持。但是,在列出其他设备之前,此列表偶尔会包含No devices 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_FINISHEDACTION_FOUND事件之前来,所以为什么在设备之前将No devices found字符串添加到列表中位于?

1 个答案:

答案 0 :(得分:2)

仅仅因为发现任务有超时以防止无休止的搜索。因此,如果周围没有可用的设备,您将获得“未找到设备”...