模拟单击确定按钮android

时间:2014-07-22 13:32:54

标签: android bluetooth

我收到蓝牙配对请求,我只需按下确定按钮。我想通过代码来做到这一点。我该怎么办?我可以在ACTION_BOND_STATE_CHANGED事件中做到这一点吗?

如果我应该使用.performClick(),我怎样才能从配对蓝牙对话框中获得对ok按钮的引用?

到目前为止,我在onReceive功能上有一个BroadCast接收器:

if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
            int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Log.i(TAG, "bond state changed");
            Log.i(TAG, "device:" + device.getName());
            Log.i(TAG, "prev state:" + prevBondState);
            Log.i(TAG, "curr state:" + bondState);
            if (prevBondState == BluetoothDevice.BOND_BONDING) {

                if (bondState == BluetoothDevice.BOND_BONDED) {
                    Globals.sendStatus("bluetooth", device.getName() + " pairing successful");
                    Log.i(TAG, device.getName() + " pairing successful");
                }
            } else if (prevBondState == BluetoothDevice.BOND_BONDED) {
                if (bondState == BluetoothDevice.BOND_NONE) {
                    Log.i(TAG, device.getName() + " unpairing successful");
                    Globals.sendStatus("bluetooth", device.getName() + " unpaired");
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

您可以尝试使用performClick()方法。这将触发与按钮关联的onClickListener

myButton.performClick();