From efd220b52ad27fb85672558c09258ff950149167 Mon Sep 17 00:00:00 2001 From: AceVest Date: Mon, 4 Jul 2016 23:01:41 +0800 Subject: [PATCH] ... --- diying/a.py | 20 +++++ diying/b.py | 55 ++++++++++++ diying/mi/Test.class | Bin 0 -> 1539 bytes diying/mi/Test.java | 34 +++++++ diying/mi/UserInfo.class | Bin 0 -> 2999 bytes diying/mi/UserInfo.java | 185 +++++++++++++++++++++++++++++++++++++++ learn/doc/install | 6 ++ tools/hack/mi/mi.py | 41 +++++++++ 8 files changed, 341 insertions(+) create mode 100755 diying/a.py create mode 100755 diying/b.py create mode 100644 diying/mi/Test.class create mode 100644 diying/mi/Test.java create mode 100644 diying/mi/UserInfo.class create mode 100644 diying/mi/UserInfo.java create mode 100755 tools/hack/mi/mi.py diff --git a/diying/a.py b/diying/a.py new file mode 100755 index 0000000..d64fa41 --- /dev/null +++ b/diying/a.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +from bluepy.btle import Scanner, DefaultDelegate + +class ScanDelegate(DefaultDelegate): + def __init__(self): + DefaultDelegate.__init__(self) + + def handleDiscovery(self, dev, isNewDev, isNewData): + if isNewDev: + print "Discovered device", dev.addr + elif isNewData: + print "Received new data from", dev.addr + +scanner = Scanner().withDelegate(ScanDelegate()) +devices = scanner.scan(10.0) + +for dev in devices: + print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi) + for (adtype, desc, value) in dev.getScanData(): + print " %s = %s" % (desc, value) diff --git a/diying/b.py b/diying/b.py new file mode 100755 index 0000000..cf3fa67 --- /dev/null +++ b/diying/b.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ------------------------------------------------------------------------ +# File Name: ble.py +# Author: Zhao Yanbai +# 2016-07-04 22:42:30 Monday CST +# Description: none +# ------------------------------------------------------------------------ +import struct +import time +import binascii +import bluepy.btle as btle + +class MyDelegate(btle.DefaultDelegate): + def __init__(self): + btle.DefaultDelegate.__init__(self) + + def handleNotification(self, cHandle, data): + print("cHandle:", hex(cHandle), " Data:", data) + +def uuid2str(uuid) : + s = binascii.b2a_hex(uuid) + return "-".join([s[0:8], s[8:12], s[12:16], s[16:20], s[20:32]]) + +def barline(title) : + s = title + s += "-"*(80-len(title)) + print(s) + +p = btle.Peripheral("C8:0F:10:6A:B1:F0", btle.ADDR_TYPE_PUBLIC, 0) +p.withDelegate( MyDelegate() ) + + +Services = p.getServices() +Characteristics = p.getCharacteristics() + + +barline("Services") +for s in Services : + print("{0} {1}".format(uuid2str(s.uuid.binVal), s.uuid.getCommonName())) + +barline("Characteristics") +ch = None +och = None +for c in Characteristics : + print("{0} {1:4} {2:>5} {3:18} {4}".format(uuid2str(c.uuid.binVal), hex(c.getHandle()), str(c.supportsRead()), c.propertiesToString(), c.uuid.getCommonName())) + if c.uuid.getCommonName() == "ff04" : + ch = c + + if c.uuid.getCommonName() == 'Alert Level' : + print binascii.b2a_hex(c.read()) + och = c + +p.disconnect() + diff --git a/diying/mi/Test.class b/diying/mi/Test.class new file mode 100644 index 0000000000000000000000000000000000000000..db769fed65452c6cfdb97e2ce2e0b089b38ba7c7 GIT binary patch literal 1539 zcmZuxT~ixX7=BIy+0AxKlI63dXsue4P)aD2LO?W08yg!6HUUMfx+F_j%-4iX6`kpo zGyV%NywKaanSqX->7^I`41a;wUR0m6fDDG+%=@16p7-lK&%62K)o(8W%wSi;YnTsW z3`r>#G>l?V^wdndE-)0`>@F<>BWb&C zw)TsS`(8zM2ECL^e58WHkArMYX z-I|`cJ$q+vK9$bgy_e;!PLP((+eA^`hMuRbtZeOCrL(3ZLK$4Koby?8f9O$HQSx%dC;_ey{Xdvw+f+PE`^-cwQy)8z}9VtrQdY zM02qWBc7PmwzaY?BQocb959uD0}X>#jn&X=b-MV{u+QTIT|8Kk_)-59l??u{Qp z{K|#EBxS%ea&VEr6xY&q0Y3Pthh6ww_z%#NAAgSCWfy@P2T)Ff4b}i6?A$;I{TRXk z6LH3m6aCC{>G9Z5^M<yDie0u=xIW8=}fWC2vi%j2lbQ%sl zJvyT00HG6yac-p(P5JNr;+KD(jXz@npLdsQP-WNbeT4VAgg#s*yrW3t3f8#aB)~KD z2*LLXs)F16iNEoPVYC0?^aF1op9e;`gNVNaBYdfY{_uAl41^~-7&L}D7^Z#1pxtj= z?jZOAqZ=+FE({m_E(Tl-x)QySLtN>=cM4dXP)TBX`3Biz9~8o-vS0a!tFhY)q);b) MeT481eSMhw7s3}d*Z=?k literal 0 HcmV?d00001 diff --git a/diying/mi/Test.java b/diying/mi/Test.java new file mode 100644 index 0000000..1fc1f10 --- /dev/null +++ b/diying/mi/Test.java @@ -0,0 +1,34 @@ +public class Test +{ + + private static int uid = 27182818; + private static int gender = 1; + private static int age = 25; + private static int height = 123; // cm + private static int weight = 45; // kg + private static String alias = "Ace"; + private static int type = 0; + private static String BTAddress = "C8:0F:10:6A:B1:F0"; + private static String hexStr = "0123456789ABCDEF"; + + private static UserInfo mUserInfo = new UserInfo(uid, gender, age, height, weight, alias, type); + + public static void main(String[] args){ + byte[] mBin = mUserInfo.getBytes(BTAddress); + String mHex = BinaryToHexString(mBin); + System.out.println(mHex); + } + + public static String BinaryToHexString(byte[] bytes) { + String result = ""; + String hex = ""; + for(int i=0; i> 4)); + hex +=String.valueOf(hexStr.charAt((bytes[i]&0x0F))); + result += hex; + } + + return result; + } + +} diff --git a/diying/mi/UserInfo.class b/diying/mi/UserInfo.class new file mode 100644 index 0000000000000000000000000000000000000000..e3174c6c12ead3d10974aed806249565a6fd0192 GIT binary patch literal 2999 zcmZ`)-BTOa75}ZI{gARai(qXC;Kt3DSV?hPQq{#avBB6T1ri4^aY&mkXc4kyNfE0U z;*YqF^VPcTxG$YdW|~g=z)L1^W@?eCU7kCA?o%K8*1w>gj$`^eyTZgk_Tij!&pr3v zbAIP{@BaP%U)BIThhHaG z&qTkTz|Y}|m=!T6A}_*|1@jV85V4Rz5odLrOW-j%wIuqyh_Zo-fvSNwWJS%uqG;bh z-N2g?a6!kCf@r;%S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;;e1hV7y1*3jQF=!G3awXLDQXDI*B&0Y63 z(TFpFpr2dAKVcw$;5&pH($=lvD-0Zafb}=ai*6zkmiGli$9K#*zI9jrH28Q4QC|8# DZZca- literal 0 HcmV?d00001 diff --git a/diying/mi/UserInfo.java b/diying/mi/UserInfo.java new file mode 100644 index 0000000..9998f66 --- /dev/null +++ b/diying/mi/UserInfo.java @@ -0,0 +1,185 @@ +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; + +public class UserInfo +{ + + private int uid; + private byte gender; + private byte age; + private byte height; // cm + private byte weight; // kg + private String alias = ""; + private byte type; + + private UserInfo() + { + + } + + public UserInfo(int uid, int gender, int age, int height, int weight, String alias, int type) + { + this.uid = uid; + this.gender = (byte) gender; + this.age = (byte) age; + this.height = (byte) (height & 0xFF); + this.weight = (byte) weight; + this.alias = alias; + this.type = (byte) type; + } + + public static UserInfo fromByteData(byte[] data) + { + if (data.length < 9) + { + return null; + } + UserInfo info = new UserInfo(); + + info.uid = data[3] << 24 | (data[2] & 0xFF) << 16 | (data[1] & 0xFF) << 8 | (data[0] & 0xFF); + info.gender = data[4]; + info.age = data[5]; + info.height = data[6]; + info.weight = data[7]; + try + { + info.alias = data.length == 9 ? "" : new String(data, 8, data.length - 9, "UTF-8"); + } catch (UnsupportedEncodingException e) + { + info.alias = ""; + } + info.type = data[data.length - 1]; + + return info; + } + + public byte[] getBytes(String mBTAddress) + { + byte[] aliasBytes; + try + { + aliasBytes = this.alias.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) + { + aliasBytes = new byte[0]; + } + ByteBuffer bf = ByteBuffer.allocate(20); + bf.put((byte) (uid & 0xff)); + bf.put((byte) (uid >> 8 & 0xff)); + bf.put((byte) (uid >> 16 & 0xff)); + bf.put((byte) (uid >> 24 & 0xff)); + bf.put(this.gender); + bf.put(this.age); + bf.put(this.height); + bf.put(this.weight); + bf.put(this.type); + bf.put((byte)0x04); + bf.put((byte)0x00); + if(aliasBytes.length<=8) + { + bf.put(aliasBytes); + bf.put(new byte[8-aliasBytes.length]); + }else{ + bf.put(aliasBytes,0,8); + } + + byte[] crcSequence = new byte[19]; + for (int u = 0; u < crcSequence.length; u++) + crcSequence[u] = bf.array()[u]; + + byte crcb = (byte) ((getCRC8(crcSequence) ^ Integer.parseInt(mBTAddress.substring(mBTAddress.length()-2), 16)) & 0xff); + bf.put(crcb); + return bf.array(); + } + + private int getCRC8(byte[] seq) + { + int len = seq.length; + int i = 0; + byte crc = 0x00; + + while (len-- > 0) + { + byte extract = seq[i++]; + for (byte tempI = 8; tempI != 0; tempI--) + { + byte sum = (byte) ((crc & 0xff) ^ (extract & 0xff)); + sum = (byte) ((sum & 0xff) & 0x01); + crc = (byte) ((crc & 0xff) >>> 1); + if (sum != 0) + { + crc = (byte) ((crc & 0xff) ^ 0x8c); + } + extract = (byte) ((extract & 0xff) >>> 1); + } + } + return (crc & 0xff); + } + + public String toString() + { + return "uid:" + this.uid + + ",gender:" + this.gender + + ",age:" + this.age + + ",height:" + this.getHeight() + + ",weight:" + this.getWeight() + + ",alias:" + this.alias + + ",type:" + this.type; + } + + /** + * @return the uid + */ + public int getUid() + { + return uid; + } + + /** + * @return the gender + */ + public byte getGender() + { + return gender; + } + + /** + * @return the age + */ + public byte getAge() + { + return age; + } + + /** + * @return the height + */ + public int getHeight() + { + return (height & 0xFF); + } + + /** + * @return the weight + */ + public int getWeight() + { + return weight & 0xFF; + } + + /** + * @return the alias + */ + public String getAlias() + { + return alias; + } + + /** + * @return the type + */ + public byte getType() + { + return type; + } +} diff --git a/learn/doc/install b/learn/doc/install index 580430d..5b46589 100644 --- a/learn/doc/install +++ b/learn/doc/install @@ -21,3 +21,9 @@ gnugo rhythmbox ffmpeg +apt-get install bluez +apt-get install libboost-python-dev +apt-get install libbluetooth-dev +apt-get install libboost-all-dev +pip install gatterlib + diff --git a/tools/hack/mi/mi.py b/tools/hack/mi/mi.py new file mode 100755 index 0000000..1aac4ae --- /dev/null +++ b/tools/hack/mi/mi.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ------------------------------------------------------------------------ +# File Name: mi.py +# Author: Zhao Yanbai +# 2016-07-04 22:36:34 Monday CST +# Description: none +# ------------------------------------------------------------------------ + +import datetime +import time +import mibanda +import sys + +def main() : + if len(sys.argv) != 2 : + print("{0} ".format(sys.argv[0])) + sys.exit() + + macAddr = sys.argv[1] + + device = mibanda.BandDevice(macAddr, "MI") + device.connect() + print "Address:", device.getAddress() + print "Name:", device.getName() + bi = device.getBatteryInfo() + print "BatteryInfo. Status:", bi.status, " Level:", bi.level, " Charge Counter:", bi.charge_counter, " Last Charge:", bi.last_charged + + print "Steps:", device.getSteps() + di = device.getDeviceInfo() + print "DeviceInfo:", di.firmware_version + + print "SetUserInfo" + device.setUserInfo(27182818, True, 25, 123, 45, 0, "Ace") + print "Finished" + t= datetime.datetime.now() + datetime.timedelta(seconds=100) + device.setAlarm(enable=1, number=0, when=t, smart=0, repeat=0) + + +if __name__ == "__main__" : + main() -- 2.44.0