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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...