Skip to main content

Python API for communicating with MiBand2

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

miband2-0.1.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

miband2-0.1.1-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file miband2-0.1.1.tar.gz.

File metadata

  • Download URL: miband2-0.1.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for miband2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 80a322cabb00018c6bb377ae8c9208e8bb91079245f7e0a377fe604f600faed7
MD5 8d6241c02b2fdb146a28291854d47d5e
BLAKE2b-256 cbde0f3f6b24b8591380837f9782a2eebb49b056dfe729ac2e4e19eba760930f

See more details on using hashes here.

File details

Details for the file miband2-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: miband2-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for miband2-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66ae02958cdf62ad9279cebf7bb6bc0b4c578f285fb33eac1d1642a935a59bb1
MD5 2d27931223c81502c5c6752368690914
BLAKE2b-256 42db8b0119fa33eb6f8f21bc02a64f6253458a5c3fc066778116caa4b4519487

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page