ペアリングされたデバイスのリストを取得する方法は理解していますが、それらが接続されているかどうかを確認するにはどうすればよいですか?
携帯電話の Bluetooth デバイス リストにそれらが表示され、接続状態が示されているので、可能であるに違いありません。
ペアリングされたデバイスのリストを取得する方法は理解していますが、それらが接続されているかどうかを確認するにはどうすればよいですか?
携帯電話の Bluetooth デバイス リストにそれらが表示され、接続状態が示されているので、可能であるに違いありません。
Bluetooth パーミッションを AndroidManifest に追加します。
<uses-permission android:name="android.permission.BLUETOOTH" />
次に、インテント フィルタを使用して、、、およびブロードキャストをリッスンACTION_ACL_CONNECTED
しACTION_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 ヘッドセットが 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" />
何らかの理由で、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...
}
};