如何以编程方式确定蓝牙设备是否已连接? (Android 2.2)
我知道如何获得配对设备的清单,但我怎么知道他们是否连接?
这一定是可能的,因为我看到他们列在我的手机的蓝牙设备列表中,并说明他们的连接状态。
使用意向filter侦听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 } } };
一些注意事项:
- 应用程序启动时无法检索连接的设备列表。 蓝牙API不允许你QUERY,而是允许你听CHANGES。
- 一个愚蠢的解决上述问题将检索所有已知/配对设备的列表…然后试图连接到每一个(以确定是否连接)。
- 或者,您可以使用后台服务观看Bluetooth API并将设备状态写入磁盘,供应用程序在以后使用。
在我的使用案例中,我只想看看是否为VoIP应用程序连接了蓝牙耳机。 以下解决scheme为我工作:
public static boolean isBluetoothHeadsetConnected() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED; }
当然,你需要蓝牙许可:
<uses-permission android:name="android.permission.BLUETOOTH" />
非常感谢Skylarsutton的回答。 我发布这个作为他的回应,但因为我发布的代码,我不能作为评论回复。 我已经提出了他的答案,所以没有find任何点。 只要付钱。
出于某种原因BluetoothAdapter.ACTION_ACL_CONNECTED无法由Android Studioparsing。 也许它在Android 4.2.2中被弃用? 这是他的代码的修改。 注册码是一样的; 接收器代码略有不同。 我使用这个更新蓝牙连接标志的应用程序的其他部分引用的服务。
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... } };