Bluetooth connection with the car

I am trying to check when my device is connected to a car. I assume that the car acts like a Bluetooth headset, so I used the following code in my onCreate activity:

// Get the default adapter BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { public void onServiceConnected(int profile, BluetoothProfile proxy) { Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); if (profile == BluetoothProfile.HEADSET) { mBluetoothHeadset = (BluetoothHeadset) proxy; LogginUtil.logString("BluetoothApp", "Headset event called at " + today.format("%k:%M:%S") + " - " + profile); } else { LogginUtil.logString("BluetoothApp", "Other event called at " + today.format("%k:%M:%S") + " - " + profile); } } public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.HEADSET) { mBluetoothHeadset = null; Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); LogginUtil.logString("BluetoothApp", "Headset event disconnected at " + today.format("%k:%M:%S")); } } }; // Establish connection to the proxy. mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.HEADSET); 

When I launch the application, when turning on and off bluetooth, I get the following output:

 Headset event called at "current time" - 1 

When I connect my device to the car, I get exactly the same result:

 Headset event called at "current time" - 1 

What do I need to do to find out that my device is actively connecting via Bluetooth with the car?

Thank you in advance, and let mw know if you need anything else.

PICTURE PICTURE

Just in case, I did not understand my question. I want to receive notifications (log only) when the device enters the state of connecting to the car via Bluetooth. Is this possible?

+5
source share
2 answers

I can't try it right now, but maybe this might work:

 int[] validStates = {BluetoothHeadset.STATE_AUDIO_CONNECTED}; List<BluetoothDevice> mConnectedDevices = mBluetoothHeadset.getDevicesMatchingConnectionStates(validStates); if (!mConnectedDevices.isEmpty()) { // You've got something connected here } 

Sources:

http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html http://developer.android.com/reference/android/bluetooth/BluetoothProfile.html#getConnectedDevices ()

+1
source

A quick answer from Google found this answer, it worked for quite a lot of people, so I'm sure it should work for you. Source: How to get the connection status of the Bluetooth headset when launching the application using Android2.2?

 private static final String ACTION_BT_HEADSET_STATE_CHANGED = "android.bluetooth.headset.action.STATE_CHANGED"; private static final int STATE_CONNECTED = 0x00000002; private static final int STATE_DISCONNECTED = 0x00000000; private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE"; private BroadcastReceiver mBlueToothHeadSetEventReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { try { String action = intent.getAction(); if(action == null) return; if(action.equals(ACTION_BT_HEADSET_STATE_CHANGED)){ int extraData = intent.getIntExtra(EXTRA_STATE , STATE_DISCONNECTED); if(extraData == STATE_CONNECTED ){ //TODO : }else if(extraData == STATE_DISCONNECTED){ //TODO: } } } catch (Exception e) { //TODO: } } }; 
0
source

All Articles