1

Goal: open a bluetooth socket to a currently connected A2DP device, when I know it is connected.

In order to connect the socket, I need the MAC address of this device.

As i currently understand, there is 2 kinds of listener to get the bluetooth state:

1- BluetoothAdapter.getProfileProxy() as described here How to get bluetooth connected devices using BluetoothHeadset API

  • triggers when bluetooth card is ON / OFF.
  • allow to get a specific bluetoothProfile (A2DP for example), then the list of the connected devices to this profile, then the mac address of this connected devices.
  • the device list is ever empty when "onServiceConnected" is called (devices need more time to connected after the bluetooth card startup). So I need to call it after the device connection.

2- A broadcast receiver on BluetoothDevice.ACTION_ACL_CONNECTED as described here How to programmatically tell if a Bluetooth device is connected? (Android 2.2)

  • triggers when bluetooth device is connected / disconnected.
  • do not allow to get any information of this device

So I need to use both of them to trigger when A2DP device is connected... and so get the MAC address of this device (then open the btSocket): Start the btAdapter.getProfileProxy inside the BroadcastReceiver "BluetoothDevice.ACTION_ACL_CONNECTED", but with a 3 seconds delay to allow it to connect.

It is the only (complicated) way to do that?

    onCreate{
    context.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
    context.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, "Action received\n");
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
        {
            Log.d(TAG, "ACTION_ACL_CONNECTED received\n");

            timerHandler.postDelayed(timerRunnableBtConnection, 3000);
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
        {
            Log.d(TAG, "ACTION_ACL_DISCONNECTED received\n");
            btAdapter.closeProfileProxy(BluetoothProfile.A2DP, btA2dp);
        }
    }
    private Runnable timerRunnableBtConnection = new Runnable() {
        @Override
        public void run() {
            btAdapter.getProfileProxy(intelloApplication.getAppContext(), mProfileListener, BluetoothProfile.A2DP);
        }
    };
};

// Define Service Listener of BluetoothProfile
// then create the btSocket on it
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.A2DP) {
            Log.d(TAG, "Bluetooth connected\n");
            btA2dp = (BluetoothA2dp) proxy;
            List<BluetoothDevice> devices = btA2dp.getConnectedDevices();
            Log.d(TAG, "List of " + devices.size() + " device\n");
            for ( final BluetoothDevice dev : devices ) {
                String address = dev.getAddress();
                Log.d(TAG, "Name: " + dev.getName() + " and address: " + address + "\n");
                    btDevice = btAdapter.getRemoteDevice(address);

                    try {
                        btSocket = createBluetoothSocket(btDevice);
                    } catch (IOException e) {
                        Log.d(TAG, e.getMessage());
                    }

                    Log.d(TAG, "...Connecting...");
                    try {
                        btSocket.connect();
                        useBluetooth = true;
                        Log.d(TAG, "....Connection ok...");
                    } catch (IOException e) {
                        try {
                            btSocket.close();
                            useBluetooth = false;
                            Log.d(TAG, "....Close connection.");
                        } catch (IOException e2) {
                            Log.d(TAG, "Unable to close socket during connection failure");
                            Log.d(TAG, e2.getMessage());
                        }
                    }

                    // Create a data stream so we can talk to server.
                    Log.d(TAG, "...Create Socket...");

                    mConnectedThread = new ConnectedThread(btSocket);
                    mConnectedThread.start();
                }
            }
        }
    }

    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.A2DP) {
            Log.d(TAG, "Bluetooth disconnected\n");
        }
    }
};
Community
  • 1
  • 1
Ben.
  • 45
  • 6

0 Answers0