简体   繁体   中英

Reaction to Bluetooth Pairing dialog input

I am working on an App that connects the android device with another device (CAN modules) over Bluetooth.

I pair previously unpaired devices like this:

Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);

Which works like a charm.

There is an issue though. The CAN Modules are set up in a way that you don't need the pin stuff or any other form of pairing confirmation, you just say that you want to pair with the device and it'll do so. Now, what happens if my App tries to connect to a Bluetooth device that is not a CAN Module, like a phone for example?

In that case, a Dialog appears asking the User to confirm the pairing. I don't mind the dialog, BUT I would like to react to the "Cancel" button in some way.

To sum it up: I want to call method doSomething() when the User presses Cancel on the Bluetooth Pairing Confirmation Dialog. Is this possible?

You should listen to ACTION_BOND_STATE_CHANGED Intent(I hope you know how to register BroadcastReceiver and use them).

Above action broadcast by system(BluetoothService) it also contains Current Bond State and Previous Bond State .

There are three Bond States.

BOND_BONDED Indicates the remote device is bonded (paired).

BOND_BONDING Indicates bonding (pairing) is in progress with the remote device.

BOND_NONE Indicates the remote device is not bonded (paired).

In your case you will receive BOND_BONDING >> BOND_NONE in case of cancel button on PassKey dialog, and BOND_BONDING >> BOND_BONDED in case of Pair button on PassKey dialog

I found a solution/workaround with this Question .

To react to a the User cancelling the Pairing Request, we need to look for the following action: BluetoothDevice.ACTION_BOND_STATE_CHANGED and the Bonding state of the device through EXTRA_BOND_STATE

here's an example:

private void pairDevice(BluetoothDevice device){
        try{
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            ctx.registerReceiver(receiver, filter);

            Method m = device.getClass().getMethod("createBond", (Class[]) null);
            m.invoke(device, (Object[]) null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

In your BroadcastReceiver

public void onReceive(Context context, Intent intent){
    String action = intent.getAction();
    if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED){
        int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

        if(state < 0){
            //we should never get here
        }
        else if(state == BluetoothDevice.BOND_BONDING){
            //bonding process is still working
            //essentially this means that the Confirmation Dialog is still visible
        }
        else if(state == BluetoothDevice.BOND_BONDED){
            //bonding process was successful
            //also means that the user pressed OK on the Dialog
        }
        else if(state == BluetoothDevice.BOND_NONE){
            //bonding process failed
            //which also means that the user pressed CANCEL on the Dialog
            doSomething(); //we can finally call the method
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM