1

In the past 6 months, I am working on the BLE applications. In my recent project, I am integrating a custom BLE device, and it's making a synchronization issue while reading data. The "onCharacteristicRead" always return 133 error code. Please go through the complete scenario and if anyone has any solution please help me.

Scenario The custom BLE device has three services available.

  1. Emergency

  2. Catch All

  3. Battery Level

I required only two services in my application. Battery and Emergency. Initially, I will scan the device using the below code,

 private void scan() {
        if (isLollyPopOrAbove()) {// Start Scanning For Lollipop devices
            mBluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters(), scanSettings(), scanCallback); // Start BLE device Scanning in a separate thread
        } else {
            mBluetoothAdapter.startLeScan(mLeScanCallback); // Start Scanning for Below Lollipop device
        }
    }




   private List<ScanFilter> scanFilters() {
        String emergencyUDID = "3C02E2A5-BB87-4BE0-ADA5-526EF4C14AAA";
        ScanFilter filter = new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(emergencyUDID)).build();
        List<ScanFilter> list = new ArrayList<ScanFilter>(1);
        list.add(filter);
        return list;
    }



   private ScanSettings scanSettings() {
        ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
        return settings;
    }

This is working fine and I am getting the scan result and I can connect to the device. Once the connection establish, I will make the below call from GattCallBack

mBluetoothGatt.discoverServices();

Then I am getting the service discovery completed callback

   @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {              //BLE service discovery complete
            if (status == BluetoothGatt.GATT_SUCCESS) {                                 //See if the service discovery was successful
                Log.i(TAG, "**ACTION_SERVICE_DISCOVERED**" + status);
                broadcastUpdate(BLEConstants.ACTION_GATT_SERVICES_DISCOVERED);                       //Go broadcast an intent to say we have discovered services
            } else {                                                                      //Service discovery failed so log a warning
                Log.i(TAG, "onServicesDiscovered received: " + status);
            }
        }

From here I find the Available MLDP services using the provided UUID's. This is also working fine.

But when I read characteristics,

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {                      //Check that we have access to a Bluetooth radio
            return;
        }
        boolean status = mBluetoothGatt.readCharacteristic(characteristic);                              //Request the BluetoothGatt to Read the characteristic
        Log.i(TAG, "READ STATUS " + status);
    }

The response status is getting true but "onCharacteristicRead" callback always getting as 133

 @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //A request to Read has completed
            //String value = characteristic.getStringValue(0);
            //int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
//            if(characteristic.getUuid().e)
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    //See if the read was successful
                    Log.i(TAG, "**ACTION_DATA_READ**" + characteristic);
                    broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic);                 //Go broadcast an intent with the characteristic data
                } else {
                    Log.i(TAG, "ACTION_DATA_READ: Error" + status);
                }


        }
Nithinjith
  • 1,775
  • 18
  • 40

2 Answers2

3

Finally, I have found a solution to handle 133 error. During the deep analysis, I have found that the BLE request response process has some delay. So I put a 500-millisecond delay for each read and write. Now it's working perfectly. I have tested it on various devices and its working. I don't know if it is a workaround. But this may be helpful for the developers facing the similar issue.

Nithinjith
  • 1,775
  • 18
  • 40
  • i faced the same problem and then i used delay too... but its not a good solution because callbacks could be take more then 500ms when your distance is more. Best solution is to depend on Read/Write callbacks. – Salman Naseem Feb 13 '18 at 17:35
  • I am catching the read-write callbacks(onCharacteristicRead() and onCharacteristicWrite()). But the problem here is, I am getting GATT error in read-write callback without delay. I don't think there is an available solution for this issue. – Nithinjith Feb 14 '18 at 08:57
  • That error occur when you Write multiple values or Read multiple values right? If yes, then i have a solution for this. – Salman Naseem Feb 14 '18 at 09:15
  • Yes, I am reading Some "Connection Service" first. In the read callback, I am writing the App status to the same service. In the write callback, I am reading "Emergency Service" status and write the app status to the read callback. Finally, I am reading battery. Each read-write, I required some delay. – Nithinjith Feb 14 '18 at 09:27
  • For multiple reads try [this](https://stackoverflow.com/questions/48638870/how-can-read-all-characteristics-value-of-my-ble-device/48643768#48643768) solution. For multiple Writes ask a question and i'll post answer there. – Salman Naseem Feb 14 '18 at 09:35
  • Thank you for your update. But this solution is not applicable to my case. In my case, I know all the Service and characteristics available in the Gatt. Also, I am doing a chaining process. Catch the callback of the first read and write the status to BLE. Then from write callback again reading the second and so on. – Nithinjith Feb 19 '18 at 11:23
1

error code: 133 is indicate GATT__ERROR, device is not in range. May be there is bug in during read event. I suggest please check from the device side first.

One que: this device need bonding for the connection?

Joshi Tushar
  • 116
  • 9