Bluetoothデバイスが接続されているかどうかをプログラム的に伝える方法は?
-
12-10-2019 - |
質問
ペアのデバイスのリストを取得する方法は理解していますが、接続されているかどうかを確認するにはどうすればよいですか?
携帯電話のBluetoothデバイスリストにリストされているのを見て、接続ステータスを述べているので、可能でなければなりません。
解決
AndroidManifestにBluetoothの許可を追加し、
<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 APIはクエリを許可していないため、変更を聞くことができます。
- 上記の問題に取り組むのは、すべての既知/ペアのデバイスのリストを取得することです...それぞれに接続しようとします(接続しているかどうかを判断するため)。
- または、バックグラウンドサービスにBluetooth APIを監視し、アプリケーションを後日使用するためにデバイスの状態をディスクに書き込むこともできます。
他のヒント
私のユースケースでは、bluetoothヘッドセットがVoIPアプリに接続されているかどうかを確認したかったのです。次のソリューションは私のために機能しました:
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" />
彼の答えをSkylarsuttonに感謝します。私は彼への応答としてこれを投稿していますが、コードを投稿しているため、コメントとして返信することはできません。私はすでに彼の答えを支持していたので、ポイントを探していません。ただそれを前払いするだけです。
何らかの理由で、bluetoothadapter.action_acl_connectedはAndroid Studioによって解決できませんでした。おそらく、Android 4.2.2で非難されたのでしょうか?彼のコードの変更です。登録コードは同じです。レシーバーコードはわずかに異なります。これを、アプリの他の部分が参照するBluetooth接続フラグを更新するサービスで使用します。
public void onCreate() {
//...
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
}
//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...
}
};
このコードはヘッドセットプロファイル用ですが、おそらく他のプロファイルでも機能します。まず、プロフィールリスナー(Kotlin Code)を提供する必要があります。
private val mProfileListener = object : BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile == BluetoothProfile.HEADSET)
mBluetoothHeadset = proxy as BluetoothHeadset
}
override fun onServiceDisconnected(profile: Int) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null
}
}
}
その後、Bluetoothのチェック中:
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET)
if (!mBluetoothAdapter.isEnabled) {
return Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
}
onseviceConnectedが呼び出されるまで少し時間がかかります。その後、以下から接続されたヘッドセットデバイスのリストを取得できます。
mBluetoothHeadset!!.connectedDevices
BluetoothAdapter.getDefaultAdapter().isEnabled
- > Bluetoothが開いているときにtrueを返します
val audioManager = this.getSystemService(Context.AUDIO_SERVICE) as
AudioManager
audioManager.isBluetoothScoOn
- >デバイスが接続されたときにtrueを返します
このスレッドは古いものであることは知っていますが、アプリの起動時にデバイスが接続されているかどうかを知る必要がありました。
//List of Paired Devices
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
else {
//There are no paired devices.
}
コトリンでも入手できます。 https://developer.android.com/guide/topics/connectivity/bluetooth#querypaireddevices