PhoneGap fixes
or: Android device target Unknown, permissions & script injection
Recently I've upgraded my Smartphone (Nexus 5), and when trying to debug some Android Apps on it,
it seemed that the Android Device Chooser couldn't show much information about the device, namely:
Serial Number:Â ?????????????
AVG Name: N/A
Target: Unknown
This was odd, but I assumed (correctly) that ADB wasn't recognizing the device, and thus it wasn't an Eclipse problem - but the android debug bridge's problem. As soon as I restarted the adb (find adb first via "locate adb")Â via:
1
2
./adb kill-server
./adb start-server //might need to sudo this one
Check that your device is now visible and good via
1
./adb devices
If it still doesn't work, try disabling and enabling USB debugging on your phone.
Finally, if all those don't work, try and review this page.
Section 2: Never forget permissions.
Working with PhoneGap can be great, but sometimes you can get errors that have zero information about them on Google.
After getting a very odd bug from PhoneGap when trying to open an external page,
I came across this explanation.
Here I was under the impression that it might be the Cordova version (which I later figured was old because my PhoneGap version was old, all hail the new NPM support!).
Eventually I realized I was just silly and forgot to add the INTERNET permission, which is kind of important, even if ACCESS_NETWORK_STATE is already there.
Section 3: Injecting JavaScript into InAppBrowser
Finally I attempted to inject JS into an inAppBrowser window via
1
ref.executeScript({ file: ... });
I was wonder at first why that didn't work, but executing code did.
1
ref.executeScript({ code: ... });
After a relative small amount of information, and a big amount of debugging, I've realized it had something to do with the protocol (at least I think).
Because my InAppBrowser was pointing to a website (http://)Â as opposed to a local file (file://).
Finally I used an XMLHttpRequest to get the local file via JS, putting it inside a variable and executing the code:
1
2
3
4
5
6
7
8
9
10
11
12
</p>
<p>xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","customscript.js",true);
xmlhttp.onreadystatechange = function() {
//Request was successful
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
execScript(xmlhttp.responseText);
}
};
execScript = function(resp) {
ref.executeScript({code: resp });
}
This should solve the problem, until next time!