Currently connected bluetooth device android
Asked Answered
B

4

17

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.

Birdella answered 28/10, 2015 at 5:57 Comment(4)
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> use this permission also check this link to #14834818Tutelary
The question is not entirely clear for me. What exactly do you do? You create Connected btlDevice when doing btlDevice.CreateRfcommSocketToServiceRecord (MY_UUID); Alternatively the system use last connected btlDevice. Please more detail what you doing.Nitty
Check this #26342218Bonhomie
I found a solution and it works on android 10 codePonder
Z
11

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 :)

Zackaryzacks answered 20/12, 2017 at 13:9 Comment(6)
Do you have a solution for < API 18?Alric
Isn't this for BLE? I tried connect my smartphone for media audio. Both GATT and GATT_SERVER return 0 sizeDownes
my device already connected BT, but still receiving "0" @Downes . May I Know have you any solution >Sassafras
@GNK. Sorry no. I ened up with no need to work on this. For viewing BT devices from Android Setting UI, maybe you can checkout on Setting app source code. I have seen this but it is quite complex..Also the comments under the question (for Android 10) may helpDownes
set GATT = BluetoothProfile.GATT_SERVER , work as wellDinodinoflagellate
that's pretty unprofessional answer because it won't return all connected devicesCaldron
S
2

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)) {

    }
}
}
Sabbat answered 15/12, 2017 at 7:11 Comment(1)
This will only give notification when the device gets Connected. What about the device is already connected and then you launch your app.? How to get the connected devices?Nixie
A
0

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

Arsenite answered 28/10, 2015 at 5:57 Comment(1)
reflection is forbidden in newer AndroidCaldron
C
0

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")
       }
   }

}

Corcoran answered 24/2, 2022 at 22:49 Comment(2)
how to make this run always? this only works like a method call and not like a broadcast receiver?Perrins
I have this set up to use a callback flow. For me, this simplifies the check in that I can treat it like a function call that will return the answer. This also let me be very explicit when I wanted the check to occur, which was important for my use-case.Corcoran

© 2022 - 2024 — McMap. All rights reserved.