Balaji Ramanathan
Balaji Ramanathan

Reputation: 19

Simple app to buzz the watch when it gets disconnected from phone

I would like a simple app that periodically checks the bluetooth connection between the phone and my watch (GTR 3 Pro), and buzzes the watch when it gets disconnected from my phone. This will be useful if I accidentally leave my phone somewhere and walk away from it, or my phone gets stolen or something like that.

Some previous amazfit watches had this feature built-in, but it doesn't seem to be available in my GTR 3 Pro right now.  Thank you.

Upvotes: 0

Views: 1618

Answers (2)

Leu
Leu

Reputation: 1433

You'll need to do a little hack to poll the Bluetooth connection to achieve the desired behavior, but first let's understand why.

Per ZeppOS architectural decision, your app will never run in background on the device. I believe this is for battery efficiency reasons or even available processing power.

With that in mind, We'll use hmApp.alarmNew and hmApp.alarmCancel in order to get it working, as follows:

  1. Create a new page that will be responsible for checking the bluetooth connection, something like page/connectionCheck.js and declare it in your app.json target (you can also use the default index.js if you want)
  2. In the onInit() of the page, register a new hmApp.alarm and cancel the existing ones if needed to avoid waking up the app unnecessarily
  3. In the build() call, verify if it's connected to the cellphone using the hmBle.connectStatus() and alert the user.

Summarizing, it'll look like this:
(I'm using zeppOS API v1.0 here to make it work on all devices)

const WAKE_UP_INTERVAL_SECONDS = 30 // this value must be higher than the screen on time on app
const POLL_ALARM_PREF_ID = 'my_bluetooth_poll_alarm'

const vibrate = hmSensor.createSensor(hmSensor.id.VIBRATE)

Page({
  onInit(param) {
    vibrate.stop() // stop any vibration
    vibrate.scene = 27 // set the vibration scene to 27 (1000ms vibration, high intensity)

    // verify if this launch was triggered by an alarm or not
    if(param === POLL_ALARM_PREF_ID) { 
      const existingAlarm = hmFS.SysProGetInt(POLL_ALARM_PREF_ID) // get existing alarm reference from system preferences
      if(existingAlarm) {
        // cancel existing alarm
        hmApp.alarmCancel(existingAlarm)
      }
    }

    // always create a new alarm to avoid alarm trigger while using the app
    const alarm = hmApp.alarmNew({
      file: 'pages/connectionCheck',
      appid: 123123, // <YOU APP ID HERE>
      delay: WAKE_UP_INTERVAL_SECONDS,
      param: POLL_ALARM_PREF_ID
    })

    hmFS.SysProSetInt(POLL_ALARM_PREF_ID, alarm) // Save new alarm reference on system preferences
  },
  build() {
    if(hmBle.connectStatus() === true) {
      // Do something if already connected, maybe return to the home screen, exit the program or even turn the sreen off
      hmApp.exit()
    } else {
      // show a message to the user / vibrate the watch
      vibrate.start()
    }
  },
  onDestroy() {
    vibrate && vibrate.stop() // stop any vibration
  }
})

Upvotes: 1

Pavel Aksyonov
Pavel Aksyonov

Reputation: 49

I made it, but only in ACTIVE application. So, if you open your mini-app, then it is possible to handle bluetooth status events (see the picture). Couldn't apply it in the background yet :-(.

enter image description here

Upvotes: 1

Related Questions