题
我了解如何获取配对设备的列表,但是如何确定它们是否已连接?
必须有可能,因为我看到它们在手机的蓝牙设备列表中列出,并说明了它们的连接状态。
解决方案
将蓝牙权限添加到您的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
}
}
};
一些注释:
- 在应用程序启动时无法检索连接设备的列表。蓝牙API不允许您查询,而是允许您收听更改。
- 解决上述问题的工作将是检索所有已知/配对设备的列表...然后尝试连接到每个设备(确定是否已连接)。
- 另外,您可以拥有背景服务观看蓝牙API,并将设备状态编写为磁盘以供您以后使用的应用程序。
其他提示
在我的用例中,我只想查看是否连接了VoIP应用程序的蓝牙耳机。以下解决方案对我有用:
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的回答。我将其发布为对他的回应,但是由于我发布的代码我无法评论。我已经为他的答案提供了投票,所以不在寻找任何观点。只是向前付款。
由于某种原因,bluetoothadapter.action_acl_connected无法通过Android Studio解决。也许它在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...
}
};
此代码适用于耳机配置文件,可能也适用于其他配置文件。首先,您需要提供个人资料侦听器(Kotlin代码):
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
}
}
}
然后在检查蓝牙时:
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET)
if (!mBluetoothAdapter.isEnabled) {
return Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
}
它需要一些时间,直到被称为OnseviceConnecon。之后,您可能会从:
mBluetoothHeadset!!.connectedDevices
BluetoothAdapter.getDefaultAdapter().isEnabled
- >蓝牙打开时返回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.
}
它也可以在Kotlin提供: https://developer.android.com/guide/topics/connectivition/bluetooth#querypaireddevices