0

我正在创建一个简单的应用程序,它将通过蓝牙进行通信。我已经创建了一个简单的活动,列出了附近打开了蓝牙的设备,但不幸的是,我不知道如何检测到某些设备何时从蓝牙网络中消失(bt 被关闭),以便我可以从名单。

这是我编写的将附近的 BT 设备添加到 ListView 的代码:

mNewDevicesArrayAdapter = new BluetoothDeviceArrayAdapter(this, 0, new ArrayList<BluetoothDevice>());

lvDiscovered = (ListView)findViewById(R.id.bt_dev_discovered_list);
lvDiscovered.setAdapter(mNewDevicesArrayAdapter);

...

// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      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);
              // If it's already paired, skip it, because it's been listed already
              if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                  mNewDevicesArrayAdapter.add(device);
              }
          // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              // TODO show no devices found!
          }              
      }
  };

当设备消失时,我发现没有适用的 ACTION 意图。也许可以使用 ACTION_DISCOVERY_FINISHED,但是如何使用呢?

提前致谢!

4

1 回答 1

0

我找到了一种从先前发现的列表中删除这些设备的简单方法。

在上面扩展我的代码时,我介绍了一个update list存储新发现设备的位置。当ACTION_DISCOVERY_FINISHED出现时,我将ListView使用此更新列表进行更新。

...
private ArrayList<BluetoothDevice>   btDevicesUpdateList;
...

// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    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);

            // If it's already paired, skip it, because it's been listed already
            // in the paired devices list
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                if(mNewDevicesArrayAdapter.getCount() == 0){
                   // if the list is empty we add the device immediately to it
                    mNewDevicesArrayAdapter.add(device);
                }
                btDevicesUpdateList.add(device);                      
             }
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           mNewDevicesArrayAdapter.setItems(btDevicesUpdateList);
           btDevicesUpdateList.clear();
           mBtAdapter.startDiscovery();
        }
    }
}  

// BluetoothDeviceArrayAdapter.java
public void setItems(ArrayList<BluetoothDevice> items){
    this.items.clear();
    this.items.addAll(items);
}

不可用的设备将不在列表中。

于 2012-06-21T20:35:49.250 回答