[英]Android: Determine if Bluetooth is connected to any devices or not
大家好,有沒有辦法檢查Android手機是否已通過編程方式連接到任何藍牙設備?
是否應存在諸如Bluetooth_state == Bluetooth_connected
或Bluetooth_state == Bluetooth_disconnected
或Bluetooth.isConnected()
。 目的是識別電話的藍牙是否連接到任何設備。
如果只想在啟動時檢查設備是否已連接,請嘗試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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.