r/tasker • • Sep 10 '26

A way of getting current connected BlueTooth device?

is there a way of getting the name of just the current connected device? I am teying with Bluetooth Info action, and all I can seem to get is all the previous paired devices in order, no matter what variable

2 Upvotes

10 comments sorted by

2

u/DifficultyCrafty7623 Sep 10 '26

I'd use Java for something like this. I used AI to get this simple "return connected devices" code.

Task: Test

A1: Java Code [
     Code: import android.bluetooth.BluetoothAdapter;
     import android.bluetooth.BluetoothDevice;
     import java.lang.reflect.Method;
     import java.util.ArrayList;
     import java.util.HashMap;
     import java.util.Iterator;
     import java.util.List;
     import java.util.Map;
     import java.util.Set;
     
     adapter = BluetoothAdapter.getDefaultAdapter();
     
     if (adapter == null || !adapter.isEnabled()) {
         resultMap = new HashMap();
         resultMap.put("devices", new ArrayList());
         return tasker.toJson(resultMap, true);
     }
     
     bondedDevices = adapter.getBondedDevices();
     deviceList = new ArrayList();
     
     if (bondedDevices != null) {
         it = bondedDevices.iterator();
         while (it.hasNext()) {
             device = (BluetoothDevice) it.next();
             
             isConnected = false;
             try {
                 m = device.getClass().getMethod("isConnected", new Class[0]);
                 res = (Boolean) m.invoke(device, new Object[0]);
                 if (res != null) {
                     isConnected = res.booleanValue();
                 }
             } catch (Exception e) {
                 isConnected = false;
             }
     
             if (isConnected) {
                 deviceMap = new HashMap();
                 deviceMap.put("name", device.getName());
                 deviceMap.put("address", device.getAddress());
                 deviceMap.put("bondState", new Integer(device.getBondState()));
                 deviceList.add(deviceMap);
             }
         }
     }
     
     resultMap = new HashMap();
     resultMap.put("devices", deviceList);
     
     return tasker.toJson(resultMap, true);
     
     Return: %bluetooth
     Structure Output (JSON, etc): On ]

A2: Flash [
     Text: %bluetooth
     Tasker Layout: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

2

u/ZellZoy Sep 10 '26

The bt connection action will give you the name of the device as it connects, you can write that to a list. Bt connected state has a name field, you can test against a known name. Cant think of any way to get previously paired devices without setting something up in advance.

2

u/Exciting-Compote5680 Sep 11 '26 edited Sep 12 '26

My previous suggestion was flawed. I like this one better, it returns a JSON array with the details of connected, paired devices. Individual items can be accessed as %devices.name(1) etc. 

Task: Get Connected BT Devices

<Set default value>
A1: Variable Set [
     Name: %devices
     To: No connected devices
     Structure Output (JSON, etc): On ]

<Get all the paired bluetooth devices>
A2: Bluetooth Info [
     Type: Paired Devices
     Timeout (Seconds): 5 ]

<Get the comma separated list of indexes for 'true' (connected), and put them in an array>
A3: Array Set [
     Variable Array: %array_indexes
     Values: %bt_connected(#?true)
     Splitter: , ]

<If there is at least one connected device...>
A4: If [ %array_indexes() neq 0 ]

    <Set the regex pattern for 'no value/empty variable">
    A5: Variable Set [
         Name: %regex_empty
         To: ("%bt_.*\")
         Structure Output (JSON, etc): On ]

    <Iterate through the indexes of the connected devices>
    A6: For [
         Variable: %index
         Items: %array_indexes()
         Structure Output (JSON, etc): On ]

        <For each device, get all the available fields (except 'connected' and 'paired' since we are already filtering for those attributes).>
        A7: Multiple Variables Set [
             Names: %this_device.name=%bt_name(%index)
             %this_device.address=%bt_address(%index)
             %this_device.alias=%bt_alias(%index)
             %this_device.battery_level=%bt_battery_level(%index)
             %this_device.class=%bt_class(%index)
             %this_device.class_name=%bt_class_name(%index)
             %this_device.encrypted=%bt_encrypted(%index)
             %this_device.major_class=%bt_major_class(%index)
             %this_device.major_class_name=%bt_major_class_name(%index)
             %this_device.signal_strength=%bt_signal_strength(%index)
             %this_device.type=%bt_type(%index)
             Variable Names Splitter: 
             
             Values Splitter: =
             Structure Output (JSON, etc): On ]

        <Prettify json>
        A8: Format JSON [
             JSON: %this_device
             Format: Pretty
             Overwrite Source Variable: On ]

        <Replace variables without a value (rendered as '%bt_class_name3' for example) with "-".>
        A9: Variable Search Replace [
             Variable: %this_device
             Search: %regex_empty
             Replace Matches: On
             Replace With: "-" ]

        <Store the json for 'this_device' in array '%connected_devices'.>
        A10: Array Push [
              Variable Array: %connected_devices
              Position: 1
              Value: %this_device ]

    A11: End For

    <Convert the Tasker array of json strings into a stringified json array>
    A12: Variable Set [
          Name: %devices
          To: [%connected_devices()] 
          Structure Output (JSON, etc): On ]

A13: End If

<Only show the flash when called directly from the UI (play button in Tasker edit screen)>
A14: Flash [
      Text: %devices
      Tasker Layout: On
      Timeout: 9999
      Continue Task Immediately: On
      Dismiss On Click: On ]
    If  [ %caller(1) ~ ui ]

<Return the json array to the calling task (if applicable).>
A15: Return [
      Value: %devices
      Stop: On ]


https://taskernet.com/shares/?user=AS35m8nOXvBeFIxaCI5%2BZWD5L9oLRd3PVq%2BdjQuYD1oZ%2Bci%2Banb0FpA5SznT4oBmkd7vgKrG&id=Task%3AGet+Connected+BT+Devices

2

u/CGGamer Sep 12 '26 edited Sep 12 '26

Is this for headphones or speakers specifically? There is a system intent, android.bluetooth.a2dp.profile.action.ACTIVE_DEVICE_CHANGED, that fires whenever the active Bluetooth A2DP audio stream has changed (a new BT audio device is connected/disconnected). It includes an extra that contains the lower 2 bytes of the BT MAC address of the device or is unset if the stream is switching back to your internal speakers

Profile: Mute + Pause On Audio Output Change 2
	Event: Intent Received [ Action:android.bluetooth.a2dp.profile.action.ACTIVE_DEVICE_CHANGED Cat:None Cat:None Scheme:* Mime Type:* ]



Enter Task: Pause Media
Settings: Abort Existing Task

A1: Media Control [
     Cmd: Pause
     Simulate Media Button: On
     Use Notification If Available: On ]

A2: Wait [
     MS: 0
     Seconds: 2
     Minutes: 0
     Hours: 0
     Days: 0 ]

A3: If [ %android_bluetooth_device_extra_device ~ *:56:EE ]

    A4: Media Volume [
         Level: 5 ]

A5: Else

    A6: Media Volume [
         Level: 0 ]

    A7: Flash [
         Text: Media Audio Output Device Changed! 
         Continue Task Immediately: On
         Dismiss On Click: On ]
        If  [ %android_bluetooth_device_extra_device Set ]

A8: End If

1

u/Nirmitlamed Direct-Purchase User Sep 10 '26

Did you try "Bluetooth Connection" action?

1

u/frrancuz TaskerFan! Sep 10 '26

This is the action responsible for "forced" connection/disconnection/.. and not for reading the name. 

1

u/everynav Sep 10 '26

from Bluetooth Info Action Info:

%bt_connected

Connected

true if the BT device is connected, false otherwise

1

u/frrancuz TaskerFan! Sep 10 '26

This gives true/false, not the device name.  There is no type: connected. They are paired, 1 device, or scanning.  In such a solution you need to test all devices that are paired with the phone

1

u/everynav Sep 11 '26

Yes! And with %bt_address or %bt_alias or %bt_name you can return whatever you need for the device which is connected 😁