BW16 BLE CLIENT與ESP32 BLE Server

請問bw16的battery ble client程式修改之後,能夠和esp32的ble server溝通嗎? 我發現bw16和esp32 ble的client程是寫法有許多不同之處? 有辦法讓兩者互連嗎?

@tom.chen2017

应该是可以的,只要它们的数据格式一样,两边都用的是 BLE,就应该能够沟通。

这个正常,就像 esp32,adafruit,arduino 的 BLE API 都有点不一样

1.請問BW16的BLE2902.h部分由哪個檔案替代呢?
2.另外
在esp32 ble連續丟8 bytes參數的寫法
static uint8_t custom[8] = {0xff, 0x40, 0x41, 0x42, 0x00, 0x00, (byte) 0xA3, (byte) 0x3D};
pRemoteCharacteristic->writeValue(custom, sizeof(custom));

在bw16可以怎麼改寫呢? 下面bw16的寫法成立嗎?
static uint8_t custom[8] = {0xff, 0x40, 0x41, 0x42, 0x00, 0x00, (byte) 0xA3, (byte) 0x3D};
battChar1.setData(&custom[8],8);

@tom.chen2017

  • 2902 的功能 BLECharacteristic 本身就有,通过以下 function 开启和使用 notify 和 indicate 功能
void setNotifyProperty(bool value);
void setIndicateProperty(bool value);
void notify(uint8_t conn_id);
void indicate(uint8_t conn_id);
static uint8_t custom[8] = {0xff, 0x40, 0x41, 0x42, 0x00, 0x00, 0xA3, 0x3D};
battChar1.setData(custom, 8);

或者

static uint8_t custom[8] = {0xff, 0x40, 0x41, 0x42, 0x00, 0x00, 0xA3, 0x3D};
battChar1.setData(custom, sizeof(custom));

battChar1.setData(custom, 8);
或者
battChar1.setData(custom, sizeof(custom));
Compiler後會告知
invalid conversion from ‘uint8_t {aka unsigned char}’ to ‘uint8_t* {aka unsigned char*}’ [-fpermissive]

請問可以提供類似esp32 BLEClient & BLEService的範例嗎? (不是BLEBatteryClient,BLEBatteryService)

@tom.chen2017

以下是与 esp32 BLEClient BLEService 兼容的 AmebaD BLE 範例。
esp32 BLEClient loop 里面的功能没写,但是两个开发板互相连接读写数据是没问题的。

BLEService:

#include "BLEDevice.h"

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

#define STRING_BUF_SIZE 100

BLEService BLEserv(SERVICE_UUID);
BLECharacteristic BLEchar(CHARACTERISTIC_UUID);
BLEAdvertData advdata;
BLEAdvertData scndata;

void setup() {
  Serial.begin(115200);

  BLE.init();
  BLE.setDeviceName(String("Long name works now"));

  BLEserv.addCharacteristic(BLEchar);
  BLEchar.setReadProperty(true);
  BLEchar.setReadPermissions(GATT_PERM_READ);
  BLEchar.setWriteProperty(true);
  BLEchar.setWritePermissions(GATT_PERM_WRITE);
  BLEchar.setBufferLen(STRING_BUF_SIZE);
  BLEchar.writeString("Hello World says Neil");

  advdata.addFlags(GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
  advdata.addCompleteName("Long name works now");
  scndata.addCompleteServices(BLEUUID(SERVICE_UUID));

  BLE.configAdvert()->setAdvData(advdata);
  BLE.configAdvert()->setScanRspData(scndata);
  BLE.configServer(1);
  BLE.addService(BLEserv);

  BLE.beginPeripheral();
}

void loop() {
  delay(2000);
}

BLEClient:


#include "BLEDevice.h"

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

#define STRING_BUF_SIZE 100

BLEAdvertData foundDevice;
BLEAdvertData targetDevice;
BLEClient* client;
BLERemoteService* BLEserv;
BLERemoteCharacteristic* BLEchar;

void scanCB(T_LE_CB_DATA* p_data) {
  foundDevice.parseScanInfo(p_data);
  if (foundDevice.hasUUID()) {
    uint8_t UUIDcount = foundDevice.getServiceCount();
    BLEUUID* UUIDlist = foundDevice.getServiceList();
    for (uint8_t i = 0 ; i < UUIDcount; i++) {
      UUIDlist[i] == BLEUUID(SERVICE_UUID);
      Serial.print("Found BLE Device at address ");
      Serial.println(foundDevice.getAddr().str());
      targetDevice = foundDevice;
      BLE.configScan()->stopScan();
    }
  }
}

void notificationCB (BLERemoteCharacteristic* chr, uint8_t* data, uint16_t len) {
  char msg[len + 1] = {0};
  memcpy(msg, data, len);
  Serial.print("Notification received for chr UUID: ");
  Serial.println(chr->getUUID().str());
  Serial.print("Received string: ");
  Serial.println(String(msg));
}

void setup() {
  Serial.begin(115200);

  BLE.init();
  BLE.setScanCallback(scanCB);
  BLE.beginCentral(1);

  BLE.configScan()->startScan(5000);
  BLE.configConnection()->connect(targetDevice, 2000);
  delay(2000);
  int8_t connID = BLE.configConnection()->getConnId(targetDevice);
  if (!BLE.connected(connID)) {
    Serial.println("BLE not connected");
  } else {
    Serial.println("BLE connected to server");
    BLE.configClient();
    client = BLE.addClient(connID);
    client->discoverServices();
    Serial.print("Discovering services of connected device");
    do {
      Serial.print(".");
      delay(1000);
    } while (!(client->discoveryDone()));
    Serial.println();

    BLEserv = client->getService(SERVICE_UUID);
    if (BLEserv != nullptr) {
      Serial.println("Found our service");
      BLEchar = BLEserv->getCharacteristic(CHARACTERISTIC_UUID);
      if (BLEchar != nullptr) {
        Serial.println("Found our characteristic");
        BLEchar->setBufferLen(STRING_BUF_SIZE);
      }
    }

    if (BLEchar->canRead()) {
      Serial.print("The characteristic value was: ");
      Serial.println(BLEchar->readString());
    }

    if (BLEchar->canNotify()) {
      BLEchar->enableNotifyIndicate();
    }

  }
}

void loop() {
  delay(1000);
}

我依照上面Client的寫法會有無法收值的問題!! 下面修改的寫法可以收到Server數值,但只有data[0]和data[7]是對的,data[1]~data[5]全部是0
程式如下(不知道少了什麼流程)

#include “BLEDevice.h”

#define SPO2_READ_TIMEOUT 5000 // 每五秒發送BLE命令
#define serviceUUID “00001233-1212-ffff-1123-777777777123”
#define charUUID “00001234-1212-ffff-1123-785feabcd123”

//static
uint8_t custom[8] = {0x12, 0x23, 0x00, 0x00, 0x00, 0x00, (byte) 0x56, (byte) 0x34};
const char* spo2DeviceName = “ttg123”;
float parseSfloat(int index, uint8_t* pData, int dataSize);
int unsignedByteToInt(const byte b);

BLEAdvertData* myDevice;
BLERemoteCharacteristic* pRemoteCharacteristic;

static unsigned long lastWriteTime;

BLEAdvertData foundDevice;
BLEAdvertData targetDevice;
BLEClient* client;
BLEDevice* bdev;
BLERemoteService* battService;

BLERemoteCharacteristic* battChar;
bool notifyState = false;
int8_t connID;
/////////////////////////////////////////////////////
void scanCB(T_LE_CB_DATA* p_data) {
foundDevice.parseScanInfo(p_data);
if (foundDevice.hasName()) {
if (foundDevice.getName() == String(“ttg123”)) {
Serial.print("Found Ameba BLE Device at address “);
Serial.println(foundDevice.getAddr().str());
targetDevice = foundDevice;
}
}
uint8_t serviceCount = foundDevice.getServiceCount();
if (serviceCount > 0) {
BLEUUID* serviceList = foundDevice.getServiceList();
for (uint8_t i = 0; i < serviceCount; i++) {
if (serviceList[i] == BLEUUID(” 00001233-1212-ffff-1123-777777777123 ")) {
Serial.print("Found Battery Service at address ");
Serial.println(foundDevice.getAddr().str());
}
}
}
}

void notificationCB (BLERemoteCharacteristic* chr, uint8_t* data, uint16_t len) {
Serial.print("Notification received for chr UUID: ");
Serial.print(chr->getUUID().str());

Serial.print(" len");
Serial.println(len);
Serial.print(" d00");
Serial.println(data[0]);
Serial.print(" d10");
Serial.println(data[1]);
Serial.print(" d20");
Serial.println(data[2]);
Serial.print(" d30");

Serial.println(data[3]);
Serial.print(" d40");
Serial.println(data[4]);
Serial.print(" d50");
Serial.println(data[5]);
Serial.print(" d60");
Serial.println(data[6]);
Serial.print(" d70");
Serial.println(data[7]);
}

void setup() {
Serial.begin(115200);
delay(100);
Serial.println(“Starting!”);
BLE.init();
lastWriteTime = millis();
BLE.configScan()->setScanMode(GAP_SCAN_MODE_ACTIVE);
BLE.configScan()->setScanInterval(1349); // Start a scan every 500ms
BLE.configScan()->setScanWindow(449); // Each scan lasts for 250ms
BLE.setScanCallback(scanCB);
BLE.beginCentral(1);

BLE.configScan()->startScan(5000);
BLE.configConnection()->connect(targetDevice, 2000);
delay(2000);
connID = BLE.configConnection()->getConnId(targetDevice);

if (!BLE.connected(connID)) {
Serial.println(“BLE not connected”);
} else {
BLE.configClient();
client = BLE.addClient(connID);
client->discoverServices();
Serial.print(“Discovering services of connected device”);
do {
Serial.print(“11”);
delay(1000);
} while (!(client->discoveryDone()));
Serial.println();

battService = client->getService(serviceUUID);
if (battService != nullptr) {
battChar = battService->getCharacteristic(charUUID);
if (battChar != nullptr) {
Serial.println(“Battery level characteristic found”);
battChar->setNotifyCallback(notificationCB);
}
}
}
}

void loop() {
if (BLE.connected(connID)) {

if (millis() - lastWriteTime > SPO2_READ_TIMEOUT) {
lastWriteTime = millis();
// battChar->setData(custom,8);
Serial.println(battChar->writeData8(0x12));
Serial.println(battChar->writeData8(0x23));
Serial.println(battChar->writeData8(0x00));
Serial.println(battChar->writeData8(0x00));
Serial.println(battChar->writeData8(0x00));
Serial.println(battChar->writeData8(0x00));
Serial.println(battChar->writeData8((byte)0x56));
Serial.println(battChar->writeData8((byte)0x34));

delay(5000);
notifyState = !notifyState;
if (notifyState) {
Serial.println(“Notifications Enabled”);
battChar->enableNotifyIndicate();
} else {
Serial.println(“Notifications Disabled”);
battChar->disableNotifyIndicate();
}
} else {
Serial.println(“BLE not connected”);
delay(5000);
}
}
}

wyy via Realtek Ameba IOT Developers Forum (RTL8722 RTL8195 RTL8710 RTL8720 BW16 Development board) - IOT / MCU Solutions 瑞昱開發者論壇 開發板 开發者论坛 开發板 <notifications@ameba.discoursemail.com> 於 2023年2月9日 週四 下午5:05寫道:

Hi, 已經解決問題!! 謝謝!!!

1 Like

請問如以下DFU等功能藍芽程式應該怎麼寫呢?
prr123 DFU Service
◈ Service UUID: abcdaaaa-351f-712b-fc0e-a2345ce70038
◈ DFU Command
◆ UUID: abcdbbbb-351f-712b-fc0e-a2345ce70038
◈ DFU Response
◆ UUID: abcdcccc-351f-712b-fc0e-a2345ce70038
◈ DFU Firmware
◆ UUID: abcddddd-351f-712b-fc0e-a2345ce70038
◈ prr123 Command/Data Service
Service UUID: a2b31111-5002-1234-2771-7bb9cfa2afbf
◈ prr123 Command
◆ UUID: a2b32222-5002-1234-2771-7bb9cfa2afbf
◈ prr123 ACK
◆ UUID: a2b33333-5002-1234-2771-7bb9cfa2afbf
◈ prr123 Log
◆ UUID: a2b34444-5002-1234-2771-7bb9cfa2afbf

@tom.chen2017

这些蓝牙数据从哪里得到的?有点看不懂。
除了 UUID 以外,还需要其他资料才能复制出一样的。

可以私下發mail提供文件嗎?

wyy via Realtek Ameba IOT Developers Forum (RTL8722 RTL8195 RTL8710 RTL8720 BW16 Development board) - IOT / MCU Solutions 瑞昱開發者論壇 開發板 开發者论坛 开發板 <notifications@ameba.discoursemail.com> 於 2023年2月20日 週一 上午11:54寫道:

@tom.chen2017

你可以通过 PM私信 把资料发我。