Yes, I received a couple XIAO MG24’s.
I plugged one in yesterday and ran the ble_scan example and that’s when I learned about the BLE (Silabs) protocol stack, which was shocking.
I use Arduino and the ArduinoBLE library - that’s where I started.
Please don’t laugh too hard, but here is the code I eventually ended up with for the ESP32-S3…
#include <Wire.h>
#include <ArduinoBLE.h>
uint8_t probeStatusData[50] = {};
bool CPTscanning = false;
bool CPTdiscovered = false;
bool CPTsubscribed = false;
void I2C_TxHandler(void) {
Wire.slaveWrite(probeStatusData, 50);
}
void setup() {
Wire.onRequest(I2C_TxHandler);
Wire.begin(0x42, 5, 6, 100000UL);
BLE.begin();
BLE.setEventHandler(BLEDiscovered, CPTdiscoveredHandler);
}
void loop() {
if (!CPTscanning) {
BLE.scanForAddress("xx:xx:xx:xx:xx:xx");
CPTscanning = true;
}
BLE.poll();
delay(100);
}
void readCPTvalue(BLECharacteristic characteristic) {
characteristic.read();
characteristic.readValue(&probeStatusData, 50);
}
void CPTdiscoveredHandler(BLEDevice peripheral) {
BLE.stopScan();
CPTscanning = false;
peripheral.connect();
while (!CPTdiscovered) {
if (peripheral.discoverService("00000100-caab-3792-3d44-97ae51c1407a")) {
CPTdiscovered = true;
} else {
peripheral.disconnect();
peripheral.connect();
delay(100);
}
}
BLEService service = peripheral.service("00000100-caab-3792-3d44-97ae51c1407a");
BLECharacteristic characteristic = service.characteristic("00000101-caab-3792-3d44-97ae51c1407a");
if (characteristic.canRead()) {
characteristic.read();
}
if (characteristic.canSubscribe()) {
while (!CPTsubscribed) {
if (characteristic.subscribe()) {
CPTsubscribed = true;
} else {
delay(500);
characteristic.subscribe();
delay(500);
}
}
}
while (peripheral.connected()) {
BLEService service = peripheral.service("00000100-caab-3792-3d44-97ae51c1407a");
BLECharacteristic characteristic = service.characteristic("00000101-caab-3792-3d44-97ae51c1407a");
if (characteristic.valueUpdated()) {
readCPTvalue(service.characteristic("00000101-caab-3792-3d44-97ae51c1407a"));
}
}
CPTdiscovered = false;
CPTsubscribed = false;
}
I am sure that is a long way from optimal, but it works.
From what I learned so far the MG24 does not allow me to scan for a specific MAC address.
I went to this website https://docs.silabs.com/bluetooth/latest/bluetooth-stack-api/sl-bt-gatt and tried to understand it, but I am not a programmer and have very little experience.
This is the start of the code from what I could gather…
//sscanf(macAddress, "%X:%X:%X:%X:%X:%X", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
uint8_t target_mac[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
void setup() {
// Start scanning (example: 1M PHY, generic discovery mode)
sl_bt_scanner_start(sl_bt_scanner_scan_phy_1m, sl_bt_scanner_discover_generic);
}
void loop() {
//
}
// In your event handler:
void handle_scanner_event(sl_bt_msg_t *evt) {
if (SL_BT_MSG_ID(evt->header) == sl_bt_evt_scanner_legacy_advertisement_report_id) {
// Compare evt->data.evt_scanner_legacy_advertisement_report.address to your target MAC
if (memcmp(evt->data.evt_scanner_legacy_advertisement_report.address.addr, target_mac, 6) == 0) {
// Found the device
}
}
}
// Enable notifications for a characteristic
void enable_notifications(uint8_t connection, uint16_t characteristic_handle) {
sl_status_t sc = sl_bt_gatt_set_characteristic_notification(connection,
characteristic_handle,
sl_bt_gatt_notification);
// Check status code
}
// Handle the characteristic value event (when notifications arrive)
void handle_characteristic_value(sl_bt_msg_t *evt) {
if (evt->data.evt_gatt_characteristic_value.att_opcode == sl_bt_gatt_handle_value_notification) {
// Process the notification data
// The notification data is in evt->data.evt_gatt_characteristic_value.value
}
}
No idea if that is the right direction or not.
Or if I need something called ‘sscanf’ to define the MAC address?
Any advice you have would be very greatly appreciated.
Thanks