ble_advert: handle Advert[] and number[] entries
parent
bcc5ae9a15
commit
cc0f1a315e
|
|
@ -1,59 +1,27 @@
|
||||||
declare let exports: any;
|
declare let exports: any;
|
||||||
declare let BLE_DEBUG: undefined | true;
|
//declare let BLE_DEBUG: undefined | true;
|
||||||
|
|
||||||
type BleAdvert = { [key: string | number]: number[] };
|
type BleAdvertObj = { [key: string | number]: number[] };
|
||||||
|
type BleAdvert = BleAdvertObj | number[];
|
||||||
type BangleWithAdvert = (typeof Bangle) & { bleAdvert?: BleAdvert | BleAdvert[]; };
|
type BangleWithAdvert = (typeof Bangle) & { bleAdvert?: BleAdvert | BleAdvert[]; };
|
||||||
type SetAdvertisingOptions = typeof NRF.setAdvertising extends (data: any, options: infer Opts) => any ? Opts : never;
|
type SetAdvertisingOptions = typeof NRF.setAdvertising extends (data: any, options: infer Opts) => any ? Opts : never;
|
||||||
|
|
||||||
const clone = (obj: any): any => {
|
const advertise = (options: SetAdvertisingOptions) => {
|
||||||
// just for our use-case
|
const clone = (obj: any): any => {
|
||||||
if(Array.isArray(obj)){
|
// just for our use-case
|
||||||
return obj.map(clone);
|
if(Array.isArray(obj)){
|
||||||
}else if(typeof obj === "object"){
|
return obj.map(clone);
|
||||||
const r = {};
|
}else if(typeof obj === "object"){
|
||||||
for(const k in obj)
|
const r = {};
|
||||||
// @ts-expect-error implicitly
|
for(const k in obj){
|
||||||
r[k] = clone(obj[k]);
|
// @ts-expect-error implicitly
|
||||||
return r;
|
r[k] = clone(obj[k]);
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.set = (id: string | number, advert: number[], options?: SetAdvertisingOptions) => {
|
|
||||||
const bangle = Bangle as BangleWithAdvert;
|
|
||||||
|
|
||||||
if(Array.isArray(bangle.bleAdvert)){
|
|
||||||
let found = false;
|
|
||||||
for(let ad of bangle.bleAdvert){
|
|
||||||
if(ad[id]){
|
|
||||||
ad[id] = advert;
|
|
||||||
found = true;
|
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
|
||||||
console.log(`bleAdvert is array, found existing entry for ${id}, replaced`)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
if(!found){
|
return obj;
|
||||||
bangle.bleAdvert.push({ [id]: advert });
|
};
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
|
||||||
console.log(`bleAdvert is array, no entry for ${id}, created`)
|
|
||||||
}
|
|
||||||
}else if(bangle.bleAdvert){
|
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
|
||||||
console.log(`bleAdvert is object, ${id} entry ${id in bangle.bleAdvert ? "replaced" : "created"}`);
|
|
||||||
bangle.bleAdvert[id] = advert;
|
|
||||||
}else{
|
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
|
||||||
console.log(`bleAdvert not present, created`);
|
|
||||||
bangle.bleAdvert = {
|
|
||||||
[id]: advert,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
|
||||||
console.log(`NRF.setAdvertising({ ${Object.keys(bangle.bleAdvert).join(", ")} }, ${JSON.stringify(options)})`);
|
|
||||||
|
|
||||||
NRF.setAdvertising(clone(bangle.bleAdvert), options);
|
|
||||||
// clone the object, to avoid firmware behaving like so:
|
// clone the object, to avoid firmware behaving like so:
|
||||||
// bleAdvert = [Uint8Array, { [0x180f]: ... }]
|
// bleAdvert = [Uint8Array, { [0x180f]: ... }]
|
||||||
// ^ ^
|
// ^ ^
|
||||||
|
|
@ -67,17 +35,83 @@ exports.set = (id: string | number, advert: number[], options?: SetAdvertisingOp
|
||||||
// taking effect for later calls.
|
// taking effect for later calls.
|
||||||
//
|
//
|
||||||
// This also allows us to identify previous adverts correctly by id.
|
// 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";
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.set = (id: string | number, advert: number[], options?: SetAdvertisingOptions) => {
|
||||||
|
const bangle = Bangle as BangleWithAdvert;
|
||||||
|
|
||||||
|
if(manyAdv(bangle.bleAdvert)){
|
||||||
|
let found = false;
|
||||||
|
let obj;
|
||||||
|
for(let ad of bangle.bleAdvert){
|
||||||
|
if(Array.isArray(ad)) continue;
|
||||||
|
obj = ad;
|
||||||
|
if(ad[id]){
|
||||||
|
ad[id] = advert;
|
||||||
|
found = true;
|
||||||
|
// if(typeof BLE_DEBUG !== "undefined")
|
||||||
|
// console.log(`bleAdvert is array, found existing entry for ${id}, replaced`)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!found){
|
||||||
|
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`)
|
||||||
|
}
|
||||||
|
}else if(bangle.bleAdvert){
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// if(typeof BLE_DEBUG !== "undefined")
|
||||||
|
// console.log(`bleAdvert not present, created`);
|
||||||
|
bangle.bleAdvert = { [id]: advert };
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(typeof BLE_DEBUG !== "undefined")
|
||||||
|
// console.log(`NRF.setAdvertising({ ${Object.keys(bangle.bleAdvert).join(", ")} }, ${JSON.stringify(options)})`);
|
||||||
|
|
||||||
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.remove = (id: string | number, options?: SetAdvertisingOptions) => {
|
exports.remove = (id: string | number, options?: SetAdvertisingOptions) => {
|
||||||
const bangle = Bangle as BangleWithAdvert;
|
const bangle = Bangle as BangleWithAdvert;
|
||||||
|
|
||||||
if(typeof BLE_DEBUG !== "undefined")
|
// if(typeof BLE_DEBUG !== "undefined")
|
||||||
console.log(`ble_advert.remove(${id}, ${JSON.stringify(options)})`);
|
// console.log(`ble_advert.remove(${id}, ${JSON.stringify(options)})`);
|
||||||
|
|
||||||
if(Array.isArray(bangle.bleAdvert)){
|
if(manyAdv(bangle.bleAdvert)){
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for(const ad of bangle.bleAdvert){
|
for(const ad of bangle.bleAdvert){
|
||||||
|
if(Array.isArray(ad)) continue;
|
||||||
if(ad[id]){
|
if(ad[id]){
|
||||||
delete ad[id];
|
delete ad[id];
|
||||||
let empty = true;
|
let empty = true;
|
||||||
|
|
@ -92,8 +126,9 @@ exports.remove = (id: string | number, options?: SetAdvertisingOptions) => {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}else if(bangle.bleAdvert){
|
}else if(bangle.bleAdvert){
|
||||||
delete bangle.bleAdvert[id];
|
if(!Array.isArray(bangle.bleAdvert))
|
||||||
|
delete bangle.bleAdvert[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
NRF.setAdvertising(bangle.bleAdvert, options);
|
advertise(options);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue