首页 > 解决方案 > BluetoothGatt: onClientRegistered() - status=133 // onConnectionStateChange 状态:257

问题描述

更新 Cordova 以及 Android 和 iOS 平台后,应用程序不再连接,我不知道为什么?Android Studio 中的 Logcat 不断循环有关 BluetoothGatt 的相同错误。我很笨,所以除了谷歌搜索和提问之外,我不确定如何从这里进行调试。

我尝试卸载、更新 Gradle、更改 wifi 插件,但我认为这是蓝牙错误。但我的信念可能是错误的。

Android Studio 中 Logcat 中的循环。

D/BluetoothAdapter: STATE_ON
    scan not started yet
I/@@@@@@: @@@ connect
I/@@@@@@: @@@ creating gatt handler
    @@@ getRemoteDevice
    @@@ connectGatt
D/BluetoothAdapter: STATE_ON
D/BluetoothGatt: connect() - device: B8:27:EB:C6:4F:19, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
    registerApp() - UUID=ae27921d-3fa8-45f4-bf37-b1d9abf04149
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0
I/@@@@@@: @@@ onConnectionStateChange status: 257 newState: 0
    @@@ connect error - status: 257
D/BluetoothAdapter: stopLeScan()
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: scan not started yet
I/@@@@@@: @@@ connect
I/@@@@@@: @@@ creating gatt handler
    @@@ getRemoteDevice
    @@@ connectGatt
D/BluetoothAdapter: STATE_ON
D/BluetoothGatt: connect() - device: B8:27:EB:C6:4F:19, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
    registerApp() - UUID=33e7344c-33e8-4366-8bd9-9336b80f4ce7
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0
I/@@@@@@: @@@ onConnectionStateChange status: 257 newState: 0
    @@@ connect error - status: 257
D/BluetoothAdapter: stopLeScan()
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: scan not started yet

这是引发错误的函数。

    // Also maintains the per-device operation queue.
    private class GattHandler extends BluetoothGattCallback
    {
        // Local copy of the key to BLE.mGatt. Fed by BLE.mNextGattHandle.
        final int mHandle;

        // The queue of operations.
        LinkedList<Runnable> mOperations = new LinkedList<Runnable>();

        // connect() and rssi() are handled separately from other operations.
        CallbackContext mConnectContext;
        CallbackContext mRssiContext;
        CallbackContext mCurrentOpContext;

        // Flag used when writing notification config descriptor.
        // In this case we don't want to send back the result to JavaScript.
        boolean mDontReportWriteDescriptor = false;

        // The Android API connection.
        BluetoothGatt mGatt;

        // Maps of integer to Gatt subobject.
        HashMap<Integer, BluetoothGattService> mServices;
        HashMap<Integer, BluetoothGattCharacteristic> mCharacteristics;
        HashMap<Integer, BluetoothGattDescriptor> mDescriptors;

        // Monotonically incrementing key to the subobject maps.
        int mNextHandle = 1;

        // Notification callbacks. The BluetoothGattCharacteristic object, as found
        // in the mCharacteristics map, is the key.
        HashMap<BluetoothGattCharacteristic, CallbackContext> mNotifications =
            new HashMap<BluetoothGattCharacteristic, CallbackContext>();

        GattHandler(int h, CallbackContext cc)
        {
            mHandle = h;
            mConnectContext = cc;
        }

        // Run the next operation, if any.
        // TODO: Make another method processNext that sets mCurrentOpContext to
        // null and calls process. That would clean up repeated code a bit.
        // Also consider writing method that adds a runnable to the mOperations
        // queue and calls process, this would also reduce some repeated code.
        void process()
        {
            if (mCurrentOpContext != null) return;
            Runnable runnable = mOperations.poll();
            if (runnable == null) return;
            runAction(runnable);
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
        {
            Log.i("@@@@@@", "@@@ onConnectionStateChange status: " + status + " newState: " + newState);

            if (status == BluetoothGatt.GATT_SUCCESS)
            {
                try
                {
                    JSONObject result = new JSONObject();
                    result.put("deviceHandle", mHandle);
                    result.put("state", newState);
                    Log.i("@@@@@@", "@@@ connect success");
                    keepCallback(mConnectContext, result);
                }
                catch(JSONException e)
                {
                    Log.i("@@@@@@", "@@@ connect error: " + e);
                    e.printStackTrace();
                    mConnectContext.error("Connect error: " + e);
                    //assert(false);
                }
            }
            else
            {
                // Could this be where we get 133? Yes it is.
                Log.i("@@@@@@", "@@@ connect error - status: " + status);
                mConnectContext.error(status);
            }
        }

我只是想要一些关于如何解决这个问题或如何通过 Android Studio 正确调试的面包屑或方向。谢谢

标签: androidbluetoothraspberry-pibluetooth-gatt

解决方案


抛出此错误是因为我没有正确连接到 wi-fi,并且我在更新 Cordova 之前拥有的代码以及 iOS 和 Android 平台工作正常。升级 API 级别时,我需要根据 Cordova 文档更改 AndroidManifest 中的 android:usesCleartextTraffic=true 。这里有一个很好的答案.. Android 8: Cleartext HTTP traffic not allowed

和文档: https ://developer.android.com/training/articles/security-config

我希望这对升级项目的人有所帮助。


推荐阅读