20

I'm new to Android and now doing a simple app that requires writing some data into a peripheral device.

Actually nothing goes wrong in a Samsung GT-S7272C device. But when I switch to Sony LT29i, there will always be a status 133 when I'm trying to write into a certain characteristic. I will give out some brief code.

BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_TIME_INPUT_CHAR);
if (tChar == null) throw new AssertionError("characteristic null when sync time!");

int diff = /*a int*/;
tChar.setValue(diff, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(tChar);

and the onCharacteristicWrite function:

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
    try {
        if (status != BluetoothGatt.GATT_SUCCESS) throw new AssertionError("Error on char write");
        super.onCharacteristicWrite(gatt, characteristic, status);
        if (characteristic.getUuid().equals(SYNC_TIME_INPUT_CHAR)) {
            BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
            BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR);
            if (tChar == null) throw new AssertionError("characteristic null when sync time!");
            tChar.setValue(/*another int*/, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
            gatt.writeCharacteristic(tChar);
        }
        else if {
            ...
        }
    } catch (AssertionError e) {
        ...
    }

Writing into first characteristic has nothing wrong and control will reach the onCharacteristicWrite and enter the first if statement with status 0, which means success. Problem is the second writing action in the if statement, which will also trigger onCharacteristicWrite function but yield a status 133, which cannot be found in the official site. Then the device disconnect automatically.

I've confirmed that the data type and the offset are all correct. And because in another device it works really nice, I think there might be some tiny differences of the bluetooth stack implementation between different device that I should do something more tricky to solve this problem.

I've search for result for a long time. Some results lead me to the C source code(Sorry, I will post the link below because I don't have enough reputation to post more than 2 links), but I can only find that 133 means GATT_ERROR there, which is not more helpful than just a 133. I've also found a issue in google group, discussing some familiar questions, but I failed to find a solution here.

I'm a little bit sad because, if it is something wrong with the C code, even if I can locate what's wrong, I still have no way to make it right in my own code, right?

I hope that someone has the familiar experience before and may give me some suggestions. Thanks a lot!

links:

  • C source code: https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-4.4.2_r1/stack/include/gatt_api.h

  • Issue: https://code.google.com/p/android/issues/detail?id=58381

Quarter2Twelve
  • 613
  • 1
  • 7
  • 14

5 Answers5

8

I had a similar issue when I tried to write to some characteristic I can't remember though if i got the same error code or not. (And it worked on some devices while it didn't on others).

What turned out to be the problem is the property of the characteristics and the writeType.

Because characteristics can have values set:

  • write without response OR
  • write with response

In reference to this property you have to set the writeType before writing the actual data to the characteristic.

You can set the type once you get the Characteristic but before writing to it.

BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR);
        if (tChar == null) throw new AssertionError("characteristic null when sync time!");

        // use one of them in regards of the Characteristic's property
        tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        //tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);


        tChar.setValue(/*another int*/, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
        gatt.writeCharacteristic(tChar);
benka
  • 4,732
  • 35
  • 47
  • 58
  • 1
    Thanks for your help. This is exactly what my problem is. But now I have an another similar problem, that is when reading another characteristic immediately after I successfully write something into device, I will get a `133` too. But I haven't found something like ReadType so far. Do you have any ideas? @benka – Quarter2Twelve Sep 17 '14 at 14:56
  • there are properties for reading too but thats a bit different: it's `read`, `notify` or `indicate`. Based on these properties you read from a characteristic of subscribe to an `onCharacteristicChanged` event. Check my post here: http://stackoverflow.com/a/25866874/2390075 – benka Sep 17 '14 at 15:00
  • if you need more assistance, just open a new question and post your code and a bit of the details and maybe post the link here in a comment too – benka Sep 17 '14 at 15:02
  • I've posted [a new question](http://stackoverflow.com/questions/25895347/android-bluetooth-status-133-in-gatt-readcharacteristic) of this new question. Hope it make things clearer. And thanks in advance. @benka – Quarter2Twelve Sep 17 '14 at 16:09
  • I am getting same problem. If I set write type `WRITE_TYPE_NO_RESPONSE`. I got call back of write success but it is not write value in ble device. – D.J Jan 24 '17 at 07:29
7

Here the error/success status code and meaning

  • GATT_ILLEGAL_PARAMETER 0x0087 (135)
  • GATT_NO_RESOURCES 0x0080 (128)
  • GATT_INTERNAL_ERROR 0x0081 (129)
  • GATT_WRONG_STATE 0x0082 (130)
  • GATT_DB_FULL 0x0083 (131)
  • GATT_BUSY 0x0084 (132)
  • GATT_ERROR 0x0085 (133)
  • GATT_CMD_STARTED 0x0086 (134)
  • GATT_PENDING 0x0088 (136)
  • GATT_AUTH_FAIL 0x0089 (137)
  • GATT_MORE 0x008a (138)
  • GATT_INVALID_CFG 0x008b (139)
  • GATT_SERVICE_STARTED 0x008c (140)
  • GATT_ENCRYPED_MITM GATT_SUCCESS
  • GATT_ENCRYPED_NO_MITM 0x008d (141)
  • GATT_NOT_ENCRYPTED 0x008e (142)
Odys
  • 8,951
  • 10
  • 69
  • 111
Ashraf
  • 3,114
  • 3
  • 23
  • 22
3

For those who may find this post as a result of a status 133 onCharacteristicWrite, I found that we get this 133 result in because the remote device disconnected. I lost a lot of time looking for a problem on the Android side, only to discover later that the problem was on the other side.

From this I gather that status = 133 seems to be some sort of undocumented generic cause for error.

Bamaco
  • 592
  • 9
  • 25
  • 1
    FOR INSTANCE ? WHAT DOES 133 MEANS ACTUALLY – Jeff Bootsholz Nov 03 '15 at 07:33
  • 1
    Sorry, seems undocumented. I have read somewhere it is possible to access source code for this, and there code 133 means "unknown error" used whenever something does not fit one of the existing error codes. You will have to google to find this. – Bamaco Nov 04 '15 at 17:30
  • 1
    Here's a link to the source: https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/master/stack/include/gatt_api.h – Luis Apr 19 '17 at 14:14
2

In my case, I needed to reuse the active Gatt connection in a new activity but wasn't able to and was constantly disconnecting with error 133. So, I resorted to calling BluetoothGatt.close() before startActivity(), and (re)connect in onStart(). If anyone has a better idea on how to keep the connection, please post.

D.J
  • 1,439
  • 1
  • 12
  • 23
rexxar
  • 1,671
  • 1
  • 21
  • 27
  • 1
    yes, it did, because it was killing the connection and reconnecting every time. I believe that that's a battery killer, but I couldn't get it to work as intended. – rexxar May 02 '17 at 09:58
0

I had the same issue with error 133 and I had to use the reliable write to make it work (sorry for the Kotlin code instead of Java):

fun BluetoothGatt.myWrite(characteristic: BluetoothGattCharacteristic, payload: ByteArray) {
  
  // Set write type
  characteristic.writeType =  when {
    characteristic.containsProperty(BluetoothGattCharacteristic.PROPERTY_WRITE) -> BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
    characteristic.containsProperty(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) -> BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
    else -> error("Characteristic ${characteristic.uuid} cannot be written to")
  }
  
  // set the payload
  characteristic.setValue(payload)

  // write
  this.beginReliableWrite()
  this.writeCharacteristic(characteristic)
  this.executeReliableWrite()

}

with the use of the extension function:

fun BluetoothGattCharacteristic.containsProperty(property: Int): Boolean {
  return properties and property != 0
}
Simone
  • 1,418
  • 15
  • 22