我有一個x86 API23 AVD(帶有Google API),它使用Android Studio 2.1.1創建,我需要發送GPS座標。我已經廣泛閱讀了如何使用命令行中的「adb emu geo fix」命令或通過telnet--在驗證後執行此操作,然後發送geo fix命令以及命令的緯度,經度和可選高度參數線也是如此。如何以編程方式將GPS位置發送到Android Studio 2.0 AVD
我在Mac OSX El Capitan盒子上運行我的代碼。
問題是我的應用程序 - 需要輸入我發送的GPS座標的應用程序,就好像它沒有獲取任何數據。
如果我使用AVD本身的擴展控件來發送與SEND按鈕相同的當前位置,或播放從.gpx文件加載的路由數據,那麼一切正常。該應用獲取GPS數據並按照預期行事。
問題是,我正在運行需要啓動AVD的測試自動化(Appium,Java,TestNG),然後發送GPS數據,然後驗證被測試的應用的行爲與預期的一致GPS數據。
這意味着我無法手動與AVD的擴展手動控制進行交互。
我必須以編程方式完成這一切。
以下是我現在通過Telnet命令進行的操作。代碼看起來基本上像這樣發送只是一個「當前位置」:
import org.apache.commons.net.telnet.TelnetClient;
static TelnetClient tc = null;
public InputStream inptStream;
public PrintStream outptStream;
public String prompt = "OK";
//Instantiate the telnet client -- we use this to send geo fix commands to the emulator
tc = new TelnetClient();
//Connect, this will generate the auth_token if it does not already exist in file system
System.out.println("Trying to connect to AVD...");
tc.connect("localhost", 5554);
//Check to see if we are connected
Boolean areWeConn = tc.isConnected();
System.out.println("Are we connected?" + areWeConn);
// Get input and output stream references
System.out.println("Getting input and output streams...");
inptStream = tc.getInputStream();
outptStream = new PrintStream(tc.getOutputStream());
//wait for OK prompt
System.out.println("Waiting for the OK prompt...");
//Not including readUntil() code because it's just reading terminal output
readUntil(prompt);
//Send the auth token number
System.out.println("Sending auth token...");
outptStream.println("auth " + "3A/Yfazi3pRcvNiB");
outptStream.flush();
//wait for OK prompt
System.out.println("Waiting for the OK prompt...");
readUntil(prompt);
//Send current location for our Starting Point
System.out.println("Sending Current Location - Starting Point");
outptStream.println("geo" + "fix" + "28.4194 -81.5812");
outptStream.flush();
//Now disconnect from Telnet
System.out.println("Disconnecting from AVD...");
tc.disconnect();
//Check to see if we are still connected
Boolean stillConn = tc.isConnected();
System.out.println("Are we still connected? " + stillConn);
當上面的代碼未能觸發我的應用程序的預期行爲,即使它出現在所有沒有任何錯誤的工作,我用與在其上運行我的應用程序,然後啓動AVD的終端使用的另一個終端發送的「當前位置」手動在Telnet行提示下列命令(認證後):
telnet localhost 5554
等待行...
然後驗證通過發送身份驗證令牌...
等待OK手動,則發送:
geo fix "28.4194 -81.5812"
這個命令出現在提示符下(沒有錯誤)完美地工作,但我的應用程序顯然沒有得到任何GPS信息。
所以,我嘗試使用上述命令的ADB版本,就像這樣:
adb emu geo fix "28.4194 -81.5812"
但這也未能奏效。
同樣,使用Appium自己的Android驅動程序我嘗試以下(創建驅動程序後,當然):
Location currLocation = new Location(28.41936, -81.5812, 0.0);
//Set Current Location for
myDriver.setLocation(currLocation);
但司機似乎「掛」在這裏。沒有調試輸出可以得到我。它只是......阻塞,直到事情最終超時。
而且,我也嘗試了所有上述Google地圖移動應用程序,但它也無法響應我發送的當前位置座標。
所以,我卡住了!
有沒有人真的有幸與PROGRAMMATICY發送「地理修復」命令到他們的應用程序下測試API23 AVD創建與Android Studio 2+?
由早於2.0版本的Android Studio創建的AVD不能用於我的目的。
任何有關我在做什麼錯誤或可能的解決方法的反饋將不勝感激!
感謝,
沃爾夫
嗨吉爾吉森,謝謝你的好評!我會給你空的令牌技巧,試試我得到的下一個機會!但是,在我已經連接了有效的身份驗證令牌後,我在代碼中使用了ADB命令,但它們仍然沒有工作。我本可以解決一些問題。 – Wulf
酷我很高興你找到了發送位置和路線!也感謝lat/long的逆轉。我會檢查出來,也許它會讓我繼續使用身份驗證。最後一件事,我注意到上面你在lat/long周圍對adb使用了雙引號,但是當我得到它的工作時,我不會將它們作爲單獨的值放在雙引號中:adb emu geo fix 28.4194 -81.5812設置我知道這是否有助於adb – Gilgeson