Open
Description
Hi!
I tried to flash my xiao ble with some firmware which I found on github, but none of them worked with this uc. I had success with esp32, but in my opinion this is rather poor choice, because of high battery consumption.
Can you tell me where is the bug in my code? I tried almost anything, but cant make it working... Microcontoler is advertising, but can't be picked up by nearby devices.. I didn't include key shuffling, cause I wanted to have bare minimum code which actually works. Here is my code:
#include <bluefruit.h>
#include <cstring>
#define ADVERTISING_CYCLE_DURATION_MS 1000
#define ADVERTISING_INTERVAL_MS 20
#define LIGHT_SLEEP_INTERVAL_MS (ADVERTISING_CYCLE_DURATION_MS - ADVERTISING_INTERVAL_MS)
#define ADVERTISING_INTERVAL_TICKS 32 // 20ms
static uint8_t public_key[28] = { /* here Advertisement key converted from base64 to hex? */};
static uint8_t adv_payload[] = {
0x1e, /* Length (30) */
0xff,
0x4c, 0x00, /* Company ID (Apple) */
0x12, 0x19, /* Offline Finding type and length */
0x00, /* State */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, /* First two bits */
0x00, /* Hint (0x00) */
};
void setBleAddress() {
uint8_t addr[6];
addr[0] = public_key[0] | 0b11000000;
addr[1] = public_key[1];
addr[2] = public_key[2];
addr[3] = public_key[3];
addr[4] = public_key[4];
addr[5] = public_key[5];
ble_gap_addr_t gap_addr;
// Set the address type (example: public address)
gap_addr.addr_type = 0x01; //this was in esp32: BLE_ADDR_TYPE_RANDOM = 0x01
// Copy the address into the structure
std::memcpy(gap_addr.addr, addr, BLE_GAP_ADDR_LEN);
// Set the address using the setAddr method
Bluefruit.setAddr(&gap_addr);
}
void setPayloadFromPubKey() {
/* copy last 22 bytes */
memcpy(&adv_payload[7], &public_key[6], 22);
/* append two bits of public key */
adv_payload[29] = public_key[0] >> 6;
}
void setup() {
Serial.begin(115200);
setPayloadFromPubKey();
setBleAddress();
while (!Bluefruit.begin()) {
Serial.println("Retrying...");
delay(500);
}
Bluefruit.Advertising.setType(BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED);
Bluefruit.Advertising.setInterval(ADVERTISING_INTERVAL_TICKS, ADVERTISING_INTERVAL_TICKS);
// Set advertising payload
Bluefruit.Advertising.setData(adv_payload, sizeof(adv_payload));
}
void loop() {
Bluefruit.Advertising.start();
delay(ADVERTISING_INTERVAL_MS);
Bluefruit.Advertising.stop();
delay(LIGHT_SLEEP_INTERVAL_MS);
}