简体   繁体   English

Android蓝牙低能耗配对

[英]Android Bluetooth Low Energy Pairing

How to pair a Bluetooth Low Energy(BLE) device with Android to read encrypted data. 如何将蓝牙低功耗(BLE)设备与Android配对以读取加密数据。

Using the information in the Android BLE page , I am able to discover the device, connect to it, discover services and read un-encrypted characteristics. 使用Android BLE页面中的信息,我能够发现设备,连接设备,发现服务和读取未加密的特征。

When I try to read an encrypted characteristic (one that will cause iOS to show a popup asking to pair and then complete the read) I am getting an error code 5 , which corresponds to Insufficient Authentication . 当我尝试读取加密特性(一个会导致iOS显示弹出窗口要求配对然后完成读取)时,我收到错误代码5 ,这对应于认证不足

I am not sure how to get the device paired or how to provide the authentication information for the read to complete . 我不确定如何使设备配对或如何提供读取完成的身份验证信息

I toyed with BluetoothGattCharacteristics by trying to add descriptors, but that did not work either. 我通过尝试添加描述符来玩弄BluetoothGattCharacteristics,但这也不起作用。
Any help is appreciated! 任何帮助表示赞赏!

When you get the GATT_INSUFFICIENT_AUTHENTICATION error, the system starts the bonding process for you. 当您收到GATT_INSUFFICIENT_AUTHENTICATION错误时,系统会为您启动绑定过程。 In the example below I'm trying to enable notifications and indications on glucose monitor. 在下面的例子中,我试图在葡萄糖监测器上启用通知和指示。 First I'm enabling the notifications on Glucose Measurement characteristic which can cause the error to appear. 首先,我启用葡萄糖测量特征的通知,这可能导致错误出现。

@Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (GM_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                mCallbacks.onGlucoseMeasurementNotificationEnabled();

                if (mGlucoseMeasurementContextCharacteristic != null) {
                    enableGlucoseMeasurementContextNotification(gatt);
                } else {
                    enableRecordAccessControlPointIndication(gatt);
                }
            }

            if (GM_CONTEXT_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                mCallbacks.onGlucoseMeasurementContextNotificationEnabled();
                enableRecordAccessControlPointIndication(gatt);
            }

            if (RACP_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                mCallbacks.onRecordAccessControlPointIndicationsEnabled();
            }
        } else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
            // this is where the tricky part comes

            if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
                mCallbacks.onBondingRequired();

                // I'm starting the Broadcast Receiver that will listen for bonding process changes

                final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
                mContext.registerReceiver(mBondingBroadcastReceiver, filter);
            } else {
                // this situation happens when you try to connect for the second time to already bonded device
                // it should never happen, in my opinion
                Logger.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
                // I don't know what to do here
                // This error was found on Nexus 7 with KRT16S build of Andorid 4.4. It does not appear on Samsung S4 with Andorid 4.3.
            }
        } else {
            mCallbacks.onError(ERROR_WRITE_DESCRIPTOR, status);
        }
    };

Where the mBondingBroadcastReceiver is: mBondingBroadcastReceiver的位置是:

private BroadcastReceiver mBondingBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
        final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);

        Logger.d(TAG, "Bond state changed for: " + device.getAddress() + " new state: " + bondState + " previous: " + previousBondState);

        // skip other devices
        if (!device.getAddress().equals(mBluetoothGatt.getDevice().getAddress()))
            return;

        if (bondState == BluetoothDevice.BOND_BONDED) {
            // Continue to do what you've started before
            enableGlucoseMeasurementNotification(mBluetoothGatt);

            mContext.unregisterReceiver(this);
            mCallbacks.onBonded();
        }
    }
};

Remember to unregister the broadcast receiver when exiting the activity. 请记住在退出活动时取消注册广播接收器。 It may have not been unregistered by the receicver itself. 它可能没有被接收者本身注册。

i think new android 4.4 provide pairing method. 我认为新的android 4.4提供了配对方法。 same problem already i am facing so wait for update and hope over problem solved createBond() method . 同样的问题我已经面临所以等待更新和希望解决问题解决createBond()方法。

http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#setPairingConfirmation%28boolean%29 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#setPairingConfirmation%28boolean%29

You might need to check the Kernel smp.c file, which method of paring it invoke for paring. 您可能需要检查内核smp.c文件,哪种方法可以调用它来进行配对。 1) passkey 2)Just work or etc . 1)密钥2)正常工作等。 i guess if it will be able to invoke MIMT and passkey level of security , there will not be any authentication issue. 我想如果它能够调用MIMT和密钥安全级别,就不会有任何身份验证问题。 Make sure all flags is set to invoke the SMP passkey methods. 确保将所有标志设置为调用SMP密钥方法。 track by putting some print in smp.c file. 通过在smp.c文件中放置一些打印来跟踪。

A solution which works in ICS : with btmgmt tool in android and hooking it in encryption APIs. 在ICS中运行的解决方案:在android中使用btmgmt工具并将其挂钩在加密API中。 with passkey or any other methods. 使用密钥或任何其他方法。 it works. 有用。 You might need to add the passkey APIs in btmgmt from latest bluez code. 您可能需要从最新的bluez代码中添加btmgmt中的密钥API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM