有兴趣的可以试试:
以下代码另存为一个python文件例如:meizu.py
import sys
from btlewrap import BluepyBackend
from btlewrap.base import BluetoothInterface
bt_interface = BluetoothInterface(BluepyBackend, adapter='hci0')
cmd_th = b'\x55\x03\x01\x11'
cmd_bt = b'\x55\x03\x01\x10'
handle = 48
def get_data(mac_addr):
with bt_interface.connect(mac_addr) as connection:
res = connection.write_handle(handle, cmd_th)
if 'rsp' not in res:
raise Exception()
data = connection.read_handle(handle)
temp10 = int.from_bytes(data[4:6], byteorder='little')
humi10 = int.from_bytes(data[6:8], byteorder='little')
temperature = float(temp10) / 100.0
humidity = float(humi10) / 100.0
res = connection.write_handle(handle, cmd_bt)
if 'rsp' not in res:
raise Exception()
data = connection.read_handle(handle)
battery = float(data[4]) / 10.0
return temperature, humidity, battery
if __name__ == '__main__':
if len(sys.argv) != 2:
print("need mac addr.")
sys.exit(0)
mac_addr = sys.argv[1]
print("-------GET Meizu Data: %s------->" % mac_addr)
temperature, humidity, battery = get_data(mac_addr)
print("temperature:%.2f,humidity:%.2f%%,battery:%.1fv" % (temperature, humidity, battery))
print("-------FINISH GET-------<")
通过命令python3 meizu.py mac_addr
只测试了python3,并且因为依赖bluepy库,安装方法参考github,https://github.com/IanHarvey/bluepy
|