How I figure it to Java function?

137 views
Skip to first unread message

Jay K

unread,
Jul 16, 2015, 3:18:04 AM7/16/15
to tas...@googlegroups.com
I'd like to know the connected bluetooth device's mac and name.
So, I found the java source below. But, I don't know how I figure it to Java function in Tasker.
Thanks in advance.

public void checkConnected(Context $context)
{
  // true == headset connected && connected headset is support hands free
  int state = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(BluetoothProfile.HEADSET);
  if (state != BluetoothProfile.STATE_CONNECTED)
    return;
 
  try
  {
    BluetoothAdapter.getDefaultAdapter().getProfileProxy($context, serviceListener, BluetoothProfile.HEADSET);
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}
 
private ServiceListener serviceListener = new ServiceListener()
{
  @Override
  public void onServiceDisconnected(int profile)
  {
 
  }
 
  @Override
  public void onServiceConnected(int profile, BluetoothProfile proxy)
  {
    for (BluetoothDevice device : proxy.getConnectedDevices())
    {
      Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
          + BluetoothProfile.STATE_CONNECTED + ")");
    }
 
    BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
  }
};



Plepleus

unread,
Aug 6, 2015, 9:32:44 AM8/6/15
to Tasker
I think the best way to do this is to setup a profile that triggers a task whenever a Bluetooth device is connected.  Event>System>Intent Received.  Within the edit screen you just need to set the first field (Action) to "android.bluetooth.device.action.ACL_CONNECTED" (without the quotation marks).  Then you can trigger the following task:

A1: Java Function [ Return:bm Class Or Object:BluetoothManager Function:new
{BluetoothManager} (Context) Param:CONTEXT Param: Param: Param: Param: Param: Param: ] 
A2: Java Function [ Return:ba Class Or Object:bm Function:getAdapter
{BluetoothAdapter} () Param: Param: Param: Param: Param: Param: Param: ] 
A3: Java Function [ Return:bd Class Or Object:ba Function:getRemoteDevice
{BluetoothDevice} (String) Param:%android_bluetooth_device_extra_device Param: Param: Param: Param: Param: Param: ] 
A4: Java Function [ Return:%name Class Or Object:bd Function:getName
{String} () Param: Param: Param: Param: Param: Param: Param: ] 
A5: Java Function [ Return:%address Class Or Object:bd Function:getAddress
{String} () Param: Param: Param: Param: Param: Param: Param: ] 

Then you'll have %name and %address as local variables with the name and hardware address of the connected device.

Jay K

unread,
Aug 7, 2015, 10:27:55 AM8/7/15
to Tasker
But it works when only the event time not in the state.
I want to find better way to achieve them, but it seems to be difficult.
Thanks any way, Plepleus.

Plepleus

unread,
Aug 8, 2015, 4:36:02 PM8/8/15
to Tasker
Well couldn't you have an Profile for android.bluetooth.device.action.ACL_CONNECTED as above and set a global variable for the MAC address and/or name and then another Profile for android.bluetooth.device.action.ACL_DISCONNECTED that clears these variables then you could use your state on these variables?  If this isn't what you're looking for, maybe a little more information on what you're trying to accomplish with this will be helpful

plepleus

Jay K

unread,
Aug 8, 2015, 9:23:46 PM8/8/15
to Tasker
Well couldn't you have an Profile for android.bluetooth.device.action.ACL_CONNECTED as above and set a global variable for the MAC address and/or name and then another Profile for android.bluetooth.device.action.ACL_DISCONNECTED that clears these variables then you could use your state on these variables?  If this isn't what you're looking for, maybe a little more information on what you're trying to accomplish with this will be helpful

You're right and it is suggested by Ryan also.
But, it is available only after Tasker start monitor.
Thanks.
Reply all
Reply to author
Forward
0 new messages