大家好,有没有办法检查Android手机是否以编程方式连接到任何蓝牙设备?
是否存在Bluetooth_state == Bluetooth_connected
或Bluetooth_state == Bluetooth_disconnected
或Bluetooth.isConnected()
等州。目标是识别手机的蓝牙是否连接到任何设备。
答案 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" />