2017-06-04 199 views
0

大家好,有沒有辦法檢查Android手機是否通過編程方式連接到任何藍牙設備?Android:確定藍牙是否連接到任何設備

應該存在諸如Bluetooth_state == Bluetooth_connectedBluetooth_state == Bluetooth_disconnectedBluetooth.isConnected()的狀態。目標是識別手機的藍牙是否連接到任何設備。

+0

至少有人建議我的東西。請。 – varun

+0

那麼,至少你可以檢查藍牙耳機'BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)'... –

回答

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. 

不要忘記在manifest中要求同意。

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

Original Answer by @jobbert