【发布时间】:2015-11-09 09:49:35
【问题描述】:
我一直在使用 Cordova 编写适用于 Android 的应用程序,以读取非 NDEF NFC 卡。 然后将标签 ID 发送到服务器并注册到数据库中,但这不是重点。
我制作了这个应用程序,并在我的 Nexus 4 上进行了测试,使用的是 Android 5.1.1,我想在旧的 android 版本上试用。
所以我拿了一个同事的手机,它是 4.0.2,但应用程序无法运行。
我尝试将此添加到我的config.xml:
<preference name="android-minSdkVersion" value="14" />
<preference name="android-targetSdkVersion" value="21" />
但这没有任何区别,应用程序只是卡住了。因此,经过一些故障排除后,我认为该应用程序甚至无法拨打 deviceready 电话。
我不知道我是否是唯一遇到此问题的人,但我希望得到一些帮助。 代码如下:
var app = {
initialize: function () {
this.bind();
},
bind: function () {
document.addEventListener('deviceready', this.deviceready, false);
document.addEventListener('online', uploadScans, false);
},
deviceready: function () {
navigator.geolocation.getCurrentPosition(app.getPosition);
tagContents = document.getElementById('tagContents');
if (!window.localStorage.getItem("id")) {
window.localStorage.setItem("id", Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1));
}
document.getElementById('id').innerHTML = "Code unique: " + window.localStorage.getItem("id");
nfc.addNdefListener(
app.onNdef
);
if (device.platform == "Android") {
// Android reads non-NDEF tag. BlackBerry and Windows don't.
nfc.addTagDiscoveredListener(
app.onNfc
);
// Android launches the app when tags with mime type text/pg are scanned
// because of an intent in AndroidManifest.xml.
// phonegap-nfc fires an ndef-mime event (as opposed to an ndef event)
// the code reuses the same onNfc handler
nfc.addMimeTypeListener(
'text/pg',
app.onNdef
);
}
if (app.isOnline()) {
app.uploadScans();
}
},
onNfc: function (nfcEvent) {
var tag = nfcEvent.tag;
app.clearScreen();
app.saveScan(tag);
navigator.notification.vibrate(100);
},
onNdef: function (nfcEvent) {
app.clearScreen();
var tag = nfcEvent.tag;
// BB7 has different names, copy to Android names
if (tag.serialNumber) {
tag.id = tag.serialNumber;
tag.isWritable = !tag.isLocked;
tag.canMakeReadOnly = tag.isLockable;
}
app.saveScan(tag);
navigator.notification.vibrate(100);
},
clearScreen: function () {
tagContents.innerHTML = "";
},
saveScan: function (tag) {
var d = new Date();
var scan = {
"deviceSN": window.localStorage.getItem("id"),
"reference": nfc.bytesToHexString(tag.id),
"scanTime": d.toJSON(),
"latitude": app.pos.latitude,
"longitude": app.pos.longitude
};
var scans = window.localStorage.getItem("scans");
if (scans != null) {
scans = JSON.parse(scans);
scans.scans.push({scan});
scans.scans.totalcount++;
} else {
var scans = {
"scans": [
{
scan
}
],
"totalcount": 1
};
}
tagContents.innerHTML = "Scan added at " + d.toString();
window.localStorage.setItem("scans", JSON.stringify(scans));
if (app.isOnline()) {
app.uploadScans();
}
},
getPosition: function (position) {
app.pos = {"latitude": position.coords.latitude, "longitude": position.coords.longitude};
},
uploadScans: function () {
tagContents.innerHTML += "\n Uploading...";
var http = new XMLHttpRequest();
var post_data = window.localStorage.getItem("scans");
var url = 'http://example.com/scan.php';
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-lenght", post_data.length);
// post the data
http.send(post_data);
deleteScans();
},
isOnline: function () {
var state = navigator.connection.type;
switch (state) {
case "unknown":
return false;
case "ethernet":
return true;
case "wifi":
return true;
case "2d":
return true;
case "3g":
return true;
case "4g":
return true;
case "cell":
return true;
case "none":
return false;
}
}
};
function deleteScans() {
window.localStorage.removeItem("scans");
}
提前谢谢你!
【问题讨论】: