问题
I've spend some time looking around to see if it is possible to access a mobile devices battery status or charge via a web browser api but haven't found any resources or really anything even discussing this topic. This makes me believe it's not possible as it isn't in the spec of whatever reason (security?).
Either way I've found that it is possible to get the battery status / charge percentage with iOS, Cordova, Phonegap, Appcelerator etc.. all of which has an API I might be able to get to work might work for what I am looking into developing. However all of these solutions still would required the app to run as native app — I'm hoping to be able to run it in browser on the device.
Is this possible? If so, what browser API should I be looking into?
回答1:
Thanks Tapani and Michael,
Your help definitely pointed me in the right direction. With the info you provided I was able to find more info on the spec and what browsers support the api.
- Chrome 42.0.2311.90 (64-bit) does not seem to support the battery API.
- Chromium Version 22.0.1225.0 (149762) also does not support the battery API.
- Chrome Canary Version 44.0.2387.0 canary (64-bit) supports the spec on MDN (https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API) but does not support the navigator.battery namespace.
- Firefox supports the navigator.battery namespace but does not support the navigator.getBattery() function.
- Safari Version 8.0.5 (10600.5.17) doesn't support anything referring to the battery api that I can tell.
I didn't get around to checking this on mobile but I don't suspect that mobile browsers would be more consistent api or even have it implemented better than any of the above browsers. Seems fairly new and the browsers organizations are still trying to figure out and are struggling to follow the documented spec. Reminds me a bit of CSS flex-box a bit.
Thanks guys!
回答2:
Battery Status API is not supported in the iOS browser. Desktop Chrome is basically the only browser, which supports it:
http://caniuse.com/#feat=battery-status
回答3:
Seems to be possible. Source: http://www.sitepoint.com/html5-battery-status-api/
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;
if (battery) {
alert(battery.level);
}
回答4:
I've created a demo for you, so you can check if the Battery API is supported by your browser.
Demo: https://elgervanboxtel.nl/blog/#/post/battery-api
As you can see on caniuse.com, it's not widely supported yet.
来源:https://stackoverflow.com/questions/29949928/is-it-possible-to-access-a-mobile-devices-battery-status-or-charge-via-a-web-bro