0

Bluetooth経由で通信する簡単なアプリケーションを作成しています。Bluetoothがオンになっている近くのデバイスを一覧表示する簡単なアクティビティをすでに作成しましたが、残念ながら、一部のデバイスがBluetoothネットワークから消えた(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 に答える