continuous glucose measurement by patching the freestyle libre app

Introduction

The purpose of this post is to inform you about what is going on among individuals living with diabetes type 1 and how technology is changing their lives in an unusual way and also to give a glimpse of how this technology is created and implemented. 

What is the Freestyle libre app?

The Freestyle Libre 2 is a small 5cm radius flat device that you can stick to your upper arm and that measures your glucose levels. It is an improvement compared to the old finger stick blood sugar procedure in which you use test strips and sample a drop of a blood sample through a small needle injection in your fingertip 3–5 times per day. It is however also very different from a continuous glucose measurement device (CGM). CGMs have the capacity to continuously measure glucose measurements and provide alarms in case of deviations from the normal range. In contrast, the Freestyle libre only provides you information when you scan your sensor with your phone.It can also forget information if you don’t scan it for a long period. 

 

The fully continuously measuring glucose measurement devices are expensive, and therefore only provided to patients that satisfy stringent criteria such as:
  • Diabetes type 1 (the version with no functioning insulin-producing pancreas cell).
  • Bad regulated glucose levels or dangerous incidents with hypoglycemia).
  • Low aged children who might benefit from it.

The Freestyle liber patch is used in combination with the app by diabetes type 1 patients who do not satisfy the above criteria to control their blood glucose levels. The app used to be part of the software on a third separate small scanning device, but in the latest versions, the app can be installed on the smartphone and uses the Near Field Communication (NFC) scanner on the phone to scan the patch. The newest version of the patch also has a Bluetooth connection that continuously transmits information about glucose levels via Bluetooth and let the app sound an alarm in the case of out of range values. The Do It Yourself Pancreas community, a community that has taken the lead in trying to improve the healthcare of type diabetics, published a protocol to retrieve the data flow from the Freestyle libre glucose sensor and create a continuous glucose monitoring system. Setting up this information flow is the first step in creating a closed-loop system, which is an insulin administration system that can measure your glucose levels and administer the right amount of insulin when needed to keep the glucose levels stable. The setup of such a system has profound impacts on the lives of people with diabetes type 1 as Marry Patton describes in her article published in the Australian diabetes journal “One year of DIY looping after 38 years of type 1 diabetes”:

“One year ago I started do-it-yourself (DIY) looping with the hybrid closed loop system, OpenAPS. I’d been living with type 1 diabetes (T1D) for 38 years and had dreamt of a closed-loop solution since I was diagnosed at 12 years of age. The impact of this technology on my life has been so profound, both physically and psychologically, that it feels as though I am no longer even dealing with the same medical condition.”

The Do It Yourself Pancreas Community

The Do It Yourself Pancreas community, a community that has taken the lead in trying to improve the healthcare of type diabetics, has composed a protocol for modifying the Freestyle Libre app on your phone and letting it continuously measure your glucose measurements instead of intermittently. It is the first step through which some diabetes patients are creating an artificial pancreas by connecting the different devices together into a closed feedback system (which consists of minimally an insulin pump, glucose measurement device and smartphone). To connect the smartphone with the loop a patched version of the app is made by patients themselves. It used to be downloadable online but has been taken down as Abbott, the creator of the Freestyle libre has commanded to remove it online based on the Digital Millennium Copyright Act. The patched app is still going around online between people but is not published in the open because of these regulations in this article.

Is patching the app yourself legal?

I am not a lawyer so I can not say anything about this and do not use what I write over here as legal advice. A clear description of the legal actions taken and why you have to patch the application yourself is provided in this link. In short, the European Union Computer Programs Directive controls the legal protection of computer programs under the copyright law of the European Union. Their most recent report in the official journal of the European Union states the following about decompilation or reverse engineering (one of the steps in patching the original app) in article 6:

The article is quite clear:

  • Red: Reproduction (copy) and translation (decompiling) of the code can be used by persons who have purchased the software and want to achieve a connection with other software or devices.
  • Yellow: Above is permitted only if the communication with other software can only be achieved by the decompiling process.
  • Blue: The decompiled and recompiled or modified software can not be given to anyone else. So it can not be made downloadable online
 

method

Let’s take a look at the protocol. The Github code of the step process for creating the patched app can be found here and in this video. I will go through the steps in a more detailed fashion for educational purposes.

Installing xDrip

The patched app works together with xDrip+ to show the results. xDrip is a glucose visualisation app for smartphone. It is an open-source project conducted by many different programmers that started in 2014. At the time of writing 58 programmers have contributed to the project. While it is an open-source project, it is currently one of the best graphical interface that you can find out there for diabetics.

 

The xDrip package is published under the GNU license. The interesting thing here is that you may copy, modify and distribute the package, but only if you disclose the source and give the same license to the new version. The license thus gives an enormous amount of freedom to anyone that wants to use the software but also obligates that person or team to use the same license and make their improvements available to everyone.

 

Correctly installing xDrip from the Nigthscout Foundation page as a first step in setting up the patched app helps to know if the patched app is working correctly.

Installing git

Git is the standard software used by programmers to work in collaboration with other programmers on the same collection of code. Programmers could be working on their computer at the same time on the same piece of code and have different versions on their computer. Working in a team like this can lead to very complex teamwork processes. To be able to keep an overview on who contributed what in which moment time programmers use git to keep track of the versions on their computer and in online repositories. To install the latest version of git use, open a terminal (press command and spacebar and type “terminal”) and then type the following in the terminal:

git  - -version

Copying the GitHub repository files

In creating the patched app, only the cloning (copying code) function of git is used. The patch code is cloned from GitHub which is, simply said, a storage place for code with the option to make the code open-source, meaning that everybody in the world is able to copy it and improve it for their own use case. To clone the patch repository type:

git clone https://github.com/birdfly/Libre2-patched-App.git

The tree software package can be used to see how your directory looks like.

brew install tree

Then show the tree with the command “tree”, and you should see this:

.
└── Libre2-patched-App
├── README.md
├── sources
│ ├── APKExtractor.smali
│ ├── CrashReportUtil.smali
│ ├── ForegroundService.smali
│ ├── ThirdPartyIntegration.smali
│ └── sika
├── xdrip2.git.patch
└── xdrip2.patch

Installing gpatch

To compare the patched app code with the original code and change the original code based on the difference you need gpatch. According to the gpatch website:

“Patch takes a patch file containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions.”

You can instal the brew package Gpatch:

brew install gpatch

Uninstalling the original app on the phone

This means going to SETTINGS and finding the app management option and then remove it. Not only removing the icon!

Downloading the original app in the right version

The right version of the Freestyle App has to be downloaded (Version 2.3.0) through a link and copied to the right directory.

.
├── Libre2-patched-App
│ ├── README.md
│ ├── sources
│ │ ├── APKExtractor.smali
│ │ ├── CrashReportUtil.smali
│ │ ├── ForegroundService.smali
│ │ ├── ThirdPartyIntegration.smali
│ │ └── sika
│ ├── xdrip2.git.patch
│ └── xdrip2.patch
└── com.freestylelibre.app.de_2019–04–22.apk

Checking integrity of the downloaded file

Check if the downloaded version is exactly the same as the one that the patched app is made for. This is done through a mathematical calculation on the original app called checksum. On Mac this is done with MD5 by typing:

md5 com.freestylelibre.app.de_2019–04–22.apk

The result should be:

420735605bacf0e18d2570079ebaa238

 

Installing a reverse engineering tool

To compare the original app with the patch it’s necessary to first reverse engineer the original app. The app was downloaded in readable code for machines but now we have to make it readable for humans. To do this you have to install APK tool. APK is “An Android Package Kit (APK for short) is the package file format used by the Android operating system for distribution and installation of mobile apps.”. The APK is installed with brew:

brew install apktool

On windows the process is a little bit more complex, check out the website.

 

Decompiling

The next step is to use the APK tool to decode the Librelink application into more human-readable code:

apktool d -o Libre2-patched-App/librelink com.freestylelibre.app.de_2019–04–22.apk

In which:

  • d = decoding/decompile
  • o = output
  • “Libre2-patched-App/librelink” = output directory

You should now have 1041 directories, 20035 files if you check with tree. The structure looks as follows:

├── Libre2-patched-App
│ ├── README.md
│ ├── librelink -> folder with the decompiled apk file
│ ├── sources
│ │ ├── APKExtractor.smali
│ │ ├── CrashReportUtil.smali
│ │ ├── ForegroundService.smali
│ │ ├── ThirdPartyIntegration.smali
│ │ └── sika
│ ├── xdrip2.git.patch
│ └── xdrip2.patch
└── com.freestylelibre.app.de_2019–04–22.apk

 

Patching the application

Go to the librelink folder:

cd Libre2-patched-App/librelink

The command cd stands for change directory. The actual patching process uses git again:

git apply ../xdrip2.git.patch

 

Renaming the old file

Rename the downloaded APK to original.apk

mv com.freestylelibre.app.de_2019–04–22.apk original.apk

The command mv stands for move.

 

Changing the location of the old file

mv original.apk Libre2-patched-App/librelink/assets

 

Navigating to Librelink subfolder

cd Libre2-patched-App/librelink/smali_classes2/com/librelink/app/

 

Copying smali files to this folder

The most reliable way to modify the java code of an application kid is to modify the more computer-readable and less human-readable .smali code file. They are in the patch directory and they can be copied with

cp ../../../../../sources/*.smali ./

in which *.smali means all smali files.

 

Rebuilding the application

Give apktool the instructions to create a new apk-file that can be used by your smartphone to create a working application:

apktool b -o librelink.apk librelink

In which:

  • b = build
  • o = output
  • librelink.apk = new apk file
  • librelink = directory of input for making new file

 

Generating a signing Keystore

Apps have to be signed to run on your phone. The signing process has the goal to prevent people from adjusting popular apps and placing the app on google play for download thereby introducing the spread of malicious software on smartphones. Signing an app as a developer first requires creating keystores. A keystore is a storage mechanism for security certificates. The first step in doing this is to install the Java Development Kid (JDK) through this link and creating a keystore:

keytool -genkey -v -keystore librelink.keystore -alias librelink -keyalg RSA -keysize 2048 -validity 10000

and answer the questions that appear.

The second step is to sign the application with the command:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore librelink.keystore librelink.apk librelink

 

Installing the new APK

The file “librelink.apk” has to be copied to google drive and opened up in google drive on the phone.

 

Did I configure all settings right?

Now, check all settings before scanning a sensor:

  • Is NFC (Near Field Communication) enabled on your phone? Go to settings and search for NFC and turn it on?
  • Are app permissions enabled for location permission and storage permission?
  • Time and time zone are set on automatic?
  • Are all the alarms within the app activated?
  • Is bluetooth switched on?
  • Are the app notifications are enabled?
  • Is sound enabled for the app?
  • Is lock screen notifications enabled?
  • Was the original app removed from your phone?

 

Starting the app xDrip

Start the app and follow through the steps.

Starting the sensor

Start the sensor and wait 60 min to get glucose measurements being transferred continuously to the phone in xDrip.

  

The debugging process

When you run into problems you can find Github tickets (“issues” in Github terminology) to post your problems. Others can respond. The poster of the issue and all responders get a message when a new response is placed. You can have a look at the process via this link:

Libre 2 — (xDrip+ + LibreLink patched) vs. (xDrip+ + MiaoMiao2) · Issue #1394 ·…

 

Result

The result is a mechanism that continuously measures glucose levels every couple of minutes:

In this picture you can see a red line that represent predicted glucose levels 80 minutes into the future:

 

Conclusion

If there was just a fraction of the processing speed in the current covid related healthcare changes taking place in creating a better health environment for diabetics, there would be no Do-It-Yourself Pancreas movement. Bureaucracies that influence political, management and medical financial decisions slow down advances as so beautiful in detail described on the DiabetTech website. There is no need for strong and decisive leadership if patients have a decline in kidney function, a susceptibility to visual problems, a higher chance of urinary tract infections, a decreased average sleep quality or a three times higher chance of epileptic seizures because they injected too much insulin, as long as its taking place slowly and behind curtains, away from the attention of the shocking news reports that captivate mainstream attention.

Patients who do not have the attention of the popular press and political power to change things can create improvements for themselves within our complex healthcare systems by using the software and protocols made available to them by the open-source community. This story about an online collaboration between patients with diabetes and the open-source community shows that the way health care works is changing and that the speed of technology change is playing a pivotal role within the developments. New heroes are arising within the technology driven culture of today’s world and these heroes are not so easily identified as the type of individuals that we like to place at the centre of attention, however, they are probably significantly more powerful.