问题
I am searching for this on the internet for quite a while, but I can't find what I am looking for.
How can I find out with my app, if my device is already connected to a Bluetooth device (/ was before I start my app).
I was hoping there was something like bool BluetoothAdapter.isPaired();
回答1:
If you are only interested if a connection to an arbitrary bluetooth device is established you can use the BluetoothAdapter.getProfileConnectionState(profile):
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
int[] profiles = {BluetoothProfile.A2DP, BluetoothProfile.HEADSET, BluetoothProfile.HEALTH};
boolean connectionExists = false;
for (int profileId : profiles) {
if (BluetoothAdapter.getProfileConnectionState(profileId) ==
BluetoothProfile.STATE_CONNECTED) {
connectionExists = true;
break;
}
}
}
回答2:
There is no way to retrieve a list of connected devices at application startup. The Bluetooth API does not allow you to QUERY, instead it allows you to listen to CHANGES.
(s. This question)
来源:https://stackoverflow.com/questions/38050396/android-check-if-bluetooth-connected