Is it possible to sync Bangle.js 2 time to smartphone time?

Posted on
Page
of 5
/ 5
Last Next
  • Is it possible to automatically sync Bangle.js 2 time to smartphone time? Either by somehow requesting smartphone time from Bangle.js 2, or by "pushing" time from smartphone to Bangle.js 2 ?

    (I'm not interested syncing to GPS time as GPS doesn't work indoors, and internet access isn't implemented yet so I can't sync to my webserver yet.)

  • Are you using an Android device and Gadgetbridge? If so, check the Gadgetbridge settings for "Sync time".

    If you're using an iPhone I have no idea.

  • Also in App loader webpage there is option to sync time when connecting - the rightmost settigs tab.

  • According to [1] Bangle.js 2 doesn't support getting time from Gadgetbridge.

    Also [2] says that "sync time" only syncs when watch gets connected to phone, which is not enough. Bangle.js 2 is quite inaccurate and needs sync several times per day.

    [1] https://www.espruino.com/Gadgetbridge#ho­w-it-works-internally
    [2] https://codeberg.org/Freeyourgadget/Gadg­etbridge/wiki/Configuration

  • Yes but that is not automatic.

  • That page doesn't mention anything about syncing time, as far as I can see. But maybe that's what you're basing your assumption on.

    I have not seen that Bangle.js 2 is that inaccurate... But of course, I do a lot of tweaking and often end up disconnecting and connecting many times during a day. Will have to test that at some point.

  • That page doesn't mention anything about syncing time, as far as I can see. ...

    Yes, it lists what is supported - time is not listed so it's not supported.

    I have not seen that Bangle.js 2 is that inaccurate...

    For example I synced my Bangle.js 2 yesterday evening and it had <1s error. Currently error is already over 3s and it's been well under 24 hours. (Acceptable error is 2s at max, but preferably well under 1s.)

  • Ah, but syncing time is not sent as a message and messages is what you see listed on that page. I agree that it might be good to have it mentioned in there somewhere, but I wouldn't say that the page says that syncing time isn't supported.

    About having syncing occurring at other times than when connecting, I leave that to @Gordon...

  • Bangle.js 2 is quite inaccurate and needs sync several times per day.

    Currently error is already over 3s and it's been well under 24 hours.

    this is something that can be hopefully improved
    bluetooth uses 32kHz crystal with 20PPM accuracy https://github.com/espruino/Espruino/blo­b/master/targets/nrf5x/bluetooth.c#L2401­
    that means according to https://www.best-microcontroller-project­s.com/ppm.html accuracy of 1.73 seconds per day. I think the RTC that can keep time can use this crystal as a clock source too. Not sure if it currently does and where currently the time value comes from. So unless you reboot the watch which spends some unaccounted time in bootloader (where the hardware including RTC may be reset and disabled) it should/could be accurate.

    EDIT: here https://github.com/espruino/Espruino/blo­b/master/targets/nrf5x/jshardware.c#L100­6 it looks like the same time source is used so not sure why it would be so inaccurate (unless you rebooted the watch)

  • @Numerist has also mentioned problems with accuracy of the clock. I am struggling to find the thread, but the fact that he wanted to use GPS time may have been a red herring/distraction from whatever @malaire is also seeing.

  • I might've reset the watch, I'm not sure. I'll have to recheck once I get my watch to work with Bluetooth again.

    But even that 1.73 seconds per day would accumulate to quite large error since Gadgetbridge doesn't even have daily sync.

  • But even that 1.73 seconds per day would accumulate to quite large error

    Well, it is also temperature dependent but as long as you wear the watch the temperature may not drift a lot. So if temperature is stable the daily drift of your specific crystal can be constant (?) so it could be compensated in software. This of course matters mainly if you cannot sync with phone/internet.

    BTW there is also BLE service for time defined so one could have some accurate time source around (e.g. ESP32 connected to internet adverting this over BLE) and then have app on bangle to scan for it and sync the time.

  • BTW there is also BLE service for time defined so one could have some accurate time source around (e.g. ESP32 connected to internet adverting this over BLE) and then have app on bangle to scan for it and sync the time.

    Wouldn't I need to manually unpair Bangle.js 2 from my phone, pair to that timesource, then unpair from timesource and pair with phone?

  • No if bangle is the active one in this, currently you can have one connection into Bangle and one from Bangle to some other device. They are unrelated.

    Also BTW every recent iphone have such service implemented (as per 40.12.4 Available Services )

    But this could be also done over advertisements so no connection would be needed, just scan for devices around and get the data directly from the packet. However that would be custom solution but relatively easy to do on both sides. maybe even esp32 board running espruino could do such current time advertisement (while syncing with NTP servers over wifi)

  • @myownself
    It's possible that on Bangle.js 2, the time gains too much. There are variables still to test, and it's still possible that the code I'm using for a very specialized clock (that no one else uses) is the problem. However, overnight the well-known Anton Clock seems to have gained a lot also. Temperature is not the factor here.

    I'm interested to know whether others have the gaining problem that @malaire and I have noticed.

    Gaining 2 seconds a day is okay with me only if I have a way to automatically correct that daily. At present the only way I know is to reload my watch script manually, although just now even that didn't work.

    If I could sync to an iPhone, I'd be all set. I'm not aware that's possible.

    I'm not a coder and know very little of javascript or about Bangle.js. I still don't know whether on Bangle.js 2 the GPS time, once set outside, still works inside, and whether I can access it continually. That seemed to solve my time problem on Bangle.js 1.

  • hmm, ok, I thought I read somewhere that Bangle.js 2 only supports one Bluetooth connection at a time.

    I wouldn't mind a custom solution. I'm a programmer and my Debian 11 desktop computer is synced to NTP and has Bluetooth dongle, but I don't know enough of Bluetooth to try implementing something like that.

  • hmm, ok, I thought I read somewhere that Bangle.js 2 only supports one Bluetooth connection at a time.

    That's correct.

    1. Does GPS time run on Bangle.js 2 indoors after being connected outdoors?

    2. Is it possible to sync with an iPhone's time? Automatically once or twice a day, if possible.

  • Yeah, I got same idea. Even tried to write such time announcement service in Linux. But low experience with BLE and shortage of time holds me...

  • I can't resist trying to learn something new, so I managed to implement quick&dirty BLE time broadcast for Debian 11.

    Broadcast side (bash, bluez), one broadcast per second for 60 seconds:

    sudo hciconfig hci0 leadv 3
    for (( n=1; n<60; n++ ))
    do
        time=$(date +%s)
        hex=$(printf '%010X' $time)
        echo $hex
        sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF FF FF \
            ${hex:0:2} ${hex:2:2} ${hex:4:2} ${hex:6:2} ${hex:8:2} \
            00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
        sleep 1
    done
    sudo hciconfig hci0 noleadv
    

    Receiver side, received_time is Unix timestamp in seconds:

    packets=100;
    NRF.setScan(function(d) {
      packets--;
      if (packets == 0) {
        NRF.setScan();
      } else if (d.manufacturer == 65535) {
        let received_time = 0;
        for (let i = 0; i < 5; i++) {
          received_time <<= 8;
          received_time += d.manufacturerData[i];
        }
      }
    });
    
  • Nice quick work.
    I'm curious though - does doing something like this actually have the kind of accuracy you are after, or is it just adding a different offset? (maybe it is better, I have no idea whatsoever)

  • Currently accuracy is quite bad - that sudo hcitool sets up repeated broadcast which I change every second, so accuracy is about 1 second here.

    To get better accuracy I'd need to learn how to send broadcast at once without delay. BLE seems to have inherent random delay of around ~10 ms so e.g. 1 ms accuracy is not possible, but I think that accuracy of <100ms should be possible.

  • btw, there is a Python BlueZ example here and Bangle.js 2 does see those messages, so that would probably be better base (no need for bash or sudo), but I don't really know python so I didn't try modifying that code.

  • Hi, the code below should work to send the time to the device: it opens the connection, sends the command to update date, and then closes the connection.

    import asyncio
    from time import time
    from bleak import BleakClient
    
    
    UUID_NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
    UUID_NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
    
    
    async def main(address):
        buffer_size = 20
    
        print(f"Connecting to {address}...")
    
        async def send_command(command):
            c = command
            while len(c) > 0:
                await client.write_gatt_char(UUID_NORDIC_TX, bytearray(c[0:buffer_size]), True)
                c = c[buffer_size:]
    
        async with BleakClient(address) as client:
            print("Connected")
            print(f"Time: {time()}")
            await send_command(bytes(f"setTime({time()});\­n", "utf-8"))
    
    asyncio.run(main("d8:ee:7c:31:a9:c0"))
    

    I'm using something similar to this to periodically sync weather data and my personal agenda with the device

  • Does that require that Bangle.js 2 is not connected to phone or computer, as it can handle only one Bluetooth connection at a time?

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

Is it possible to sync Bangle.js 2 time to smartphone time?

Posted by Avatar for malaire @malaire

Actions