I'm building a chrome kiosk app in ReactJS that needs to access the deviceId of the device it is running on for logging purposes. I use the following code to call chrome.enterprise.deviceAttributes.getDirectoryDeviceId:
const getDeviceId = (cb) => {
if (!chrome.enterprise) {
if (cb) {
cb();
}
return;
}
// get device id
chrome.enterprise.deviceAttributes.getDirectoryDeviceId(assetId => {
deviceId = assetId;
if (cb) {
cb();
}
});
};
Afterwards, the function is called by doing something along these lines:
getDeviceId(() => {
debug('Got device id: %s', deviceId);
});
Now, I know that this code can only work on a enrolled, ChromeOS device. I also know that the chrome.enterprise.deviceAttributes is only available to extensions that are pre-installed by policy. Because of this I can only test it after it is submitted to the ChromeOS store and is installed via the developer dashboard.
Long story short: the above code doesn't return the deviceId and this causes all my api calls to fail, I can't really debug it because it gets installed as a kiosk app. I was wondering if anyone here could tell me what I'm doing wrong and how I can fix this.
Thanks in advance!
My problem was that I was using the code as a synchronous function while it is an asynchronous function. For the correct usage, look at Tom Dunn's answer on here.