mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-25 03:29:58 -07:00
JS Blebeacon keep/not prev cfg, use arraybuf, more options
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
let bleBeacon = require("blebeacon");
|
||||
let blebeacon = require("blebeacon");
|
||||
let math = require("math");
|
||||
|
||||
let currentIndex = 0;
|
||||
let currentByteValue = 0;
|
||||
let watchValues = [
|
||||
0x1A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0A, 0x0B, 0x0C, 0x11, 0x12, 0x13, 0x14, 0x15,
|
||||
@@ -9,50 +9,15 @@ let watchValues = [
|
||||
0x20, 0xEC, 0xEF
|
||||
];
|
||||
|
||||
function byteToHex(byte) {
|
||||
let hexChars = '0123456789abcdef';
|
||||
let hex = '';
|
||||
if (byte >= 0 && byte <= 255) {
|
||||
hex = hexChars[(byte >> 4) & 0x0F] + hexChars[byte & 0x0F];
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
function getNextByteValue() {
|
||||
let value = currentByteValue;
|
||||
currentByteValue = (currentByteValue + 1) % 256;
|
||||
return value;
|
||||
}
|
||||
|
||||
function generateRandomMac() {
|
||||
let mac = '';
|
||||
let mac = [];
|
||||
for (let i = 0; i < 6; i++) {
|
||||
if (mac.length) mac += ':';
|
||||
let byte = getNextByteValue();
|
||||
mac += byteToHex(byte);
|
||||
mac.push(math.floor(math.random() * 256));
|
||||
}
|
||||
return mac;
|
||||
}
|
||||
|
||||
function bytesToHexString(bytes) {
|
||||
if (!bytes) {
|
||||
print("Invalid input for bytesToHexString");
|
||||
return '';
|
||||
}
|
||||
|
||||
let hexString = '';
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
hexString += byteToHex(bytes[i]);
|
||||
}
|
||||
return hexString;
|
||||
return Uint8Array(mac);
|
||||
}
|
||||
|
||||
function sendRandomModelAdvertisement() {
|
||||
if (!watchValues || watchValues.length === 0) {
|
||||
print("watchValues array is empty or undefined.");
|
||||
return;
|
||||
}
|
||||
|
||||
let model = watchValues[currentIndex];
|
||||
|
||||
let packet = [
|
||||
@@ -60,28 +25,27 @@ function sendRandomModelAdvertisement() {
|
||||
model
|
||||
];
|
||||
|
||||
let packetString = bytesToHexString(packet);
|
||||
if (!packetString) {
|
||||
print("Failed to generate packet string.");
|
||||
return;
|
||||
}
|
||||
let intervalMs = 50;
|
||||
|
||||
bleBeacon.setMac(generateRandomMac());
|
||||
bleBeacon.setData(packetString);
|
||||
bleBeacon.send();
|
||||
// Power level, min interval and max interval are optional
|
||||
blebeacon.setConfig(generateRandomMac(), 0x1F, intervalMs, intervalMs * 3);
|
||||
|
||||
blebeacon.setData(Uint8Array(packet));
|
||||
|
||||
blebeacon.start();
|
||||
|
||||
print("Sent data for model ID " + to_string(model));
|
||||
|
||||
currentIndex = (currentIndex + 1) % watchValues.length;
|
||||
|
||||
delay(500);
|
||||
delay(intervalMs);
|
||||
|
||||
bleBeacon.stop();
|
||||
|
||||
bleBeacon
|
||||
blebeacon.stop();
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Make sure it resets at script exit, true will keep advertising in background
|
||||
blebeacon.keepAlive(true);
|
||||
|
||||
while (true) {
|
||||
sendRandomModelAdvertisement();
|
||||
}
|
||||
Reference in New Issue
Block a user