简体   繁体   English

Android:确定蓝牙是否已连接到任何设备

[英]Android: Determine if Bluetooth is connected to any devices or not

Hello everyone is there a way to check if Android phone is connected to any Bluetooth devices programmatically? 大家好,有没有办法检查Android手机是否已通过编程方式连接到任何蓝牙设备?

Should there be a state such as Bluetooth_state == Bluetooth_connected OR Bluetooth_state == Bluetooth_disconnected OR Bluetooth.isConnected() . 是否应存在诸如Bluetooth_state == Bluetooth_connectedBluetooth_state == Bluetooth_disconnectedBluetooth.isConnected() The goal is to recognise if Phone's Bluetooth is connected to any device or not. 目的是识别电话的蓝牙是否连接到任何设备。

If you only want to check that device is connected or not upon launch, try mBluetoothAdapter.getProfileConnectionState() ; 如果只想在启动时检查设备是否已连接,请尝试mBluetoothAdapter.getProfileConnectionState() ; should work for you. 应该为您工作。

 public static boolean isBluetoothHeadsetConnected() {
 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
       && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
}//BluetoothHeadset.A2DP can also be used for Stereo media devices.

Don't forget to ask for permission in manifest as well. 别忘了在清单中请求许可。

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

Original Answer by @jobbert @jobbert的原始答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM