Bluetoothが他のデバイス(モバイル、ヘッドセットなど)に接続されているかどうかを確認する方法を教えてもらえますか?
8792 次
3 に答える
4
現在接続されているデバイスのリストを取得する方法はわかりませんが、ACL_CONNECTED インテントを使用して新しい接続をリッスンできます: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED
このインテントには、接続先のリモート デバイスを含む追加のフィールドが含まれます。
Android では、すべての Bluetooth 接続が ACL 接続であるため、このインテントに登録すると、すべての新しい接続が取得されます。
したがって、レシーバーは次のようになります。
public class ReceiverBlue extends BroadcastReceiver {
public final static String CTAG = "ReceiverBlue";
public Set<BluetoothDevice> connectedDevices = new HashSet<BluetoothDevice>();
public void onReceive(Context ctx, Intent intent) {
final BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase( action ) ) {
Log.v(CTAG, "We are now connected to " + device.getName() );
if (!connectedDevices.contains(device))
connectedDevices.add(device);
}
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase( action ) ) {
Log.v(CTAG, "We have just disconnected from " + device.getName() );
connectedDevices.remove(device);
}
}
}
于 2012-04-04T23:48:29.680 に答える
0
getBondedDevices() が役立つと思います:)
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
ありがとう :)
于 2012-04-04T21:30:58.977 に答える