6

In my app I need pairing bluetooth device and immediately connect with it.

I have the following function in order to pairing devices:

public boolean createBond(BluetoothDevice btDevice)
{
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = btDevice.getClass().getMethod("createBond", (Class[]) null);
        Boolean returnValue = (Boolean) m.invoke(btDevice, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
        return returnValue;

    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
    return false;
}

And I use it as the following way:

Boolean isBonded = false;
try {
    isBonded = createBond(bdDevice);
    if(isBonded)
    {
         //Connect with device
    }
}

And it show me the dialog to pairing devices and enter the pin.

The problem is that createBond functions always return true, and it doen's wait until I enter the pin and paired with device, so I don't use correctly:

isBonded = createBond(bdDevice);
if(isBonded) {...}

So the question is How can I paired with device and when it is paired connect to it?

P.D My code is based in the first answer of the following thread: Android + Pair devices via bluetooth programmatically

Community
  • 1
  • 1
RdlP
  • 1,366
  • 5
  • 25
  • 45

1 Answers1

4

I found the solution.

First I need a BroadcastReceiver like:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // CONNECT
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Discover new device
        }
    }
};

And then I need register the receiver as follow:

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
context.registerReceiver(myReceiver, intentFilter);

In this way the receiver is listening for ACTION_FOUND (Discover new device) and ACTION_BOND_STATE_CHANGED (Device change its bond state), then I check if the new state is BOND_BOUNDED and if it is I connect with device.

Now when I call createBond Method (described in the question) and enter the pin, ACTION_BOND_STATE_CHANGED will fire and device.getBondState() == BluetoothDevice.BOND_BONDED will be True and it will connect.

RdlP
  • 1,366
  • 5
  • 25
  • 45
  • 1
    What methods did you use to programmatically connect the bluetooth device? I understand how to programmatically pair, but it's difficult to connect in a broadcast receiver. – waylonion Mar 02 '15 at 02:40
  • for a SPP conection I use the following lines: ``BluetoothDevice device = adapter.getRemoteDevice(mac);`` ``final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");`` ``socket = device.createRfcommSocketToServiceRecord(uuid);`` ``socket.connect();`` ``out = socket.getOutputStream();`` ``in = socket.getInputStream();`` Where ``adapter`` is an instance of ``BluetoothAdapter`` and ``mac`` is a String. – RdlP Mar 02 '15 at 07:29
  • why not directly to use mmSocket.connect() in order to connect and communicate with another device? – LiangWang May 16 '15 at 11:45
  • First, I need to create Socket object, I use line 1,2 and 3 to create socket object and then I use `socket.connect();` – RdlP May 16 '15 at 15:12