device.uuid undefined in PhoneGap on Android?
I'm writing a PhoneGap application that sends requests to a central database. It needs to be able to identify the unique devices that connect to it.
PhoneGap's device.uuid
property would seem to do the trick. On webOS and iPhone, I get back a unique string of alphanumeric characters, which will satisfy my need. However, the Android devices I've tested (Motorola Droid and the Android SDK emulator, both running Android 2.1) return "undefined" as the device.uuid
.
device.platform
and device.name
return correct values on all three platforms, so the problem doesn't have to do with the device
object开发者_如何转开发 itself (it's defined in the code blocks where I use it).
Is this an Android limitation? A problem with PhoneGap?
Is there any other way to get such a globally unique identifier if not through device.uuid
?
EDIT: It appears that the deviceready
event is never getting fired, which needs to happen before the device.uuid
property becomes available.
Depending on Phonegap version and/or device, you might need to install the plugin explicitly. In our project, device was present but device.uuid was undefined. Running cordova plugin add org.apache.cordova.device
fixed the issue in our case.
I haven't yet found a solution for this, but it is worth noting that this issue is fixed in Android 2.2. But other than that, you'll have to find some other way of getting a unique device identifier if the device is running 2.1 or earlier.
I had the same problem. It was working previously, and I eventually traced the problem back to the manifest permissions.
If android.permission.ACCESS_NETWORK_STATE
is not enabled, deviceready
will not fire, and device.uuid
will not be available.
this works for me:
first installation of device plugin:
cordova plugin add cordova-plugin-device
and in my index.js i have this:
var app = {
initialize: function() {
$.support.cors = true;
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
receivedEvent: function(id) {
console.log('Received Event: ' + id);
},
getDeviceInfo: function() {
var aio = new Object();
aio.agent = device.platform + "," + device.version + "," + device.model;
aio.deviceid = device.uuid;
return aio;
}
};
app.initialize();
module.controller('AppController',function($scope) {
console.log('GOOOO');
ons.ready(function() {
console.log("ons ready");
var appInfoObj = app.getDeviceInfo();
console.log("Agent: " + appInfoObj.agent);
console.log("UUID: " + appInfoObj.deviceid);
});
});
精彩评论