Ria
Ria

Reputation: 873

Currently connected bluetooth device android

I can able to see two states in Bluetooth device in Android. 1. Paired 2. Connected. -
I am trying to get currently connected Bluetooth device in Android. But I am getting only paired device list from adapter.getBondedDevices(); I need currently connected device. How can i get this. Please someone help me to achieve this. Thanks in advance.

Upvotes: 17

Views: 9231

Answers (4)

lixiaodaoaaa
lixiaodaoaaa

Reputation: 470

I finally find this below can Work

We can use the reflect tech to invoke the Android SDK method to get already connected devices . Below is the code that works for me .

private void checkBluetoothGamepadIsConnected() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;
        boolean isConnectedGamePad = false;
        try {
            Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState",
                                                                    (Class[]) null);
            //Open permission.
            method.setAccessible(true);
            int state = (int) method.invoke(adapter, (Object[]) null);
            LogUtil.d("MainViewModel" + "getConnectedBluetooth() state:" + state);
            LogUtil.d(TAG + "getConnectedBluetooth() state:" + state
                              + " stateSTATE_CONNECTED:" + BluetoothAdapter.STATE_CONNECTED
                              + " STATE_CONNECTING:" + BluetoothAdapter.STATE_CONNECTING
                              + " STATE_DISCONNECTED:" + BluetoothAdapter.STATE_DISCONNECTED);

            Set<BluetoothDevice> devices = adapter.getBondedDevices();
            LogUtil.d(TAG + "getConnectedBluetooth devices.size:" + devices.size());
            for (BluetoothDevice device : devices) {
                Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected",
                                                                                   (Class[]) null);
                method.setAccessible(true);
                boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
                LogUtil.d(TAG + "getConnectedBluetooth() isConnected:" + isConnected
                                  + " getAddress:" + device.getAddress() + " getName:" + device.getName());
                if (isConnected) {
                    LogUtil.d(TAG + "getConnectedBluetooth() ** Connected getAddress:"
                                      + device.getAddress() + " getName:" + device.getName() + " getBondState:"
                                      + device.getBondState() + " getType:" + device.getType());
                    if(device.getName().contains(GAMEPAD_NAME)){
                        isConnectedGamePad = true;
                        break;
                    }
                }
            }

        } catch (Exception e) {
            LogUtil.d(TAG + "getConnectedBluetooth() error:" + e.toString());
            e.printStackTrace();
        }
        isGamePadConnectedData.postValue(isConnectedGamePad);
    }

detail explanation

You use below method cannot know the device now is connected or not , you must use the invoke ,check the connect or not connect.

Set<BluetoothDevice> devices = adapter.getBondedDevices();

the Best important is here

    Set<BluetoothDevice> devices = adapter.getBondedDevices();
            LogUtil.d(TAG + "getConnectedBluetooth devices.size:" + devices.size());
            for (BluetoothDevice device : devices) {
                Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected",
                                                                                   (Class[]) null);
                method.setAccessible(true);
                boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
                LogUtil.d(TAG + "getConnectedBluetooth() isConnected:" + isConnected
                                  + " getAddress:" + device.getAddress() + " getName:" + device.getName());
                if (isConnected) {
                    LogUtil.d(TAG + "getConnectedBluetooth() ** Connected getAddress:"
                                      + device.getAddress() + " getName:" + device.getName() + " getBondState:"
                                      + device.getBondState() + " getType:" + device.getType());
                    if(device.getName().contains(GAMEPAD_NAME)){
                        isConnectedGamePad = true;
                        break;
                    }
                }
            }

explanation the important

about the constant GAMEPAD_NAME is my bluetooth device name, what I do is checked my Bluetooth is now connected or not .

when my Bluetooth is connected ,it will return true. the variable isConnectedGamePad = true;

my livedata will post the boolean value isConnect.

that work it well

Upvotes: 0

notmystyle
notmystyle

Reputation: 1066

I previously was using a method to get connected devices by the "headset" profile, but I ran in to an issue with a Samsung Galaxy Watch 4 where it comes up as a "headset" device even though it does not support audio. This is problematic when just asking if a "headset" is connected, because you may inadvertently try to send audio to that device even though it doesn't support it.

To get all of the connected devices you need to use a ServiceListener. The good news is that the service listener will always list the devices connected, and you can use that to inspect if they support audio or not. To simplify things further, I used a callback flow like follows:

@OptIn(ExperimentalCoroutinesApi::class)
internal fun headsetConnectedAndSupportsAudio() = callbackFlow<Boolean> {
    val serviceListener = object : BluetoothProfile.ServiceListener {

        override fun onServiceDisconnected(profile: Int) = Unit // no op

        override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
            var connected = false
            var supportsAudio = false

            // check for a device that supports audio and is connected in our connected bluetooth devices.
            for (device in proxy.connectedDevices) {
                connected = proxy.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED
                supportsAudio = device.bluetoothClass.hasService(BluetoothClass.Service.AUDIO)

                Timber.d("Bluetooth Device - ${device.name} isConnected: $connected supportsAudio: $supportsAudio")
                // we have found a connected device that supports audio, stop iterating and emit a success
                if (connected && supportsAudio) { break }
            }

            trySend(connected && supportsAudio)
                .onClosed { throwable -> Timber.e(throwable) }
                .isSuccess

            getBluetoothAdapter().closeProfileProxy(profile, proxy)

            close()
        }
    }

    // register our service listener to receive headset connection updates
    getBluetoothAdapter().getProfileProxy(
        context,
        serviceListener,
        BluetoothProfile.HEADSET
    )

    awaitClose { channel.close() }
}

And then to use the callback flow you do something like:

mainScope.launch {
   headsetConnectedAndSupportsAudio().cancellable().collect { btAudioSourceConnected ->
       if (btAudioSourceConnected) {
           Timber.d("Bluetooth headset connected + supports audio"
       } else {
           Timber.d("No Bluetooth headset connected that supports audio")
       }
   }

}

Upvotes: 0

Salman Naseem
Salman Naseem

Reputation: 462

That's pretty straight forward. Android BluetoothManager have method of

getConnectedDevices()

Implementation like:

BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    List<BluetoothDevice> connected = manager.getConnectedDevices(GATT);
    Log.i("Connected Devices: ", connected.size()+"");

If you want more details about connected devices then you can use the above list method put it into for loop and get the inner details of each Bluetooth device which are connected.

Logs:

12-20 18:04:09.679 14933-14933/com.salman.dleague.blescanning I/Connected Devices:: 2

Hope its helpful :)

Upvotes: 11

Saurabh Mistry
Saurabh Mistry

Reputation: 13689

Add this in your manifest file

<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" 
/>
<action 
android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" 
/>           
</intent-filter>  
</receiver>  

Add this Class

public class MyBluetoothReceiver extends BroadcastReceiver {
 @Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    // When discovery finds a device
    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

    BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

     Toast.makeText(getApplicationContext(),device.getName() +" CONNECTED",Toast.LENGTH_LONG).show();

    } else if (BluetoothAdapter.ACL_DISCONNECTED
            .equals(action)) {

    }
}
}

Upvotes: 2

Related Questions