从 BLUE 读取多个特征并读取字符串
read mutiple characteristic from BLE and reading a string
所以我有两个问题。
让我们从第一个开始,你如何让两个 readCharacteristic
在彼此之后?我展示的代码是我认为你可以做到的。但是因为在第一个 readCharacteristic
调用中尚未调用 onCharacteristicRead
,因此不会触发下一个 readCharacteristic
。在这里,我通过在 onCharacteristicRead
中的第一个 readCharacteristic
的 if 语句中调用第二个 readCharacteristic
来解决它,但我不知道这是 normal/stupid 解决方案吗?
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
设备读取了一个整数,另一个字母 'M' 转换为整数并返回到应用程序端的 'M' 。我只读 SIGNLE 'M' 的原因是因为我不知道如何发送 whole string
之类的 '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'
如果您想了解更多信息,请直接询问
你最好分开问两个问题,因为它们之间没有任何关系。
我会回答第一个问题。您不能在两次读取之间的 onServicesDiscovered
方法内等待。即使您等待 30 秒,它也不会起作用。原因是只有一个线程可以 运行 同时在每个 BluetoothGatt
对象上回调,它是 onCharacteristicRead
的调用者清除内部 gatt 忙标志,否则会阻止你提交另一个请求。如果你愿意,你最好实现一些队列机制来保持代码更清晰。
所以我有两个问题。
让我们从第一个开始,你如何让两个 readCharacteristic
在彼此之后?我展示的代码是我认为你可以做到的。但是因为在第一个 readCharacteristic
调用中尚未调用 onCharacteristicRead
,因此不会触发下一个 readCharacteristic
。在这里,我通过在 onCharacteristicRead
中的第一个 readCharacteristic
的 if 语句中调用第二个 readCharacteristic
来解决它,但我不知道这是 normal/stupid 解决方案吗?
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
设备读取了一个整数,另一个字母 'M' 转换为整数并返回到应用程序端的 'M' 。我只读 SIGNLE 'M' 的原因是因为我不知道如何发送 whole string
之类的 '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'
如果您想了解更多信息,请直接询问
你最好分开问两个问题,因为它们之间没有任何关系。
我会回答第一个问题。您不能在两次读取之间的 onServicesDiscovered
方法内等待。即使您等待 30 秒,它也不会起作用。原因是只有一个线程可以 运行 同时在每个 BluetoothGatt
对象上回调,它是 onCharacteristicRead
的调用者清除内部 gatt 忙标志,否则会阻止你提交另一个请求。如果你愿意,你最好实现一些队列机制来保持代码更清晰。