|
| 1 | +var bleno = require('bleno'); |
| 2 | +var eddystoneBeacon = require('eddystone-beacon'); |
| 3 | + |
| 4 | +/***********************Altering Library Functions**************************/ |
| 5 | +/** |
| 6 | + * In order to broadcast in the fatbeacon format, we override several |
| 7 | + * functions in the advertisement-data module of the eddystone-beacon library. |
| 8 | + */ |
| 9 | +var AdvertisementData = |
| 10 | + require('eddystone-beacon/lib/util/advertisement-data'); |
| 11 | +var Eir = require('eddystone-beacon/lib/util/eir'); |
| 12 | + |
| 13 | +var FAT_BEACON_FRAME_TYPE = 0x0e; |
| 14 | +var MAX_URL_LENGTH = 18; |
| 15 | +var SERVICE_UUID = 'feaa'; |
| 16 | + |
| 17 | +/** |
| 18 | + * this patch ensures that the correct Fatbeacon eir flag (0x06) is added |
| 19 | + * to the packet instead of the library standard flag. |
| 20 | + */ |
| 21 | +AdvertisementData.makeEirData = function(serviceData) { |
| 22 | + var eir = new Eir(); |
| 23 | + eir.addFlags(0x06); |
| 24 | + eir.add16BitCompleteServiceList([SERVICE_UUID]); |
| 25 | + eir.addServiceData(SERVICE_UUID, serviceData); |
| 26 | + return eir.buffer(); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * This patch alters the method signature to accept a name instead of a url. |
| 31 | + * It also encodes the name in hex instead of the libraries url encoding |
| 32 | + * format. Lastly, it adds the Fatbeacon header to the packet. |
| 33 | + */ |
| 34 | +AdvertisementData.makeUrlBuffer = function(name) { |
| 35 | + console.log(`AdvertisementData.makeUrlBuffer called with: ${name}`); |
| 36 | + |
| 37 | + var encodedName = Buffer.from(name); |
| 38 | + if (encodedName.length > MAX_URL_LENGTH) { |
| 39 | + throw new Error(`Encoded Name must be less than ${MAX_URL_LENGTH} bytes.` + |
| 40 | + ` It is currently ${encodedName.length} bytes.`); |
| 41 | + } |
| 42 | + |
| 43 | + var serviceData = Buffer.concat([ |
| 44 | + Buffer.from([0x10, 0xba, FAT_BEACON_FRAME_TYPE]), //FatBeacon Header |
| 45 | + encodedName |
| 46 | + ]); |
| 47 | + |
| 48 | + return AdvertisementData.makeEirData(serviceData); |
| 49 | +} |
| 50 | + |
| 51 | +/*********************************************************/ |
| 52 | + |
| 53 | +// Start Advertising name. |
| 54 | +eddystoneBeacon.advertiseUrl('New Fatbeacon'); |
0 commit comments