Android的,我怎么能做出BLE装置配对设备(保税) [英] Android, How can I make BLE device to paired device (bonded)

查看:1746
本文介绍了Android的,我怎么能做出BLE装置配对设备(保税)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关贸总协定之前, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecord

Before GATT, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecord

方法可以使配对设备,

但关贸总协定还没有选件的配对设备, 只使用BluetoothDevice.connectGatt(...)

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

我想打一个配对的设备是否已连接。

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

THX。

推荐答案

据我所知,开始在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...
            }
        }
    }
};

这篇关于Android的,我怎么能做出BLE装置配对设备(保税)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆