如何在 Mbed OS 6 中通过 BLE 读取/写入数据?

发布于 2025-01-14 00:02:54 字数 591 浏览 6 评论 0原文

我有一部 Android 智能手机和一块 Discovery L475VG IoT 开发板。我正在尝试通过 BLE 发送/接收数据(双向)。我查阅了大量文档(尽管其中大部分对于 Mbed OS 6 来说已经过时),并且我一直在尝试分析 GattServercharacteristicUpdate 和characteristicWrite 示例代码中发生的情况。 参见此处

这两个示例代码适用于它们应该演示的内容,但他们似乎以不同的方式实现读取和写入,或者也许我只是没有关注正在发生的事情?

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

read() 和 write() 函数看起来是我需要的显而易见的东西,但它们似乎是私有函数,因此我认为还有更多功能,而且,mbed Studio 自然不会只让我调用它们来自 main()。

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

I have an Android smartphone and a Discovery L475VG IoT development board. I am trying send/receive data (both directions) via BLE. I have consulted a good bit of documentation (although much of it is out of date for Mbed OS 6) and I have been trying to analyze what's going on in the GattServer characteristicUpdate and characteristicWrite example codes. See here

The two example codes work for what they're supposed to demonstrate, but they seem to go about implementing reads and writes differently, or maybe I am just not following what's going on?

If it isn't obvious, I am not super familiar with the BLE stack as a whole, and particularly not the mbed implementation of it. I previously made an Arduino-based system that accomplished the same goal I'm trying to here, but I am having trouble figuring out more complicated, lower-level library that mbed provides.

The read() and write() functions, which seem like the obvious things I would need, appear to be private functions, so I assume there is more to it than that and, naturally, mbed Studio doesn't just let me call them from main().

Can someone point me in the right direction of where to start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小耗子 2025-01-21 00:02:54

我尝试修改 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 ¶ms)
    {
        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。

I tried to modify the BLE_GattServer_CharacteristicWrite to allow you to set the characteristic value after writing. This approach is completely untested since I don't have access to a mbed developer board.

/* 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 ¶ms)
    {
        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;
}

This should set the characteristic to the written value * 2 when reading afterwards.

守不住的情 2025-01-21 00:02:54

好的,我已经弄清楚了,感谢 Michael Kotzjan 的帮助:

    virtual void onDataWritten(const GattWriteCallbackParams ¶ms)
    {
        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);
        }
    }

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

Ok, I have figured it out, thanks to Michael Kotzjan for the help:

    virtual void onDataWritten(const GattWriteCallbackParams ¶ms)
    {
        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);
        }
    }

When the phone sends a byte to the mbed board, the mbed board sets the write characteristic to double the value that was received. The pointer stuff was a bit confusing, but this works now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文