代码拉取完成,页面将自动刷新
同步操作将从 FredyXu/cw_esp32_ble_weapp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。