【问题标题】:Android, How can I make BLE device to paired device (bonded)Android,如何使 BLE 设备成为配对设备(绑定)
【发布时间】:2014-08-29 23:55:55
【问题描述】:

在关贸总协定之前, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecord

方法可以配对设备,

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

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

谢谢。

【问题讨论】:

    标签: android bluetooth-lowenergy gatt


    【解决方案1】:

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

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

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

    mBluetoothGatt.readCharacteristic(characteristic)
    

    如果需要绑定远程BLE设备进行任何通信,那么回调时

    onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

    调用它的status 参数值将等于GATT_INSUFFICIENT_AUTHENTICATIONGATT_INSUFFICIENT_ENCRYPTION,而不等于GATT_SUCCESS。如果发生这种情况,则配对过程将自动启动。

    这是一个示例,用于找出调用 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
        }
    }
    

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

    这就是接收器的实现方式

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

    【讨论】:

    • 这个答案对我帮助很大!我在使用 Android 6.0+ 时遇到了用户甚至看不到的配对通知问题(旧版本的 Android 提供了很好的对话框,我希望在所有情况下都这样做)。我所做的与您不同的一件事是,如果 device.getBondState() == BluetoothDevice.BOND_NONE,则从一开始就调用 createBond()(即甚至在连接到设备之前)。这是因为我在读取特征时没有得到“GATT_INSUFFICIENT_AUTHENTICATION”;操作系统会在发送通知以配对设备时保留它。
    • 如果我的 onCharacteristicRead() 永远不会被调用怎么办?
    • @IgorGanapolsky 如果您 100% 确定您正在调用此方法 mBluetoothGatt.readCharacteristic(characteristic) 则将调用回调,除非远程设备可能存在与代码相关的问题或可能连接丢失(如果是这种情况,你应该得到一个不同的回调,但我现在不记得名字了)。如果有人知道不同,请告诉我们。
    • @Kyriakos createBond() 方法是否仍然需要,或者我们可以直接调用readCharacteristic() 吗?
    • @IgorGanapolsky 我认为这实际上取决于您尝试连接的远程 BLE 设备。如果该设备需要您的连接来检索数据,那么我假设您需要致电createBond(),否则您可以直接致电readCharacteristic()。我对此并不完全确定,如果有人知道不同,请告诉我们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多