英文:
read mutiple characteristic from BLE and reading a string
问题
以下是翻译后的内容:
所以我有两个问题。
让我们从第一个问题开始,如何在彼此之后进行两次 `readCharacteristic`?我展示的代码是我认为你可以这样做的。但是因为在第一次 `readCharacteristic` 调用中尚未调用 `onCharacteristicRead`,所以下一个 `readCharacteristic` 不会触发。在这里,我通过在 `onCharacteristicRead` 中的第一个 `readCharacteristic` 的 if 语句中调用第二个 `readCharacteristic` 来解决了这个问题,但我不知道这是否是正常/愚蠢的解决方案?
下一个问题可能有点困难?
`该代码是在 PSoC 创建者 4.3 中制作的`
所以目前我从我的 `PSoC 6 BLE` 设备中读取一个整数,另一个字母 'M' 转换为整数,然后再转回 'M' 在应用程序端。我之所以只读一个 'M',是因为我不知道如何发送像 'Made it' 这样的 `整个字符串`。我认为我遇到的问题在 `PSoC` 那一侧,我不知道如何 `读取整个字符串`。
```c
for(;;)
{
/* 在这里放置您的应用程序代码。https://www.youtube.com/watch?v=Aeip0hkc4YE */
cy_stc_ble_gatt_handle_value_pair_t serviceHandle;
cy_stc_ble_gatt_value_t serviceData;
//这些是我之前在代码中声明的变量
//static uint8 data[1] = {0};
//static char * ValStr;
//这里我只有一个简单的整数,每秒递增
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); //将数据发送到 -> OUTBOUND
//这部分可能不应该在循环中,但目前是这样的。
ValStr = "Mads Sander Hoegstrup"; //我想在我的 Android 应用程序上读取整个字符串
serviceData.val = (uint8*) ValStr; //这只取了 'M',这是我可以从我的应用程序中读取的唯一变量,而不是字符串的其余部分
serviceData.len = 1; //如果增加不会有帮助,如果大于1,我会读取0而不是一个字母
serviceHandle.attrHandle = CY_BLE_CUSTOM_SERVICE_DEVICE_OUTBOUND_2_CHAR_HANDLE;
serviceHandle.value = serviceData;
Cy_BLE_GATTS_WriteAttributeValueLocal(&serviceHandle); //将数据发送到 -> OUTBOUND_2
data[0]++;
CyDelay(1000);
}
在这里,您可以看到我接收到了正确的值,一个整数和一个字符串,但只有字母 'M',而不是字符串 'Mads Sander Hoegstrup'。
[![在这里输入图片描述][1]][1]
[1]: https://i.stack.imgur.com/GpxNO.png
如果您需要更多信息,请随时提问。
<details>
<summary>英文:</summary>
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?
```java
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'
Just ask if you want more information
答案1
得分: 0
你最好分开提问,因为它们彼此无关。
我会回答第一个问题。你不能在两次读取之间在 onServicesDiscovered
方法内等待。即使你等待30秒,也不会起作用。原因是在每次 BluetoothGatt
对象上,只有一个线程可以运行回调,而是调用 onCharacteristicRead
的调用者会清除内部的忙碌标志,否则会阻止你提交另一个请求。如果你愿意,最好实现一些队列机制,以保持代码更清晰。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论