0

所以我有两个问题。让我们从第一个开始,你如何readCharacteristic在彼此之后制作两个?我展示的代码是我认为你可以做到的。但是因为onCharacteristicRead在第一次readCharacteristic调用中还没有被调用,所以下一次调用readCharacteristic没有被触发。在这里,我通过在 if 语句中readCharacteristic为第一个调用第二个来解决它,但我不知道这是正常/愚蠢的解决方案吗?readCharacteristiconCharacteristicRead

public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattService mBluetoothGattService = gatt.getService(UUID.fromString(CSUuid));
        if (mBluetoothGattService != null) {
            Log.i(TAG, "Connection State: Service characteristic UUID found: " + mBluetoothGattService.getUuid().toString());
            mCharacterisitc = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead));
            mCharacterisitc2 = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead2));
            Log.w(TAG, "Connection State 1: mCharacterisitc " + mCharacterisitc + " " + mCharacterisitc2);
            readCharacteristic(gatt, mCharacterisitc);
            //I know I have to wait for the above is done, but can I do it here instead of
            //calling the line under in onCharacteristicRead? 
            readCharacteristic(gatt, mCharacterisitc2);
        } else {
            Log.i(TAG, "Connection State: Service characteristic not found for UUID: " + UuidRead);
        }
    }
}

下一个问题我觉得有点难?

the code is made in PSoC creator 4.3

因此,此刻我从我的PSoC 6 BLE设备中读取了一个 int,另一个字母“M”转换为整数并在应用程序端返回为“M”。我只阅读 SIGNLE 'M' 的原因是因为我不知道如何发送whole stringlike 'Made it'。我认为我遇到的问题是PSoC side我不知道该怎么做read a whole string

for(;;)
{
    /* Place your application code here. https://www.youtube.com/watch?v=Aeip0hkc4YE*/
    cy_stc_ble_gatt_handle_value_pair_t serviceHandle;
    cy_stc_ble_gatt_value_t serviceData;
    
    //this is the variables I've declared earlier in the code
    //static uint8 data[1] = {0}; 
    //static char * ValStr;
    
    //here I just have a simple Integer which count up every sec
    serviceData.val = (uint8*)data;
    serviceData.len = 1;
    
    serviceHandle.attrHandle = CY_BLE_CUSTOM_SERVICE_DEVICE_OUTBOUND_CHAR_HANDLE;
    serviceHandle.value = serviceData;
    
    Cy_BLE_GATTS_WriteAttributeValueLocal(&serviceHandle); //sending the data to -> OUTBOUND
    
    //this part should probably not be in a for-loop, but for now it is.
    ValStr = "Mads Sander Hoegstrup"; //I want read whole string on my android APP
    serviceData.val = (uint8*) ValStr; //this only takes the 'M' and thats the only variable I can read from my APP not the rest of the string
    serviceData.len = 1; //Does not help to increase, if it's more than 1 I read 0 and not a letter
    
    serviceHandle.attrHandle = CY_BLE_CUSTOM_SERVICE_DEVICE_OUTBOUND_2_CHAR_HANDLE;
    serviceHandle.value = serviceData;
    
    Cy_BLE_GATTS_WriteAttributeValueLocal(&serviceHandle); //sending the data to -> OUTBOUND_2
    
    data[0]++;
    CyDelay(1000);
}

在这里你可以看到我修改了正确的值,一个整数和一个字符串,但只有字母“M”而不是字符串“Mads Sander Hoegstrup”

在此处输入图像描述

只需询问您是否需要更多信息

4

1 回答 1

0

你最好问两个不同的问题,因为它们彼此无关。

我会回答第一个问题。您不能onServicesDiscovered在两次读取之间的方法内等待。即使您等待 30 秒,它也不起作用。原因是只有一个线程可以同时在每个BluetoothGatt对象上运行回调,并且调用者onCharacteristicRead会清除内部 gatt 繁忙标志,否则会阻止您提交另一个请求。如果你愿意,你最好实现一些队列机制来保持代码更干净。

于 2020-09-18T19:08:49.117 回答