how to auto reconnect bangle?

Posted on
  • Bangle is so interesting, and I using it to get realtime emotional data(from HRM Sensor). but have some problems for stable connection. commonly after 3-5 minutes, bangles connection disconnected.
    so I wanna make a code automatically reconnect with no popup window of BLE connection when it disconnected. can you help me?

  • What are you connecting to the Bangle from? What OS/what programming language?

    If you're talking about Web Bluetooth then reconnection without a popup is possible, but the Puck.js library doesn't do that automatically. You could however just use Web Bluetooth directly. They even have a reconnect example at https://googlechrome.github.io/samples/w­eb-bluetooth/automatic-reconnect.html

  • Hi Gordon! my OS is Window10, and programming language is Javascript.
    I refer to this example, my frontend framework is Vue.js. I share some of codes of page.

       bangleCode: `Bangle.setLCDPower(1);
            Bangle.setLCDTimeout(0);
            Bangle.setHRMPower(1);
            var hrmInfo, hrmOffset = 0;
            var hrmInterval;
            var btm = g.getHeight()-1;
            
            function onHRM(h) {
              if (counter!==undefined) {
                // the first time we're called remove
                // the countdown
                counter = undefined;
                g.clear();
              }
              hrmInfo = h;
              /* On 2v09 and earlier firmwares the only solution for realtime
              HRM was to look at the 'raw' array that got reported. If you timed
              it right you could grab the data pretty much as soon as it was written.
              In new firmwares, '.raw' is not available. */
              if (hrmInterval) clearInterval(hrmInterval);
              hrmInterval = undefined;
              if (hrmInfo.raw) {
                hrmOffset = 0;
                setTimeout(function() {
                  hrmInterval = setInterval(readHRM,41);
                }, 40);
              }
            
              var px = g.getWidth()/2;
              g.setFontAlign(0,0);
              g.clearRect(0,24,239,80);
              g.setFont("6x8").drawString("Confidence "+hrmInfo.confidence+"%", px, 75);
              var str = hrmInfo.bpm;
              g.setFontVector(40).drawString(str,px,45­);
              px += g.stringWidth(str)/2;
              g.setFont("6x8");
              g.drawString("BPM",px+15,45);
            }
            Bangle.on('HRM', onHRM);
            /* On newer (2v10) firmwares we can subscribe to get
            HRM events as they happen */
            Bangle.on('HRM-raw', function(v) {
              hrmOffset++;
              if (hrmOffset>g.getWidth()) {
                hrmOffset=0;
                g.clearRect(0,80,239,239);
                g.moveTo(-100,0);
              }  
              y = E.clip(btm-v.filt/4,btm-10,btm);  
              g.setColor(1,0,0).fillRect(hrmOffset,btm­, hrmOffset, y);
              y = E.clip(170 - (v.raw/2),80,btm);
              g.setColor(g.theme.fg).lineTo(hrmOffset,­ y);
              if (counter !==undefined) {
                counter = undefined;
                g.clear();
              }
            });
            
            // It takes 5 secs for us to get the first HRM event
            var counter = 5;
            function countDown() {
              if (counter) {
                g.drawString(counter--,g.getWidth()/2,g.­getHeight()/2, true);
                setTimeout(countDown, 1000);
              }
            }
            g.clear().setFont("6x8",2).setFontAlign(­0,0);
            g.drawString("Please wait...",g.getWidth()/2,g.getHeight()/2 - 16);
            countDown();        
            
            var wasHigh = 0, wasLow = 0;
            var lastHigh = getTime();
            var hrmList = [];
            var hrmInfo;
            
            function readHRM() {
              if (!hrmInfo) return;
            
              if (hrmOffset==0) {
                g.clearRect(0,100,239,239);
                g.moveTo(-100,0);
              }
              for (var i=0;i<2;i++) {
                var hrmData = hrmInfo.raw[hrmOffset];
                hrmOffset++;
                y = E.clip(170 - (hrmData*2),100,230);
                g.setColor(g.theme.fg).lineTo(hrmOffset,­ y);
                var print = ["HRM", hrmData ];
                 Bluetooth.println(
                   print.join(",")
                   );
              }
            }
            Bangle.on('accel',function(a) {
              var d = [
                "ACC",
                Math.round(a.x*100),
                Math.round(a.y*100),
                Math.round(a.z*100)
                ];
              Bluetooth.println(d.join(","));
            })
            `,
    
    connectWatch1(state) {
                let connection1;
                // disconnect if connected already
                if (connection1) {
                    connection1.close();
                    connection1 = undefined;
                    state.watch1.connect = false
                }
    
                // Connect
                Puck.connect(function(c) {
                    if (!c) {
                        // alert("Couldn't connect!");
                        // return;
                        state.watch1.connect = false
                        this.connectWatch1()
                    }
                    connection1 = c;
                    state.watch1.connect = true
                        // console.log(connection)
                        // Handle the data we get back, and call 'onLine'
                        // whenever we get a line
                    var buf = "";
                    connection1.on("data", function(d) {
                        buf += d;
                        var l = buf.split("\n");
                        buf = l.pop();
                        l.forEach(
                            (line) => {
                                var d = line.split(',')
                                if (d[0] == 'ACC') {
                                    // we have an accelerometer reading
                                    state.watch1.accelX = parseInt(d[1]) / 100
                                    state.watch1.accelY = parseInt(d[2]) / 100
                                    state.watch1.accelZ = parseInt(d[3]) / 100
                                } else if (d[0] == 'HRM') {
                                    // we have an accelerometer reading
                                    state.watch1.hrm = parseInt(d[1])
                                }
    
                                // console.log("RECEIVED:" + state.watch1);
                            }
                            // console.log('RECEIVED:' + line)
                        );
                    });
                    // First, reset the Bangle
                    connection1.write("reset();\n", function() {
                        // Wait for it to reset itself
                        setTimeout(function() {
                            // Now upload our code to it
                            connection1.write(
                                "\x03\x10if(1){" +
                                state.bangleCode +
                                "}\n",
                                function() {
                                    console.log("Ready...");
                                }
                            );
                        }, 1500);
                    });
                });
            },
    

    thank you for your good source of reconnect!😀

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

how to auto reconnect bangle?

Posted by Avatar for user133052 @user133052

Actions