1 Star 0 Fork 21

niubileni / cw_esp32_ble_weapp

forked from FredyXu / cw_esp32_ble_weapp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ble.h 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
FredyXu 提交于 2020-12-18 14:53 . 版本更新
#ifndef _BLE_H
#define _BLE_H
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#define SERVICE_UUID "6880d7e7-93ba-4fdf-8940-31226ab93ac6"
#define CHARACTERISTIC_UUID "ccc5dfd0-6ce3-4f1a-9ab9-f7d9a88321ec"
BLECharacteristic *pCharacteristic;
// 初始化蓝牙
void ble_init();
// 发送信息
void ble_send(String msg);
// 处理收到的信息
void ble_op_msg(String msg);
// 分割字符串
void str_split(String s,String p);
// 多核发送信息
void ble_send_core(String msg, int core);
void ble_send_core_do(void *v);
void ble_send_core(String msg, int core) {
var_ble_msg = msg;
xTaskCreatePinnedToCore(ble_send_core_do, "ble_send_core_do", 10000, NULL, 1, NULL, core);
}
void ble_send_core_do(void *v)
{
pCharacteristic->setValue((char *)var_ble_msg.c_str());
pCharacteristic->notify();
var_ble_msg = "";
vTaskDelete(NULL);
}
void ble_send(String msg_type, String msg)
{
if(msg_type == "code") {
msg = "inp:" + msg;
}
else if(msg_type == "cmd") {
msg = "cmd:" + msg;
}
pCharacteristic->setValue((char *)msg.c_str());
pCharacteristic->notify();
}
class callback: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if(value.length() > 0) {
ble_op_msg(value.c_str());
}
}
};
void ble_op_msg(String msg) {
// 分割信息
String info[] = {"", "", ""};
int p = 0;
for(int i = 0; i < msg.length(); i++) {
if(msg[i] == ':') {
p++;
continue;
}
info[p] += msg[i];
}
// 设置参数
if(info[0] == "set") {
update_settings(info[1], info[2]);
}
// 播放电码
else if(info[0] == "ply") {
play_code(info[1]);
}
// 播放反馈
else if(info[0] == "plc") {
flag_play_code = true;
play_code_core(info[1], 1);
}
else if(info[0] == "stp") {
flag_play_code = false;
}
}
void ble_init() {
BLEDevice::init("ZClub CW");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->setCallbacks(new callback());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("蓝牙初始化完成");
}
#endif
C
1
https://gitee.com/niubileni/cw_esp32_ble_weapp.git
git@gitee.com:niubileni/cw_esp32_ble_weapp.git
niubileni
cw_esp32_ble_weapp
cw_esp32_ble_weapp
master

搜索帮助