且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的,我怎么能做出BLE装置配对设备(保税)

更新时间:2023-01-24 08:06:18

据我所知,开始在BLE的配对过程有两种方式:

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

1)从API 19,你就可以通过调用的 mBluetoothDevice.createBond() 。你不必与远程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.

2)当你尝试做关贸总协定操作,让我们举个例子方法

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

mBluetoothGatt.readCharacteristic(characteristic)

如果远程BLE设备需要被结合到做任何通信那么当回调

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

onCharacteristicRead(                 BluetoothGatt关贸总协定                 BluetoothGattCharacteristic特点,                 INT状态)

onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

被调用它的状态参数值将等于为 GATT_INSUFFICIENT_AUTHENTICATION GATT_INSUFFICIENT_ENCRYPTION 和不等于 GATT_SUCCESS 。如果发生这种情况,然后配对过程中会自动启动。

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

下面是一个例子,找出失败时一旦 onCharacteristicRead 回调被调用

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

@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
    }
}

其他人提的是,该操作将自动开始配对过程: Android的蓝牙低耗能配对

Other people mentioning that this operation starts the pairing procedure automatically: 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);

            if(state == BluetoothDevice.BOND_BONDING)
            {
                // Bonding...
            }
            else if(state == BluetoothDevice.BOND_BONDED)
            {
                // Bonded...
                mActivity.unregisterReceiver(mReceiver);

            }
            else if(state == BluetoothDevice.BOND_NONE)
            {
                // Not bonded...
            }
        }
    }
};