Skip to content

Instantly share code, notes, and snippets.

@willwhui
Last active December 14, 2019 12:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwhui/7829f4a76d8bf8a3410fe50a9000352c to your computer and use it in GitHub Desktop.
Save willwhui/7829f4a76d8bf8a3410fe50a9000352c to your computer and use it in GitHub Desktop.
获取小米智能设备token
@willwhui
Copy link
Author

willwhui commented Nov 30, 2017

关于token的获取:

miui9如何进入开发者模式

参见:http://www.miui.com/unlock/done.html

将手机root之后获取

参见“Xiaomi Smart WiFi Socket and Smart Power Strip”链接中的 Retrieving the Access Token
有两点要注意,

  1. 文中的:

Before you begin you need to install libffi-dev and libssl-dev by running the command below. This is needed for python-mirobo to be installed correctly.
$ sudo apt-get install libffi-dev libssl-dev

指的是通过电脑获取token之后,配置好yaml文件之后,hass加载小米设备之前,需要在树莓派(我的hass装载树莓派上)中(虚拟环境外)安装这两个组件。
我原以为是在电脑上安装,结果在后面的步骤中,在树莓派上安装必需的“python-mirobo”时,出现了如下错误:

Command "/srv/homeassistant/bin/python3 -u -c "import setuptools, tokenize;file='/tmp/pip-build-rfcp4ngv/cffi/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-alsgwdmy-record/install-record.txt --single-version-externally-managed --compile --install-headers /srv/homeassistant/include/site/python3.5/cffi" failed with error code 1 in /tmp/pip-build-rfcp4ngv/cffi/

(参见讨论:https://community.home-assistant.io/t/home-assistant-cannot-install-dependency-python-mirobo-0-2-0/28207/6?u=)

  1. 文中接着上面一句出现的,这句和token无关,但是是最终接入成功的关键:

If your Home Assistant installation is running in a Virtualenv, make sure you activate it by running the commands below.
$ sudo su -s /bin/bash homeassistant
$ source /srv/homeassistant/bin/activate

指的是后续在树莓派中安装必需的“python-mirobo”时,应该现进入python虚拟环境中(如果hass之前是在虚拟环境中的话)。
python-mirobo是支持小米智能设备协议的模块,安装情况详见下文“开关加载不成功”部分。

手机并不被adb services识别,所以得不到token

获取token的时候需要用到adb devices等命令,但并不能列出我的设备
手机:小米手机5,OS miui 9
ubunt命令行中输入lsusb,可见设备存在:

$ lsusb
...
Bus 001 Device 008: ID 2717:ff48  
...

最终找到这里:https://android.stackexchange.com/questions/144966/how-do-i-get-my-device-detected-by-adb-on-linux
需要按照文中的方法进行设置。注意编辑“~/.android/adb_usb.ini”这一步对我来是是不可避免的。
最终得到了:

~$ adb devices
List of devices attached 
14f36c46	device

注意,上面的product id "ff48"在设置的中途变化了一次

@willwhui
Copy link
Author

willwhui commented Nov 30, 2017

在bluestacks虚拟机上获取token(失败了)

安装好bluestacks,在上面安装米家app
注意:bluestacks必须设置成英文,否则米家app中登录之后看不到自己的智能设备

root之前,先看看能不能连接到bluestacks

把bluestacks运行起来,然后,
根据http://mattpilz.com/using-bluestacks-fast-easy-android-emulation/
先找到adb应用程序,并运行 adb:
结果是类似这样的:

C:\Users\YourUserName\AppData\Local\Android\Sdk\platform-tools>adb devices
List of devices attached

一个设备都没有。
帖子中提到要先连接,再枚举设备:

> adb connect 127.0.0.1
connected to 127.0.0.1:5555
>adb devices
List of devices attached
127.0.0.1:5555  device

这样就看到设备了。
然后根据hass官方的文档(上面提到的"Retrieving the Access Token"),知道可以进入adb shell看看里面有什么:

> adb shell <------------- 注:以下就是在bluestacks的android系统里了 
shell@ghost:/ $ ls
acct
boot
cache
config
d
data
default.prop
dev
etc
file_contexts
init
init.environ.rc
init.rc
init.trace.rc
init.usb.rc
init.x86.rc
lib
mnt
proc
property_contexts
root
sbin
sdcard
seapp_contexts
sepolicy
storage
sys
system
ueventd.android_x86.rc
ueventd.rc
vendor
x86.prop
shell@ghost:/ $ ls /data <-------------注:想直接进入data目录拿文件是不可能的
opendir failed, Permission denied

如果试着在adb shell之前运行adb root,得到root权限,其结果是:和设备建立的连接被终止

所以,需要先root掉bluestacks,然而:

按照这里的指导 https://forum.xda-developers.com/general/general/bluestacks-tweaker-2-tool-modifing-t3622681
做完之后,并不能root掉bluestacks!

@willwhui
Copy link
Author

willwhui commented Nov 30, 2017

直接从host系统中获取小米的miio2.db文件

根据官方文档中的命令行

echo "select name,localIP,token from devicerecord;" | sqlite3 /data/data/com.xiaomi.smarthome/databases/miio2.db 

可知,token都在miio2.db文件里,从里面select出来就好了。

这个文件应该存在于bluestacks在host os的某个目录的data.vid中。
可以找到这个文件并且找个可以打开.vid文件的工具
但我这里有一个简单办法:
前面尝试root的过程中,也就是根据这里操作的时候 https://forum.xda-developers.com/general/general/bluestacks-tweaker-2-tool-modifing-t3622681
使用到了一个工具叫做Bluestacks Tweaker.exe
将它和bluestacks同时运行起来。
在这个tweaker.exe的程序界面中有一个tab页面叫做FM(估计是File Manager的意思)
等bluestacks完全启动后,就可以从这个页面中直接访问虚拟机的/data/data/com.xiaomi.smarthome/databases/目录,得到miio2.db文件
将这个文件拷贝出来,想办法把想要的数据select出来。

因为我有linux机器,所以我把这个文件copy到linux里面,在命令行运行上述带有select的命令。
然后在命令行中,进入到包含这个文件的目录。
运行:

echo "select name,localIP,token from devicerecord;" | sqlite3 miio2.db

就得到想要的结果了!

@XuCcc
Copy link

XuCcc commented Feb 12, 2018

/data/data/com.xiaomi.smarthome/databases/miio2.db 里token为空咋回事0.0

@chrisvella
Copy link

chrisvella commented Mar 6, 2018

@XuCcc take a look at python-miio issue 185 if you are still having trouble.

@willwhui
Copy link
Author

willwhui commented Apr 2, 2018

@XuCcc 可能是米家没有完成设备同步?
或者如@chrisvella所说,你使用了高版本的米家app
我从朋友那里听说了,米家app升级之后,改变了文件格式,你可以找一个旧版的米家app试试。
目前我的旧版本app还是可以成功登录的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment