read mutiple characteristic from BLE and reading a string

442 views Asked by At

So I have two question. Let's start with the first one, how do you make two readCharacteristic after eachothers? the code I've showed is what I was thinking you could do it. But because onCharacteristicRead isn't called yet in the first readCharacteristic call the next readCharacteristic isn't triggered. Here i solved it by calling the second readCharacteristic in the if-statement for the first readCharacteristic in the onCharacteristicRead, but i don't know it this is normal/stupid solution?

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

Next question is a bit hard I think?

the code is made in PSoC creator 4.3

So at the moment I read a single int from my PSoC 6 BLE device, and another letter 'M' converted to a integer and back to a 'M' on the app-side. The reason I only read a SIGNLE 'M' is because I don't know how to send a whole string like 'Made it'. I think the issue I'm having is on the PSoC side where I don't know how to 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);
}

Here you can see that I revice the right values, a Integer and a String, but only the letter 'M' and not the string 'Mads Sander Hoegstrup'

enter image description here

Just ask if you want more information

1

There are 1 answers

3
Emil On BEST ANSWER

You'd better ask two separate questions, since they have nothing to do with each other.

I'll answer the first question. You cannot wait inside the onServicesDiscovered method between the two reads. Even if you wait for 30 seconds it will not work. The reason is that only one thread can run a callback on each BluetoothGatt object at the same time, and it's the caller of onCharacteristicRead that clears the internal gatt busy flag which otherwise prevents you from submitting another request. You'd better implement some queue mechanism to keep the code cleaner if you like.