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

在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