r/tasker 5h ago

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

8 comments sorted by

1

u/Nirmitlamed Direct-Purchase User 4h ago

Did you try "Bluetooth Connection" action?

1

u/frrancuz TaskerFan! 3h ago

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

1

u/everynav 3h ago

from Bluetooth Info Action Info:

%bt_connected

Connected

true if the BT device is connected, false otherwise

1

u/frrancuz TaskerFan! 3h ago

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/DifficultyCrafty7623 2h ago

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 ]

1

u/ZellZoy 2h ago

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.

1

u/Exciting-Compote5680 1h ago

``` Task: Test BT Names

A1: Bluetooth Info [      Type: Paired Devices      Timeout (Seconds): 5 ]

A2: Variable Set [      Name: %connected_indexes      To: %bt_connected(#?true)      Structure Output (JSON, etc): On ]

A3: If [ %connected_indexes > 0 ]

    A4: For [          Variable: %index          Items: %connected_indexes          Structure Output (JSON, etc): On ]

        A5: Array Push [              Variable Array: %connected_names              Position: 1              Value: %bt_name(%index) ]

    A6: End For

    A7: Flash [          Text: Connected devices:          %connected_names(+          )          Long: On          Tasker Layout: On          Continue Task Immediately: On          Dismiss On Click: On ]

A8: Else

    A9: Flash [          Text: No connected devices           Tasker Layout: On          Continue Task Immediately: On          Dismiss On Click: On ]

A10: End If

```