读取来自BLE的多个特征,并读取一个字符串。

huangapple go评论63阅读模式
英文:

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&#39;s start with the first one, how do you make two `readCharacteristic` after eachothers? the code I&#39;ve showed is what I was thinking you could do it. But because `onCharacteristicRead` isn&#39;t called yet in the first `readCharacteristic` call the next `readCharacteristic` isn&#39;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&#39;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, &quot;Connection State: Service characteristic UUID found: &quot; + mBluetoothGattService.getUuid().toString());
mCharacterisitc = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead));
mCharacterisitc2 = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead2));
Log.w(TAG, &quot;Connection State 1: mCharacterisitc &quot; + mCharacterisitc + &quot; &quot; + 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, &quot;Connection State: Service characteristic not found for UUID: &quot; + 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 &#39;Made it&#39;. 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&#39;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(&amp;serviceHandle); //sending the data to -&gt; OUTBOUND
    
    //this part should probably not be in a for-loop, but for now it is.
    ValStr = &quot;Mads Sander Hoegstrup&quot;; //I want read whole string on my android APP
    serviceData.val = (uint8*) ValStr; //this only takes the &#39;M&#39; 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&#39;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(&amp;serviceHandle); //sending the data to -&gt; 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'

读取来自BLE的多个特征,并读取一个字符串。

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.

huangapple
  • 本文由 发表于 2020年9月18日 22:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63957863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定