簡體   English   中英

Android藍牙低能耗配對

[英]Android Bluetooth Low Energy Pairing

如何將藍牙低功耗(BLE)設備與Android配對以讀取加密數據。

使用Android BLE頁面中的信息,我能夠發現設備,連接設備,發現服務和讀取未加密的特征。

當我嘗試讀取加密特性(一個會導致iOS顯示彈出窗口要求配對然后完成讀取)時,我收到錯誤代碼5 ,這對應於認證不足

我不確定如何使設備配對或如何提供讀取完成的身份驗證信息

我通過嘗試添加描述符來玩弄BluetoothGattCharacteristics,但這也不起作用。
任何幫助表示贊賞!

當您收到GATT_INSUFFICIENT_AUTHENTICATION錯誤時,系統會為您啟動綁定過程。 在下面的例子中,我試圖在葡萄糖監測器上啟用通知和指示。 首先,我啟用葡萄糖測量特征的通知,這可能導致錯誤出現。

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

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

請記住在退出活動時取消注冊廣播接收器。 它可能沒有被接收者本身注冊。

我認為新的android 4.4提供了配對方法。 同樣的問題我已經面臨所以等待更新和希望解決問題解決createBond()方法。

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

您可能需要檢查內核smp.c文件,哪種方法可以調用它來進行配對。 1)密鑰2)正常工作等。 我想如果它能夠調用MIMT和密鑰安全級別,就不會有任何身份驗證問題。 確保將所有標志設置為調用SMP密鑰方法。 通過在smp.c文件中放置一些打印來跟蹤。

在ICS中運行的解決方案:在android中使用btmgmt工具並將其掛鈎在加密API中。 使用密鑰或任何其他方法。 有用。 您可能需要從最新的bluez代碼中添加btmgmt中的密鑰API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM