Reading Xiaomi Mi Scale data with Web Bluetooth Scanning API

Henry Lim - May 10 '20 - - Dev Community

The Web Bluetooth API provides the ability to connect and interact with Bluetooth Low Energy (BLE) peripherals. It was introduced in Chrome 56 on macOS back in January 2017.

Reading the weight data

Note: This demo only works on the first generation of Mi Body Composition Scale which was released in 2017 (Model Number: XMTZC02HM).

To capture the BLE advertising packets from the Xiaomi Mi Scale, we will need to use the Web Bluetooth Scanning API

However, the Web Bluetooth Scanning API is still under development. You need to use Chrome 79+ with the chrome://flags/#enable-experimental-web-platform-features flag enabled to use the API.

navigator.bluetooth.requestLEScan() will start scanning for the advertising packets. Right before that, the permission prompt will popup asking the user for permission to access Bluetooth.

After the user has granted permission, we can listen to the advertising packets by using the advertisementreceived event:

The advertisementreceived event will return information like Device Local Name, Device ID, Received Signal Strength Indicator (RSSI), Transmit power (TX Power), Service UUIDs, Manufacturer Data, and Service Data.

To retrieve the payload data from Xiaomi Mi Scale, we need to get the data from serviceData.

The output result of "valueDataView.buffer"

The payload format for the Mi Scale

Next, to get the weight data, we need to get the value of bytes 11 and 12 (which are little-endian) then divide the value by 200.

With that, we have successfully retrieved the weight data from Xiaomi Mi Scale using the Web Bluetooth Scanning API.

Extra: Reading the Impedance Data

The Xiaomi Mi Scale is also able to measure information like muscle mass, bone mass, body fat, and more through Bioelectrical Impedance Analysis (BIA). 

We can get the impedance data from bytes 9 and 10:

Next, we can convert the impedance value using this algorithm. The output should look something like this:


Try It Out

Note: There's no way to get the impedance data if you are wearing socks, but I don't want to show my ugly feet… (And yes, that's a T-Rex socks)

Demo: https://scale.limhenry.xyz
GitHub (Source Code): github.com/limhenry/web-bluetooth-mi-scale


References

  1. Web Bluetooth Documentation: Link
  2. Web Bluetooth Scanning API Sample: Link
  3. Xiaomi Mi Scale BLE (Unofficial) Documentation: Link
  4. TypedArray or DataView: Understanding byte order: Link
. . . . . . . . . . . .
Terabox Video Player