c++ - BLE gattServer.write()
问题描述
我在尝试更新在运行 mbedOS 的 MCU 上运行的自定义 BLE 服务中的特征值时遇到问题。我正在尝试使用应用程序“nRF Connect”传递的值更新此特征的值。该值被正确接收,但特征的值未更新。请看下面的代码:
/* mbed Microcontroller Library
* Copyright (c) 2006-2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <events/mbed_events.h>
#include <mbed.h>
#include "ble/BLE.h"
uint16_t customServiceUUID = 0xA000;
uint16_t readCharUUID = 0xA001;
uint16_t writeCharUUID = 0xA002;
char data_received[30];
char SSID[30];
char password[30];
char pin[] = "1234";
char code[5];
const static char DEVICE_NAME[] = "ChangeMe!!"; // change this
static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
/* Set Up custom Characteristics */
static uint8_t readValue[10] = {0};
ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
static uint8_t writeValue[10] = {0};
WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
/* Set up custom service */
GattCharacteristic *characteristics[] = {&readChar, &writeChar};
GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
/*
* Restart advertising when phone app disconnects
*/
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
{
BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
}
/*
* Handle writes to writeCharacteristic
*/
void writeCharCallback(const GattWriteCallbackParams *params)
{
/* Check to see what characteristic was written, by handle */
if(params->handle == writeChar.getValueHandle()) {
//printf("Data received: length = %d, data = 0x",params->len);
for(int x=0; x < params->len; x++) {
if(params->data[x] == 'a'){
printf("il carattere e %c\n" , params->data[x]);
for(int x ; x + 5 ; x++){
int i = 0;
code[i] = params->data[x];
i++;
}
}
data_received[x] = params->data[x];
printf("%c", params->data[x]);
}
printf("\n\r");
printf("il codice e %s\n", code);
/* Update the readChar with the value of writeChar */
BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data , params->len);
}
}
/*
* Initialization callback
*/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE &ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
return;
}
else{
printf("%s",error);
}
ble.gap().onDisconnection(disconnectionCallback);
ble.gattServer().onDataWritten(writeCharCallback);
/* Setup advertising */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
ble.gap().setAdvertisingInterval(100); // 100ms.
/* Add our custom service */
ble.addService(customService);
/* Start advertising */
ble.gap().startAdvertising();
}
/*
* Main loop
*/
int main(void)
{
/* initialize stuff */
printf("\n\r********* Starting Main Loop *********\n\r");
BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
ble.init(bleInitComplete);
/* SpinWait for initialization to complete. This is necessary because the
* BLE object is used in the main loop below. */
while (ble.hasInitialized() == false) { /* spin loop */ }
/* Infinite loop waiting for BLE interrupt events */
while (true) {
ble.waitForEvent(); /* Save power */
}
}
特别是,应该更新特征值的函数是:
void writeCharCallback(const GattWriteCallbackParams *params)
{
/* Check to see what characteristic was written, by handle */
if(params->handle == writeChar.getValueHandle()) {
//printf("Data received: length = %d, data = 0x",params->len);
for(int x=0; x < params->len; x++) {
if(params->data[x] == 'a'){
printf("il carattere e %c\n" , params->data[x]);
for(int x ; x + 5 ; x++){
int i = 0;
code[i] = params->data[x];
i++;
}
}
data_received[x] = params->data[x];
printf("%c", params->data[x]);
}
printf("\n\r");
printf("il codice e %s\n", code);
/* Update the readChar with the value of writeChar */
BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data , params->len);
}
}
有关更多信息,请参阅以下链接 https://os.mbed.com/teams/Bluetooth-Low-Energy/code/BLE_GATT_Example/
解决方案
推荐阅读
- reactjs - 第二次调度的相同动作不会调用 saga,而在第一次调度时它会正确调用
- python-3.x - 去掉 '$' 和 ',' 的数字不会从 str 转换为 int
- mysql - 如何为表中存在的每一列调用 MySQL 函数?
- typescript - 如何在抽象类中推断构造函数参数类型
- c++ - QObject::connect: 没有这样的信号错误 C++
- http - iframe 无法访问自己创建的 cookie
- android - Android Spinner 自定义文本视图不可点击
- python - 如何在 Python Web 应用程序中为状态更改通知生成服务器发送的事件?
- javascript - 如何使用javascript获取开始日期和结束日期之间的日期,结果日期应为数组格式
- java - 在 java 中使用 Zeroc Ice 时出现 NoValueFactoryException