Skip to main content

MiBand2 Python API based on bleak

The library provides convenient APIs to control and read data of MiBand2.

Supported features

  • sending notifications
  • date/time setup and reading
  • onetime alarm setup
  • reading battery level

Usage

import asyncio
import bleak
from miband2 import Band2, Key, NotificationType as NT
from datetime import datetime

KEY = Key(
    key=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05',
    reset=True # note *
)

async def main():
    located_band = await scan_bands(timeout=15)
    client = bleak.BleakClient(located_band, timeout=15)
    
    async with Band2(client) as band:
        auth_status = await band.auth(KEY)
        print(f"auth status: {auth_status}")

        battery_level = await band.get_battery()
        print(f"battery: {battery_level}")

        await band.ring(NT.SINGLE)
        
        now = datetime.now()
        await band.set_datetime(now)

async def scan_bands(timeout):
    def bands_only(dev, adv):
        return "MI Band" in dev.name
    return await bleak.BleakScanner.find_device_by_filter(timeout=timeout, filterfunc=bands_only)

asyncio.run(main())

Note: key reset is required for the first time only. Later, one can auth with the same key setting the reset=False

Why?

This project initially helped me to learn BLE. My old Band2 got its second life when I started to learn BLE and became curious if I can communicate with the device using a Linux machine. This Band2 device acted as an oracle visually confirming if I craft packets correctly. The fact of a visual feedback in a form of a notification or displaying a newly set time provided some incentive to me and made learning funnier than it could be if relying solely on reading books.

Metadata

Release files for expmiband2 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for expmiband2 0.1.3
File Size Uploaded
expmiband2-0.1.3.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for expmiband2 0.1.3
File Interpreter ABI Platform
expmiband2-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / expmiband2-0.1.3.tar.gz

Download URL expmiband2-0.1.3.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
389d07a5fead467a75213497e6adc86c93706f607833daf4084ea5af4e85f72e
BLAKE2b-256 checksum
How to use checksums
cd0b8b45dc821a93c014416a11e4ed85118d4f6aead19544cafb6d09ccabaf12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / expmiband2-0.1.3-py3-none-any.whl

Download URL expmiband2-0.1.3-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e411c9927dd92829bf3abf6e7e21880739dc67d5a6c06b446dbe969a21c8002
BLAKE2b-256 checksum
How to use checksums
08e473dbeadc8d069bc1482b750ad9d1b9ff67edbf9b148f0c9a6b034690994a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page