微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Android蓝牙状态133在onCharacteristicwrite

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

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

和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*/,0);
            gatt.writeCharacteristic(tChar);
        }
        else if {
            ...
        }
    } catch (AssertionError e) {
        ...
    }

写入第一个特征没有错,控制将达到onCharacteristicWrite,并输入状态为0的第一个if语句,这意味着成功.问题是在if语句中的第二个写入动作,它也将触发CharCharacteristicWrite函数,但产生状态133,在the official site中找不到,然后设备将自动断开.

我已经确认数据类型和偏移量都是正确的.并且因为在另一个设备中,它的工作非常好,我认为在不同的设备之间可能会有一些微小的蓝牙栈实现差异,我应该做更多的棘手来解决这个问题.

搜索结果很长时间.一些结果导致我的C源代码(对不起,我会发布链接,因为我没有足够的声誉发布超过2个链接),但我只能发现133意味着GATT_ERROR在那里,这不是更有帮助而不仅仅是一个133.我还发现一个问题在谷歌集团,讨论一些熟悉的问题,但我没有找到一个解决方在这里.

我有点伤心,因为如果C代码有问题,即使我可以找到错误的地方,我仍然没有办法在我自己的代码中正确使用它,对吗?

我希望有人有熟悉的经验,可能会给我一些建议.非常感谢!

链接

> 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

解决方法

我有一个类似的问题,当我试图写一些特性,我不记得,如果我有相同的错误代码或不. (它在一些设备上工作,而没有在其他设备上).

原来是问题的特征就是特性和writeType.

因为特征可以设置值:

>写没有回应或
写回应

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

一旦获得特征,但在写入之前,您可以设置该类型.

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*/,0);
        gatt.writeCharacteristic(tChar);

原文地址:https://www.jb51.cc/android/126435.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐