BLE
AWTK 的低功耗藍牙 (BLE) 插件
包含頭檔案
#include "ble/ble.h"
呼叫方法
-
- 創建 ble 物件,
如:
ble_t* ble = ble_create();
-
- 掃描設備
掃描前先要注冊 on_device_found 回呼函式,每掃描到一個設備就會呼叫該回呼函式,
static ret_t ble_main_on_device_found(void* ctx, ble_device_t* device) {
...
return RET_OK;
}
ble_set_on_device_found(ble, ble_main_on_device_found, win);
ble_start_scan(ble);
-
- 連接設備
當 on_device_services_discovered 被呼叫時,設備才真正連接,之后才能收發資料,
注冊回呼函式:
static ret_t ble_deivce_on_device_services_discovered(void* ctx, ble_device_t* device) {
return RET_OK;
}
ble_set_on_device_services_discovered(device->ble, ble_deivce_on_device_services_discovered, win);
ble_connect_to(ble, address);
-
- 收發資料,
收發資料是異步的,先注冊相應的回呼函式,再收發資料,
ble_set_on_characteristic_write(device->ble, ble_on_characteristic_write, win);
ble_set_on_characteristic_read(device->ble, ble_on_characteristic_read, win);
ble_read_characteristic(device->ble, device->id, achar->id);
ble_write_characteristic(device->ble, device->id, achar->id, data, FALSE);
-
- 主動上報的資料
注冊 on_characteristic_changed 即可,
ble_set_on_characteristic_changed(device->ble, ble_on_characteristic_changed, win);
完整示例請參考 demo,
API
BLE API
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260320.html
標籤:其他
