2024-05-18 16:24:33 +00:00
|
|
|
declare let exports: any;
|
2024-05-28 21:29:22 +00:00
|
|
|
//declare let BLE_DEBUG: undefined | true;
|
2024-05-18 16:24:33 +00:00
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
type BleAdvertObj = { [key: string | number]: number[] };
|
|
|
|
type BleAdvert = BleAdvertObj | number[];
|
2024-05-18 16:24:33 +00:00
|
|
|
type BangleWithAdvert = (typeof Bangle) & { bleAdvert?: BleAdvert | BleAdvert[]; };
|
|
|
|
type SetAdvertisingOptions = typeof NRF.setAdvertising extends (data: any, options: infer Opts) => any ? Opts : never;
|
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
const advertise = (options: SetAdvertisingOptions) => {
|
|
|
|
const clone = (obj: any): any => {
|
|
|
|
// just for our use-case
|
|
|
|
if(Array.isArray(obj)){
|
|
|
|
return obj.map(clone);
|
|
|
|
}else if(typeof obj === "object"){
|
|
|
|
const r = {};
|
|
|
|
for(const k in obj){
|
|
|
|
// @ts-expect-error implicitly
|
|
|
|
r[k] = clone(obj[k]);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// clone the object, to avoid firmware behaving like so:
|
|
|
|
// bleAdvert = [Uint8Array, { [0x180f]: ... }]
|
|
|
|
// ^ ^
|
|
|
|
// | |
|
|
|
|
// | +- added by this call
|
|
|
|
// +- modified from a previous setAdvertising()
|
|
|
|
//
|
|
|
|
// The firmware (jswrap_ble_setAdvertising) will convert arrays within
|
|
|
|
// the advertising array to Uint8Array, but if this has already been done,
|
|
|
|
// we get iterator errors. So we clone the object to avoid these mutations
|
|
|
|
// taking effect for later calls.
|
|
|
|
//
|
|
|
|
// This also allows us to identify previous adverts correctly by id.
|
|
|
|
NRF.setAdvertising(clone((Bangle as BangleWithAdvert).bleAdvert), options);
|
|
|
|
};
|
|
|
|
|
|
|
|
const manyAdv = (bleAdvert: BleAdvert | BleAdvert[] | undefined): bleAdvert is BleAdvert[] => {
|
|
|
|
return Array.isArray(bleAdvert) && typeof bleAdvert[0] === "object";
|
2024-05-18 15:17:51 +00:00
|
|
|
};
|
|
|
|
|
2024-05-18 16:24:33 +00:00
|
|
|
exports.set = (id: string | number, advert: number[], options?: SetAdvertisingOptions) => {
|
|
|
|
const bangle = Bangle as BangleWithAdvert;
|
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
if(manyAdv(bangle.bleAdvert)){
|
2024-05-18 16:24:33 +00:00
|
|
|
let found = false;
|
2024-05-28 21:29:22 +00:00
|
|
|
let obj;
|
2024-05-18 16:24:33 +00:00
|
|
|
for(let ad of bangle.bleAdvert){
|
2024-05-28 21:29:22 +00:00
|
|
|
if(Array.isArray(ad)) continue;
|
|
|
|
obj = ad;
|
2024-05-18 16:24:33 +00:00
|
|
|
if(ad[id]){
|
|
|
|
ad[id] = advert;
|
|
|
|
found = true;
|
2024-05-28 21:29:22 +00:00
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`bleAdvert is array, found existing entry for ${id}, replaced`)
|
2024-05-18 16:24:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-05-27 10:37:12 +00:00
|
|
|
if(!found){
|
2024-05-28 21:29:22 +00:00
|
|
|
if(obj)
|
|
|
|
obj[id] = advert;
|
|
|
|
else
|
|
|
|
bangle.bleAdvert.push({ [id]: advert });
|
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`bleAdvert is array, no entry for ${id}, created`)
|
2024-05-27 10:37:12 +00:00
|
|
|
}
|
2024-05-18 16:24:33 +00:00
|
|
|
}else if(bangle.bleAdvert){
|
2024-05-28 21:29:22 +00:00
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`bleAdvert is object, ${id} entry ${id in bangle.bleAdvert ? "replaced" : "created"}`);
|
|
|
|
if(Array.isArray(bangle.bleAdvert)){
|
|
|
|
bangle.bleAdvert = [bangle.bleAdvert, { [id]: advert }];
|
|
|
|
}else{
|
|
|
|
bangle.bleAdvert[id] = advert;
|
|
|
|
}
|
2024-05-18 16:24:33 +00:00
|
|
|
}else{
|
2024-05-28 21:29:22 +00:00
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`bleAdvert not present, created`);
|
|
|
|
bangle.bleAdvert = { [id]: advert };
|
2024-05-18 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`NRF.setAdvertising({ ${Object.keys(bangle.bleAdvert).join(", ")} }, ${JSON.stringify(options)})`);
|
2024-05-27 10:37:12 +00:00
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
advertise(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.push = (adv: number[], options?: SetAdvertisingOptions) => {
|
|
|
|
const bangle = Bangle as BangleWithAdvert;
|
|
|
|
|
|
|
|
if(manyAdv(bangle.bleAdvert)){
|
|
|
|
bangle.bleAdvert.push(adv);
|
|
|
|
}else if(bangle.bleAdvert){
|
|
|
|
bangle.bleAdvert = [bangle.bleAdvert, adv];
|
|
|
|
}else{
|
|
|
|
// keep a second entry for normal/original advertising as well as this extra one
|
|
|
|
bangle.bleAdvert = [adv, {}];
|
|
|
|
}
|
|
|
|
|
|
|
|
advertise(options);
|
2024-05-18 16:24:33 +00:00
|
|
|
};
|
|
|
|
|
2024-05-28 21:36:04 +00:00
|
|
|
/*
|
2024-05-18 16:24:33 +00:00
|
|
|
exports.remove = (id: string | number, options?: SetAdvertisingOptions) => {
|
|
|
|
const bangle = Bangle as BangleWithAdvert;
|
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
// if(typeof BLE_DEBUG !== "undefined")
|
|
|
|
// console.log(`ble_advert.remove(${id}, ${JSON.stringify(options)})`);
|
2024-05-27 10:37:12 +00:00
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
if(manyAdv(bangle.bleAdvert)){
|
2024-05-18 16:24:33 +00:00
|
|
|
let i = 0;
|
|
|
|
for(const ad of bangle.bleAdvert){
|
2024-05-28 21:29:22 +00:00
|
|
|
if(Array.isArray(ad)) continue;
|
2024-05-18 16:24:33 +00:00
|
|
|
if(ad[id]){
|
|
|
|
delete ad[id];
|
|
|
|
let empty = true;
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for(const _ in ad){
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(empty) bangle.bleAdvert.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}else if(bangle.bleAdvert){
|
2024-05-28 21:29:22 +00:00
|
|
|
if(!Array.isArray(bangle.bleAdvert))
|
|
|
|
delete bangle.bleAdvert[id];
|
2024-05-18 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:29:22 +00:00
|
|
|
advertise(options);
|
2024-05-18 16:24:33 +00:00
|
|
|
};
|
2024-05-28 21:36:04 +00:00
|
|
|
*/
|