BLE
蓝牙低功耗(BLE)是用于构建个人区域网络的低功率无线技术标准。BLE 的典型应用是医疗保健,健身追踪器,信标,智能家居,安全,娱乐,接近传感器,工业和汽车。
Arm Mbed BLE,也称为 BLE_API,是 Mbed 的蓝牙低功耗软件解决方案。许多 Mbed 目标和组件支持 Mbed BLE。开发人员可以使用它来创建新的支持 BLE 的应用程序。
Mbed 的 BLE_API 与平台上的 BLE 控制器接口。它隐藏了 BLE 堆栈在 C++ 抽象背后的复杂性,并且与所有支持 BLE 的 Mbed 平台兼容。Mbed OS BLE_API 自动配置时钟,定时器和其他硬件外设,以最低功耗工作。
BLE_API,桥接和栈
您可以使用 Mbed OS,BLE_API 和控制器特定的蓝牙栈以及一些桥接软件构建 BLE 应用程序,以使其适应 BLE_API:
- BLE_API 如上所述。
- 桥接软件特定于每个供应商的平台。它为 BLE_API 提供的接口提供实例化,并帮助驱动底层控制器和蓝牙栈。
- 蓝牙栈实现蓝牙协议并且特定于控制器,因此使用不同控制器的供应商可以提供不同的栈。
在 BLE_API 里面
BLE_API 提供构建块来帮助构建应用程序。这些分为两大类:
-
在 ble/表达 BLE 结构的接口,例如 GAP,GATT,服务和特性。
-
ble/services 下的类为许多常用的 GATT 配置文件提供参考实现。'services/' 下的代码不是必需的,但它是原型设计的有用起点。我们继续实施标准的 GATT 配置文件。
BLEDevice 类和头文件
Mbed 的 BLE_API 的入口点是使用标题 ble/BLE.h 可访问的 BLE 类。此类允许您获取 BLE 对象,该对象包含规范兼容的 BLE 设备的基本属性,并且可以与任何 BLE 无线电一起使用:
#include "ble/BLE.h"
BLE& mydevicename = BLE::Instance();
该类的成员函数可以按目的划分:
-
基本 BLE 操作,例如初始化控制器。
-
管理 GAP,GATT 或安全性的蓝牙模块的访问者。
用法
- 设置广告和连接模式。
- 将 UUID 分配给服务及其特性。
- 创建输入特征。
- 构造一个服务类并将其添加到 BLE 栈。
- 特征值更改时推送通知。
BLE 类参考
数据结构 | |
struct | InitializationCompleteCallbackContext |
struct | OnEventsToProcessCallbackContext |
公共类型 | |
typedef unsigned | InstanceID_t |
typedef FunctionPointerWithContext< OnEventsToProcessCallbackContext * > | OnEventsToProcessCallback_t |
typedef void(* | InitializationCompleteCallback_t) (InitializationCompleteCallbackContext *context) |
静态公共成员函数 | |
static BLE & | Instance (InstanceID_t id=DEFAULT_INSTANCE) |
static const char * | errorToString (ble_error_t error) |
Static Public Attributes | |
static const InstanceID_t | DEFAULT_INSTANCE = 0 |
static const InstanceID_t | NUM_INSTANCES = 1 |
Friends | |
class | BLEInstanceBase |
示例:BLE 信标
这是一个演示如何创建 BLE 信标的示例。
/* 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"
#include "ble/services/iBeacon.h"
static iBeacon* ibeaconPtr;
static EventQueue eventQueue(/* event count */ 4 * EVENTS_EVENT_SIZE);
/**
* This function is called when the ble initialization process has failled
*/
void onBleInitError(BLE &ble, ble_error_t error)
{
/* Initialization error handling should go here */
}
/**
* Callback triggered when the ble initialization process has finished
*/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
/* Ensure that it is the default instance of BLE */
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
/**
* The Beacon payload has the following composition:
* 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
* Major/Minor = 0x1122 / 0x3344
* Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
*
* Note: please remember to calibrate your beacons TX Power for more accurate results.
*/
static const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
uint16_t majorNumber = 1122;
uint16_t minorNumber = 3344;
uint16_t txPower = 0xC8;
ibeaconPtr = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower);
ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ble.gap().startAdvertising();
}
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
BLE &ble = BLE::Instance();
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}
int main()
{
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
ble.init(bleInitComplete);
eventQueue.dispatch_forever();
return 0;
}
示例:BLE 心率监测器
这是一个示例,演示如何构建可由 BLE 客户端(如手机)连接和监控的心率传感器。
/* 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"
#include "ble/Gap.h"
#include "ble/services/HeartRateService.h"
DigitalOut led1(LED1, 1);
const static char DEVICE_NAME[] = "HRM";
static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
static uint8_t hrmCounter = 100; // init HRM to 100bps
static HeartRateService *hrServicePtr;
static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
BLE::Instance().gap().startAdvertising(); // restart advertising
}
void updateSensorValue() {
// Do blocking calls or whatever is necessary for sensor polling.
// In our case, we simply update the HRM measurement.
hrmCounter++;
// 100 <= HRM bps <=175
if (hrmCounter == 175) {
hrmCounter = 100;
}
hrServicePtr->updateHeartRate(hrmCounter);
}
void periodicCallback(void)
{
led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
if (BLE::Instance().getGapState().connected) {
eventQueue.call(updateSensorValue);
}
}
void onBleInitError(BLE &ble, ble_error_t error)
{
(void)ble;
(void)error;
/* Initialization error handling should go here */
}
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
onBleInitError(ble, error);
return;
}
if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
ble.gap().onDisconnection(disconnectionCallback);
/* Setup primary service. */
hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
/* Setup advertising. */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(1000); /* 1000ms */
ble.gap().startAdvertising();
}
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
BLE &ble = BLE::Instance();
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}
int main()
{
eventQueue.call_every(500, periodicCallback);
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
ble.init(bleInitComplete);
eventQueue.dispatch_forever();
return 0;
}
相关内容
- Mbed 启用支持 BLE 的目标和组件目标和组件。