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

时间:2017-06-04 14:17:50

标签: android bluetooth

大家好,有没有办法检查Android手机是否以编程方式连接到任何蓝牙设备?

是否存在Bluetooth_state == Bluetooth_connectedBluetooth_state == Bluetooth_disconnectedBluetooth.isConnected()等州。目标是识别手机的蓝牙是否连接到任何设备。

1 个答案:

答案 0 :(得分:0)

如果您只想在启动时检查设备是否已连接,请尝试mBluetoothAdapter.getProfileConnectionState();应该适合你。

 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.

不要忘记在舱单中申请许可。

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

Original Answer by @jobbert