1

After updating Cordova and the Android and iOS platforms the app does not connect anymore and I cannot figure out why? The Logcat in Android studio keeps looping the same error about the BluetoothGatt. I am noobish so I am not really sure how to debug from here besides googling and asking questions.

I tried uninstalling, updating Gradle, changed plugins for wifi , but I believe this a bluetooth error. But my beliefs are probably wrong.

The loop in the Logcat in Android studio.

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

This is the function that is throwing the errors.

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

I would just like some breadcrumbs or direction on how to solve this or how to debug properly through Android studio. Thanks

Curtis M
  • 905
  • 1
  • 8
  • 14

1 Answers1

0

This error was being thrown because I was not properly connecting to wi-fi and the code I had before I updated Cordova and the iOS and Android platform were working fine. I needed to change android:usesCleartextTraffic=true in the AndroidManifest as per the Cordova docs when upgrading API Level. Which has a good answer here.. Android 8: Cleartext HTTP traffic not permitted

and the docs: https://developer.android.com/training/articles/security-config

I hope this helps someone who upgrades their project.

Curtis M
  • 905
  • 1
  • 8
  • 14
  • 1
    I have the same problem (i.e. all of a sudden the code that was working fine on a phone is no more, and return error 257). It's not a Cordova implementation though. Just native Java. I will try to investigate around your findings, although not sure why Wi-Fi would create problems on Bluetooth. – Maurizio Macagno Aug 19 '19 at 22:34
  • @MaurizioMacagno , Yeah , I would look for an update in your platform which could have new security measures or a plugin that might have updated and requires a change in code. – Curtis M Aug 20 '19 at 14:06