简体   繁体   English

对“蓝牙配对”对话框输入的反应

[英]Reaction to Bluetooth Pairing dialog input

I am working on an App that connects the android device with another device (CAN modules) over Bluetooth. 我正在使用通过蓝牙将android设备与另一设备(CAN模块)连接的应用程序。

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. CAN模块的设置方式不需要引脚,也不需要任何其他形式的配对确认,您只需要说要与设备配对就可以了。 Now, what happens if my App tries to connect to a Bluetooth device that is not a CAN Module, like a phone for example? 现在,如果我的应用程序尝试连接到非CAN模块的蓝牙设备(例如电话),会发生什么?

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. 总结一下:当用户在“蓝牙配对确认”对话框上按“ Cancel时,我想调用doSomething()方法。 Is this possible? 这可能吗?

You should listen to ACTION_BOND_STATE_CHANGED Intent(I hope you know how to register BroadcastReceiver and use them). 您应该听ACTION_BOND_STATE_CHANGED Intent(希望您知道如何注册BroadcastReceiver并使用它们)。

Above action broadcast by system(BluetoothService) it also contains Current Bond State and Previous Bond State . 在由system(BluetoothService)广播的动作之上,它还包含Current Bond StatePrevious Bond State

There are three Bond States. 有三个邦联国。

BOND_BONDED Indicates the remote device is bonded (paired). BOND_BONDED指示远程设备已绑定(配对)。

BOND_BONDING Indicates bonding (pairing) is in progress with the remote device. BOND_BONDING表示正在与远程设备进行绑定(配对)。

BOND_NONE Indicates the remote device is not bonded (paired). BOND_NONE指示远程设备未绑定(配对)。

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 在您的情况下,如果在PassKey对话框上BOND_BONDING >> BOND_BONDED取消”按钮,您将收到BOND_BONDING >> BOND_NONE ;如果在PassKey对话框上使用“配对”按钮,您将收到BOND_BONDING >> BOND_NONE

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 要对用户取消配对请求做出反应,我们需要寻找以下操作:BluetoothDevice.ACTION_BOND_STATE_CHANGED和设备通过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 在您的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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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