• Below is some code to make a Puck.js (or Pixl.js or MDBT42) act as a beacon to be tracked via Apple's massive Find My network ...act as an Apple AirTag equivalent if you will. Thus you can find your Puck.js if you lose it.

    An Apple Mac computer is required though.

    It is some Espruino code to mimic the beacon code of the OpenHaystack framework. It is based on Eric Betts original code.

    The code:

    /*
    OPENHAYSTACK
    A framework for tracking personal Bluetooth devices via Apple's massive Find My network
    2021-07-15 SK
    */
    print("OpenHaystack Beacon\n===================\n");
    
    const key_b64 = "Fy5SNl1ehMqUe49PJ4sxgrvTJLj/hlNjVYy5q1=­="; // replace with your public (advertisement) key
    
    function b64ToArray(str) {
        let bstr = atob(str);
        let arr = [];
        for (let i = 0; i < bstr.length; i++) {
            arr[i] = bstr.charCodeAt(i);
        }
        return arr;
    }
    
    const key = b64ToArray(key_b64); // public key
    
    print("Public key:");
    print("- base64:\n" + key_b64);
    print("- hexadecimal:\n" + key.map(x => x.toString(16).padStart(2, '0')).join(' '));
    
    const mac = [ key[0] | 0b11000000, key[1], key[2], key[3], key[4], key[5] ].map(x => x.toString(16).padStart(2, '0')).join(':'); // mac address
    
    print("\nMAC address:\n" + mac);
    
    const adv = [ 0x1e, 0xff, 0x4c, 0x00, 0x12, 0x19, 0x00, key[6], key[7], key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15], key[16], key[17], key[18], key[19], key[20], key[21], key[22], key[23], key[24], key[25], key[26], key[27], key[0] >> 6, 0x00 ]; // advertising packet
    
    print("\nAdvertising packet:\n" + adv.map(x => x.toString(16).padStart(2, '0')).join(' ') + "\n");
    
    NRF.setAddress(mac);
    NRF.setAdvertising(adv, {interval:5000});
    
    print("\nTo start beacon click on IDE upper-left icon to disconnect board. BLE stack will restart");
    print("Then reset (if code saved in RAM) / hard-reset (if code in Flash) board to reconnect IDE");
    print("To hard-reset hold BTN1 for >5s while repowering the board. Type reset(1) to erase Flash");
    

    ...yields:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v09 (c) 2021 G.Williams
    >
    OpenHaystack Beacon
    ===================
    
    Public key:
    - base64:
    Fy5SNl1ehMqUe49PJ4sxgrvTJLj/hlNjVYy5q1==­
    - hexadecimal:
    17 2e 52 36 5d 5e 84 ca 94 7b 8f 4f 27 8b 31 82 bb d3 24 b8 ff 86 53 63 55 8c b9 ab
    
    MAC address:
    d7:2e:52:36:5d:5e
    
    Advertising packet:
    1e ff 4c 00 12 19 00 84 ca 94 7b 8f 4f 27 8b 31 82 bb d3 24 b8 ff 86 53 63 55 8c b9 ab 00 00
    
    BLE Connected, queueing BLE restart for later
    
    To start beacon click on IDE upper-left icon to disconnect board. BLE stack will restart
    Then reset (if code saved in RAM) / hard-reset (if code in Flash) board to reconnect IDE
    To hard-reset hold BTN1 for >5s while repowering the board. Type reset(1) to erase Flash
    
About

Avatar for sebi @sebi started