如何在 Mbed OS 6 中通过 BLE read/write 数据?

How to read/write data via BLE in Mbed OS 6?

我有一部 Android 智能手机和一块 Discovery L475VG 物联网开发板。我正在通过 BLE 尝试 send/receive 数据(双向)。我查阅了很多文档(尽管其中很多文档对于 Mbed OS 6 来说已经过时了)并且我一直在尝试分析 GattServer characteristicUpdate 和 characteristicWrite 示例代码中发生了什么。 See here

这两个示例代码适用于它们应该演示的内容,但它们似乎以不同方式实现读取和写入,或者我可能只是没有了解发生了什么?

如果不是很明显,我对整个 BLE 堆栈不是很熟悉,尤其是它的 mbed 实现。我之前制作了一个基于 Arduino 的系统,它实现了我在这里尝试实现的相同目标,但我无法找出 mbed 提供的更复杂、更低级别的库。

read() 和 write() 函数,这似乎是我需要的显而易见的东西,似乎是私有函数,所以我认为它不止于此,自然而然,mbed Studio 没有让我从 main() 调用它们。

有人能指出我从哪里开始的正确方向吗?

我试过修改BLE_GattServer_CharacteristicWrite让你写完后可以设置特征值。这种方法完全未经测试,因为我无法访问 mbed 开发板。

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 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 "ble/BLE.h"
#include "gatt_server_process.h"
#include "mbed-trace/mbed_trace.h"

static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);

class GattServerDemo : ble::GattServer::EventHandler {

    const static uint16_t EXAMPLE_SERVICE_UUID         = 0xA000;
    const static uint16_t WRITABLE_CHARACTERISTIC_UUID = 0xA001;

public:
    GattServerDemo()
    {
        const UUID uuid = WRITABLE_CHARACTERISTIC_UUID;
        _writable_characteristic = new ReadWriteGattCharacteristic<uint8_t> (uuid, &_characteristic_value);

        if (!_writable_characteristic) {
            printf("Allocation of ReadWriteGattCharacteristic failed\r\n");
        }
    }

    ~GattServerDemo()
    {
    }

    void start(BLE &ble, events::EventQueue &event_queue)
    {
        const UUID uuid = EXAMPLE_SERVICE_UUID;
        GattCharacteristic* charTable[] = { _writable_characteristic };
        GattService example_service(uuid, charTable, 1);

        _server = &ble.gattServer();
        _server.addService(example_service);

        _server.setEventHandler(this);

        printf("Example service added with UUID 0xA000\r\n");
        printf("Connect and write to characteristic 0xA001\r\n");
    }

private:
    /**
     * This callback allows the LEDService to receive updates to the ledState Characteristic.
     *
     * @param[in] params Information about the characterisitc being updated.
     */
    virtual void onDataWritten(const GattWriteCallbackParams &params)
    {
        if ((params.handle == _writable_characteristic->getValueHandle()) && (params.len == 1)) {
            printf("New characteristic value written: %x\r\n", *(params.data));
            // Set value of characteristic to value * 2
            _writable_characteristic->set(*_server, params.data * 2);
        }
    }

private:
    ReadWriteGattCharacteristic<uint8_t> *_writable_characteristic = nullptr;
    uint8_t _characteristic_value = 0;
    GattServer *_server = nullptr;
};

int main()
{
    mbed_trace_init();

    BLE &ble = BLE::Instance();

    printf("\r\nGattServer demo of a writable characteristic\r\n");

    GattServerDemo demo;

    /* this process will handle basic setup and advertising for us */
    GattServerProcess ble_process(event_queue, ble);

    /* once it's done it will let us continue with our demo*/
    ble_process.on_init(callback(&demo, &GattServerDemo::start));

    ble_process.start();

    return 0;
}

这个以后读取的时候要设置特征为写入值*2

好的,我已经弄明白了,感谢 Michael Kotzjan 的帮助:

    virtual void onDataWritten(const GattWriteCallbackParams &params)
    {
        if ((params.handle == _writable_characteristic->getValueHandle()) && (params.len == 1)) {
            printf("New characteristic value written: %x\r\n", *(params.data));
            // Set value of characteristic to value * 2
            uint8_t temp = *params.data * 2;
            _server->write(_writable_characteristic->getValueHandle(),&temp, sizeof(temp),false);
        }
    }

当 phone 向 mbed 板发送一个字节时,mbed 板将写入特性设置为接收到的值的两倍。指针的东西有点混乱,但现在可以了。