90

ペアリングされたデバイスのリストを取得する方法は理解していますが、それらが接続されているかどうかを確認するにはどうすればよいですか?

携帯電話の Bluetooth デバイス リストにそれらが表示され、接続状態が示されているので、可能であるに違いありません。

4

7 に答える 7

165

Bluetooth パーミッションを AndroidManifest に追加します。

<uses-permission android:name="android.permission.BLUETOOTH" />

次に、インテント フィルタを使用して、、、およびブロードキャストをリッスンACTION_ACL_CONNECTEDACTION_ACL_DISCONNECT_REQUESTEDますACTION_ACL_DISCONNECTED

public void onCreate() {
    ...
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter);
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           ... //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           ... //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           ... //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           ... //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           ... //Device has disconnected
        }
    }
};

いくつかのメモ:

  • アプリケーションの起動時に、接続されているデバイスのリストを取得する方法はありません。Bluetooth API では、クエリを実行することはできませんが、変更をリッスンすることはできます。
  • 上記の問題に対するおかしな回避策は、すべての既知の/ペアリングされたデバイスのリストを取得することです...そして、それぞれに接続しようとします(接続されているかどうかを判断するため)。
  • または、バックグラウンド サービスで Bluetooth API を監視し、アプリケーションが後で使用できるようにデバイスの状態をディスクに書き込むこともできます。
于 2011-01-17T18:52:49.147 に答える
41

私の使用例では、Bluetooth ヘッドセットが VoIP アプリに接続されているかどうかだけを確認したかったのです。次の解決策は私にとってはうまくいきました。

コトリン:

fun isBluetoothHeadsetConnected(): Boolean {
    val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    return (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled
        && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED)
}

ジャワ:

public static boolean isBluetoothHeadsetConnected() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
            && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
} 

もちろん、Bluetooth パーミッションが必要です。

<uses-permission android:name="android.permission.BLUETOOTH" />

于 2016-12-16T09:47:57.407 に答える
12

何らかの理由で、BluetoothAdapter.ACTION_ACL_CONNECTED が Android Studio で解決できませんでした。おそらく、Android 4.2.2 で非推奨になったのでしょうか?

以下は、Skylarsutton のコードの修正です (Skylarsutton の回答に感謝します)。登録コードは同じです。レシーバーコードはわずかに異なります。アプリの他の部分が参照する Bluetooth 接続フラグを更新するサービスでこれを使用します。

    public void onCreate() {
        //...
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(BTReceiver, filter);
    }

    //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            //Do something if connected
            Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_SHORT).show();
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            //Do something if disconnected
            Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
        }
        //else if...
    }
};
于 2013-08-01T23:45:12.267 に答える