Android,如何使 BLE 设备与配对设备(绑定)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24645519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:32:36  来源:igfitidea点击:

Android, How can I make BLE device to paired device (bonded)

androidbluetooth-lowenergygatt

提问by user3563211

Before GATT, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecord

在 GATT 之前,createRfcommSocketToServiceRecord,createInsecureRfcommSocketToServiceRecord

methods can make paired device,

方法可以使配对设备,

but GATT has no option about paired device, only use BluetoothDevice.connectGatt(...)

但是 GATT 没有关于配对设备的选项,只能使用 BluetoothDevice.connectGatt(...)

I want to make a paired device if it's connected already.

如果已经连接,我想制作一个配对设备。

thx.

谢谢。

回答by Programonks

As far as I know, to initiate a pairing procedure in BLE there are two ways:

据我所知,要在 BLE 中启动配对程序,有两种方法:

1) From API 19 and up you can start the pairing by calling the mBluetoothDevice.createBond(). You don't need to be connected with the remote BLE device to start the pairing process.

1) 从 API 19 开始,您可以通过调用mBluetoothDevice.createBond(). 您无需连接远程 BLE 设备即可开始配对过程。

2) When you try to do a Gatt operation, let's take for example the method

2)当你尝试做Gatt操作时,让我们以方法为例

mBluetoothGatt.readCharacteristic(characteristic)

If the remote BLE device needs to be bonded to do any communication then when the callback

如果需要绑定远程 BLE 设备以进行任何通信,则当回调

onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

gets called its statusparameter value will be equal to either GATT_INSUFFICIENT_AUTHENTICATIONor GATT_INSUFFICIENT_ENCRYPTION, and not equal to GATT_SUCCESS. If this happens then the pairing procedure will start automatically.

被调用,其status参数值将等于GATT_INSUFFICIENT_AUTHENTICATIONGATT_INSUFFICIENT_ENCRYPTION,而不等于GATT_SUCCESS。如果发生这种情况,配对过程将自动开始。

Here is an example to find out when it fails once the onCharacteristicReadcallback gets called

这是一个示例,用于确定一旦onCharacteristicRead调用回调何时失败

@Override
public void onCharacteristicRead(
        BluetoothGatt gatt,
        BluetoothGattCharacteristic characteristic,
        int status)
{

    if(BluetoothGatt.GATT_SUCCESS == status)
    {
        // characteristic was read successful
    }
    else if(BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION == status ||
            BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION == status)
    {
        /*
         * failed to complete the operation because of encryption issues,
         * this means we need to bond with the device
         */

        /*
         * registering Bluetooth BroadcastReceiver to be notified
         * for any bonding messages
         */
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mActivity.registerReceiver(mReceiver, filter);
    }
    else
    {
        // operation failed for some other reason
    }
}

Other people mentioning that this operation starts the pairing procedure automatically: Android Bluetooth Low Energy Pairing

其他人提到此操作会自动启动配对程序: Android Bluetooth Low Energy Pairing

And this is how the receiver can be implemented

这就是接收器的实现方式

private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        final String action = intent.getAction();

        if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
        {
            final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);

            switch(state){
                case BluetoothDevice.BOND_BONDING:
                    // Bonding...
                    break;

                case BluetoothDevice.BOND_BONDED:
                    // Bonded...
                    mActivity.unregisterReceiver(mReceiver);
                    break;

                case BluetoothDevice.BOND_NONE:
                    // Not bonded...
                    break;
            }
        }
    }
};