2

I was wondering if there is any particular callback or anything similar that I could get when checking if my device is connected via bluetooth to a system of a car.

I am aware that there are some companies which change the UI when connected to these systems. Let's say, a UI for no Bluetooth connection or Bluetooth connection wearing headphones vs a completely different UI when connected to the car (for example really big buttons to be easily clicked while driving)

I had a look at BluetoothAdapter from Android. Particularly I was interested in ACTION_CONNECTION_STATE_CHANGED which could let me know when a new connection has been made, but I did not really see any way to identify when has been connected to the system of a car or any other Bluetooth device.

Is there any other way to do it? If so, please provide a simple implementation or link to good documentation.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
bellotas
  • 2,349
  • 8
  • 29
  • 60
  • 1
    Have you seen: [How to differentiate the connected bluetooth device in android programmatically?](https://stackoverflow.com/q/36328653/295004) – Morrison Chang Sep 16 '20 at 05:45
  • Yes, that seems pretty much it. Nevertheless, I am looking how to get the current device that I am connected to (BluetoothAdapter.getDefaultAdapter().getBondedDevices(); gives the list of those devices which are bonded to my phone, but i am not able to check which is currently connected) – bellotas Sep 16 '20 at 06:49
  • Possible answer: [Android check if Bluetooth connected](https://stackoverflow.com/a/47803712/295004) and related linked question: [Android check if Bluetooth connected](https://stackoverflow.com/q/4715865/295004) – Morrison Chang Sep 16 '20 at 07:16

1 Answers1

0

I have followed the question proposed in the commentaries to filter the Bluetooth connections by state (In this case I am interested in the ones which are connected).

public class BluetoothServiceListener implements BluetoothProfile.ServiceListener {
    private String TAG = "bluetoothListener";
    private static final int[] states={
            BluetoothProfile.STATE_CONNECTED};
    @Override
    public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
        List<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
        for (BluetoothDevice device:Devices){
            if(device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO);
                Log.i(TAG, "NAME | " + device.getName() + device.getBluetoothClass().getDeviceClass());
        }
    }

    @Override
    public void onServiceDisconnected(int profile) {
    }

}
bellotas
  • 2,349
  • 8
  • 29
  • 60