Migrating a GadgetBridge installation to a new device

Introduction

GadgetBridge for Android is an app that allows syncing locally with various wearable devices such as wristbands and watches, without requiring an account with the respective device manufacturer cloud. Such devices are typically connected through Bluetooth (LE), migration to a new device - or after reinstalling GadgetBridge on the same device - often invalidates the connection. I personally use an Amazfit watch (mainly because of the decent battery life and because it does mostly what I need of a watch), and it pairs through Bluetooth only to a single device. That is, switching to another phone requires resetting the watch so that the new phone (or the newly installed GadgetBridge instance) can re-connect.

However, resetting the watch will make it create a new Bluetooth address for pairing, which will then appear as a new device in GadgetBridge, losing the connection to any previously recorded sensor data. Luckily, it is relatively easy to directly manipulate the database to re-connect this history.

Prerequisites

In the following, I assume that the previous database history is available in the app, e.g. through

  • The app data has been backed up and fully restored with all internal data.
  • The internal GadgetBridge database has been manually exported to mass storage, copied to the new device, and manually re-imported.
  • GadgetBridge has a convenient auto-backup feature for its database, which I generally use. This database can then be re-imported manually on the new installation.

Note that importing a database will overwrite the current one, so this should be done immediately after re-installing the app and before re-pairing with the wearable device.

How to migrate old sensor data

With the old history available, creating a new pairing will cause two devices to be visible in the app (with two different Bluetooth MAC addresses). After verifying that the device connects properly with the new device instance, the following steps can be used to merge the old device history with the newly paired instance, removing the old device entry in the process:

  1. Manually export the current database with both wearable devices visible, simply using the “Database management” menu: “Export DB”
  2. Copy the file “Android/data/nodomain.freeyourgadget.gadgetbridge/files/Gadgetbridge” from the Android device to a host PC that can run SQlite tools, e.g. through MTP or via ADB (adb pull /storage/emulated/0/Android/data/nodomain.freeyourgadget.gadgetbridge/files/Gadgetbridge).
  3. Create a backup of this file, just in case anything goes wrong in the next few steps.
  4. Open the database locally with SQlite tools. I used sqlitebrowser 3.11.2 on Ubuntu.
  5. Navigate to the “DEVICE” table and get the “_id” attribute for the current device instance. In sqlitebrowser, navigate to the “Browse Data” tab and select the “DEVICE” table. The best way to verify is to tap the burger menu (three dots) in the GadgetBridge device list for the new device that is properly connected via Bluetooth. Note the “Addr:” line. This MAC address should appear as “IDENTIFIER” attribute for one of the lines in the “DEVICE” table. Take the respective “_id” value from this line - e.g. “2”. If you have more than one gadget that you want to keep separate, also note the “_id” of the previous device instance (the one for the MAC address before repairing).
  6. Modify the sensor history to associate past values with this new device instance. In sqlitebrowser, navigate to the “Execute SQL” tab and run:

      UPDATE MI_BAND_ACTIVITY_SAMPLE
      SET DEVICE_ID = <use new _id value>
      WHERE DEVICE_ID = <use old _id value>;
    

    e.g.

      UPDATE MI_BAND_ACTIVITY_SAMPLE
      SET DEVICE_ID = 2
      WHERE DEVICE_ID = 1;
    

    If you only have one gadget, simply drop the selector line to set all the sensor history to the new device, e.g.

      UPDATE MI_BAND_ACTIVITY_SAMPLE
      SET DEVICE_ID = 2;
    
  7. Save the database, and transfer back to the Android device (via MTP or adb push Gadgetbridge /storage/emulated/0/Android/data/nodomain.freeyourgadget.gadgetbridge/files/Gadgetbridge).

  8. In GadgetBridge, re-import the database using “Database management” / “Import DB”.

  9. Delete the old device entry using the trash icon in the burger menu popup.

You should now see only the new device instance, but with the full history.

Avatar
René Mayrhofer
Professor of Networks and Security & Director of Engineering at Android Platform Security; pacifist, privacy fan, recovering hypocrite; generally here to question and learn

Related