java Android 蓝牙状态 133 onCharacteristicwrite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25888817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:53:07  来源:igfitidea点击:

Android Bluetooth status 133 in onCharacteristicwrite

javaandroidbluetoothbluetooth-lowenergy

提问by Quarter2Twelve

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

我是 Android 新手,现在正在做一个简单的应用程序,需要将一些数据写入外围设备。

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.

实际上,三星 GT-S7272C 设备没有任何问题。但是当我切换到 Sony LT29i 时,当我尝试写入某个特性时,总会出现状态 133。我会给出一些简短的代码。

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:

和 onCharacteristicWrite 函数:

@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 ifstatement with status 0, which means success. Problem is the second writing action in the ifstatement, which will also trigger onCharacteristicWrite function but yield a status 133, which cannot be found in the official site. Then the device disconnect automatically.

写入第一个特性没有错,控制会到达 onCharacteristicWrite 并输入if带有 status的第一条语句0,这意味着成功。问题是if语句中的第二个写入动作,也会触发 onCharacteristicWrite 函数,但会产生一个 status 133,在官网找不到。然后设备自动断开连接。

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 133means 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.

我已经搜索了很长时间的结果。一些结果让我找到了 C 源代码(抱歉,我会发布下面的链接,因为我没有足够的声誉发布 2 个以上的链接),但我只能在133那里找到这意味着 GATT_ERROR,这并不比只是一个133. 我也在google group里发现了一个issue,讨论了一些熟悉的问题,但是在这里没找到解决办法。

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?

我有点难过,因为如果是C代码出了问题,即使我能定位到哪里出了问题,我仍然没有办法在我自己的代码中改正,对吧?

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

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

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

采纳答案by benka

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 propertyof the characteristicsand the writeType.

什么原来是这个问题是propertycharacteristicswriteType

Because characteristics can have values set:

因为特征可以设置值:

  • write without responseOR
  • write with response
  • write without response或者
  • write with response

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

参考此属性,您必须writeType在将实际数据写入特征之前设置。

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

回答by Ashraf

Here the error/success status code and meaning

这里的错误/成功状态代码和含义

  • GATT_ILLEGAL_PARAMETER0x0087(135)
  • GATT_NO_RESOURCES0x0080(128)
  • GATT_INTERNAL_ERROR0x0081(129)
  • GATT_WRONG_STATE0x0082(130)
  • GATT_DB_FULL0x0083(131)
  • GATT_BUSY0x0084(132)
  • GATT_ERROR0x0085(133)
  • GATT_CMD_STARTED0x0086(134)
  • GATT_PENDING0x0088(136)
  • GATT_AUTH_FAIL0x0089(137)
  • GATT_MORE0x008a(138)
  • GATT_INVALID_CFG0x008b(139)
  • GATT_SERVICE_STARTED0x008c(140)
  • GATT_ENCRYPED_MITMGATT_SUCCESS
  • GATT_ENCRYPED_NO_MITM0x008d(141)
  • GATT_NOT_ENCRYPTED0x008e(142)
  • GATT_ILLEGAL_PARAMETER0x0087(135)
  • GATT_NO_RESOURCES0x0080(128)
  • GATT_INTERNAL_ERROR0x0081(129)
  • GATT_WRONG_STATE0x0082(130)
  • GATT_DB_FULL0x0083(131)
  • GATT_BUSY0x0084(132)
  • GATT_ERROR0x0085(133)
  • GATT_CMD_STARTED0x0086(134)
  • GATT_PENDING0x0088(136)
  • GATT_AUTH_FAIL0x0089(137)
  • GATT_MORE0x008a(138)
  • GATT_INVALID_CFG0x008b(139)
  • GATT_SERVICE_STARTED0x008c(140)
  • GATT_ENCRYPED_MITMGATT_SUCCESS
  • GATT_ENCRYPED_NO_MITM0x008d(141)
  • GATT_NOT_ENCRYPTED0x008e(142)

回答by Bamaco

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.

对于那些可能因为状态 133 onCharacteristicWrite 而找到这篇文章的人,我发现我们得到这个 133 结果是因为远程设备断开连接。我花了很多时间在Android端寻找问题,后来才发现问题在另一端。

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

由此我认为 status = 133 似乎是某种未记录的通用错误原因。

回答by rexxar

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.

就我而言,我需要在新活动中重用活动的 Gatt 连接,但无法并且不断断开连接并显示错误 133。因此,我求助于BluetoothGatt.close()beforestartActivity()和(重新)连接 in onStart()。如果有人对如何保持连接有更好的想法,请发布。