我們經常會遇到這樣子的情況,需要演示一些手機上的一些界面的時候。不能夠把手機上的影像投影到電腦上,同時在電腦上操作手機。也可能是我瞭解的比較少吧。
最近在論壇上看到了 [STF 框架之 minitouch 工具] (https://testerhome.com/topics/4400) 其實裏面已經介紹的很詳細了,只是使用C#的來實現的。對於c#確實不太熟悉,所以就拿了 minicap_java DoctorQ使用java基於minicap實現的一個GUI展示Android手機屏幕的一個工具來繼續完善它,使它能夠支持一些鼠標點擊操作。
介紹
minitouch提供了一個socket接口用來出來在Android設備上的多點觸摸事件以及手勢。它能夠支持api 10以上的設備且不需要通過root. 但是根據不同的cpu的ABI需要使用不同的minitouch。
使用
首先我們需要找出你的設備所支持的ABI
ABI=$(adb shell getprop ro.product.cpu.abi | tr -d '\r')
注意:如果你有多臺設備連接的情況下並且你沒有設置$ANDROID_SERIAL的話,你需要去指定設備
-s <serial>
推送對應的文件到設備上。
adb push libs/$ABI/minitouch /data/local/tmp/
注意如果你的SDK<16的情況下,你需要使用minitouch-nopie
當然你還需要更改下minitouch的執行權限。
chmod 777 /data/local/tmp/minitouch
並且通過下面的命令來判斷是否已經操作成功了。
adb shell /data/local/tmp/minitouch -h
下來我們可以直接通過
adb shell /data/local/tmp/minitouch
來執行,這個時候設備就開始監聽了。這個時候除非出現錯誤的消息或者說程序退出,我們需要進行端口轉發 通過如下命令:
adb forward tcp:1111 localabstract:minitouch
- 現在我們就需要去連接對應的端口,獲取數據了。
提示文檔裏面給的是通過nc localhost 1111
,但是我們肯定不是這樣子的,我們需要自己去創建一個socket來進行連接。獲取socket對應的數據。
socket命令
d <contact> <x> <y> <pressure>
例如:d 0 10 10 50
壓力值50 在點 10,10 使用一個觸點按下。
m <contact> <x> <y> <pressure>
例如: m 0 10 10 50
壓力值爲50在 10,10滑動。
u <contact>
例如:u 0
手勢擡起
實現
這裏我們只說明鼠標的一些事件的實現。
mp.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("i press"+e.getX()+","+e.getY());
Point point = pointConvert(e.getPoint());
if (outputStream != null) {
String command = String.format("d 0 %s %s 50\n", (int)point.getX(), (int)point.getY());
executeTouch(command);
}
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("i release");
if (outputStream != null) {
String command = "u 0\n";
executeTouch(command);
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
mp.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println(e.getPoint().getX()+","+e.getPoint().getY());
Point point = pointConvert(e.getPoint());
if (outputStream != null) {
String command = String.format("m 0 %s %s 50\n", (int)point.getX(), (int)point.getY());
executeTouch(command);
}
}
@Override
public void mouseMoved(MouseEvent e) {
}
});
private Point pointConvert(Point point)
{
Point realpoint = new Point((int)((point.getX()*1.0 / width) * banner.getMaxX()) , (int)((point.getY()*1.0 /height) * banner.getMaxY()) );
return realpoint;
}
private void executeTouch(String command) {
if (outputStream != null) {
try {
System.out.println("command" + command);
outputStream.write(command.getBytes());
outputStream.flush();
String endCommand = "c\n";
outputStream.write(endCommand.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
minicapDemo具體可參考代碼。
參考文檔
STF 框架之 minicap 工具
STF 框架之 minitouch 工具
感想
上面的項目基本上都沒改什麼,只是簡單的加了幾句代碼而已。還是要感謝幾個大神。