I'm trying to know at my application start up if a paired device is connect or not. In practice the main app give me an MAC address and I've got to respond if we're connected or not.
A BroadcastReceiver can't be a full option because if the user start the connection with the device before launching the app I'll be unable to know if I'm connect to the device or not.
I found this on the forum : How to programmatically tell if a Bluetooth device is connected?
There is no way to retrieve the list of connected devices at application startup. The Bluetooth API will only let you listen to connection changes.
but i think this isn't accurate anymore because with this code :
public class cServiceListener implements BluetoothProfile.ServiceListener {
private static final int[] states={ BluetoothProfile.STATE_DISCONNECTING,
BluetoothProfile.STATE_DISCONNECTED,
BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.STATE_CONNECTING};
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
//List<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
Log.i("myTag","\n\n<==============profile connexion state =============> : "+ mBluetoothAdapter.getProfileConnectionState(1));
for (BluetoothDevice loop:devices){
Log.i("myTag","Nom du device :" +loop.getName()+" Etat du Device:"+bluetoothProfile.getConnectionState(loop)
+ " Type du device : "+ loop.getType() + " Classe du device : " + loop.getBluetoothClass().toString() );
}
}
@Override
public void onServiceDisconnected(int profile) {
}}
I'm able to tell if SOME devices are connected or not but not with all of them !! I try the code with portable speakers and with others devices where the calls AND the sounds is take in charge and it work perfectly : if the device is connect it return 2 and if not it return 0. But when i try it on my real objective (which is a car) it return 0 at anytime. The test car only take in charge calls (you can't play your music with Bluetooth connection, to old). So I think with more modern car it can work..
First i was thinking that car using BLE technology so I try with this code
List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
Log.d("myTag","\n Taille de la liste : " + devices.size());
for(BluetoothDevice device : devices) {
if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
Log.d("myTag","\n BLE <===> Nom du device :" + device.getName());
}
Log.d("myTag","\n<====NONE BLE=====> // \n\n Nom du device :" + device.getName()+"\n\n");
}
But in fact this isn't the point, this don't work. And it's weird because
mBluetoothAdapter.getProfileConnectionState(1)
returning the correct value when I'm connect to the car only. I really don't know what to do know and why bluetoothProfile.getConnectionState() returning the wrong value. If someone have any idea, I'll be glad to know.