Skip to main content

Encode and decode NDEF message using Xiaomi NFC protocol.

Project description

Xiaomi NDEF

!python-versions Pypi

Test Release

Encode and decode NDEF message using Xiaomi NFC protocol.

Usage

pip install xiaomi-ndef
from pyndef import NdefMessage

from xiaomi_ndef import MiConnectData, XiaomiNfcPayload, V2NfcProtocol
from xiaomi_ndef import ndef, xiaomi, handoff, tag

# Example
NDEF_MSG_BYTES = b"..."

# Parse ndef
ndef_msg = NdefMessage.parse(NDEF_MSG_BYTES)

ndef_type = ndef.get_xiami_ndef_payload_type(ndef_msg)
ndef_bytes = ndef.get_xiami_ndef_payload_bytes(ndef_msg, ndef_type)

# Parse ndef payload
mi_connect_data = MiConnectData.parse(ndef_bytes)
nfc_protocol = mi_connect_data.get_nfc_protocol()
nfc_payload = mi_connect_data.to_xiaomi_nfc_payload(nfc_protocol)

# Build new screen mirror record
handoff_ndef_type, handoff_payload = xiaomi.new_handoff_screen_mirror(
    device_type=handoff.DeviceType.PC,
    bluetooth_mac="00:00:00:00:00:00",
    enable_lyra=True
)
handoff_record = ndef.new_xiaomi_ndef_record(handoff_ndef_type, handoff_payload)

# Build new ndef msg
handoff_msg = NdefMessage(handoff_record)
print(handoff_msg.to_bytes().hex())

# Customize xiaomi ndef
XiaomiNfcPayload(
    major_version=1,
    minor_version=11,
    id_hash=0,
    protocol=V2NfcProtocol,
    appData=tag.NfcTagAppData(
        major_version=1,
        minor_version=0,
        write_time=1666666666,
        flags=0,
        records=(
            tag.NfcTagDeviceRecord(
                device_type=tag.DeviceType.MI_SOUND_BOX,
                flags=0,
                device_number=0,
                attributes_map=tag.NfcTagDeviceRecord.new_attributes_map([
                    tag.DeviceAttribute.WIFI_MAC_ADDRESS.new_pair("00:00:00:00:00:00"),
                    tag.DeviceAttribute.BLUETOOTH_MAC_ADDRESS.new_pair("00:00:00:00:00:01")
                ])
            ),
            tag.NfcTagActionRecord(
                action=tag.Action.CUSTOM,
                condition=tag.Condition.AUTO,
                device_number=0,
                flags=0
            )
        )
    )
)

Related Projects

Reference

Package name Version
com.xiaomi.mi_connect_service 3.1.453.10
com.milink.service 15.0.5.0.ceaac61.2919843
com.xiaomi.smarthome 9.1.501
com.android.nfc 14

License

MIT License

Copyright (c) 2024 XFY9326

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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

xiaomi_ndef-0.1.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

xiaomi_ndef-0.1.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file xiaomi_ndef-0.1.2.tar.gz.

File metadata

  • Download URL: xiaomi_ndef-0.1.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for xiaomi_ndef-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8ecd32a410cd54e1e6feea6efa30ac2a5d1021f5c5c7bdf4a944bfa646e2dc8d
MD5 3d25b7262f9e3b4226597563f655db00
BLAKE2b-256 018b704e7d7933cd6a68febf7190fc7a8c2d8865d7187e677df3e44cdf4a7d7f

See more details on using hashes here.

File details

Details for the file xiaomi_ndef-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: xiaomi_ndef-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for xiaomi_ndef-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c4ded2570b331f917b96156da8536efde9c740327c960a70e924cff34e1a2869
MD5 a3289b6259037244baa581e1048b4eda
BLAKE2b-256 3d52f8483fe71af8995074fe0ad86924b3b007f8f9bfb51439792a8127688f35

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