by Eddy Verbruggen for iOS, Android and WP8
- Description
- Installation 2. Automatically (CLI / Plugman) 2. Manually 2. PhoneGap Build
- Usage
- Credits
- License
This plugin allows you to switch the flashlight / torch of the device on and off.
- Works on Android 2.x and newer.
- Works on iOS 5.0+, maybe even lower.
- Works on Windows Phone 8 (since v2.0.0).
- Depends on capabilities of the device, so you can test it with an API call.
- Compatible with Cordova Plugman.
- Pending review at PhoneGap Build.
Flashlight is compatible with Cordova Plugman, compatible with PhoneGap 3.0 CLI, here's how it works with the CLI:
$ cordova plugin add https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin.git
$ cordova prepare
Then reference Flashlight.js
in index.html
, after cordova.js
/phonegap.js
. Mind the path:
<script type="text/javascript" src="js/plugins/Flashlight.js"></script>
1. Add the following xml to your config.xml
in the root directory of your www
folder:
<!-- for iOS -->
<feature name="Flashlight">
<param name="ios-package" value="Flashlight" />
</feature>
<!-- for Android -->
<feature name="Flashlight">
<param name="android-package" value="nl.xservices.plugins.Flashlight" />
</feature>
<!-- for WP8 -->
<feature name="Flashlight">
<param name="wp-package" value="Flashlight" />
</feature>
2. For Android, add the following xml to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA"/>
3. For WP8, add the following xml to your Properties/WMAppManifest.xml
:
<Capability Name="ID_CAP_ISV_CAMERA"/>
<!-- you can try without this one, but I found I needed it on my Nokia Lumia 925, so it's enabled when using auto-install -->
<Capability Name="ID_CAP_MICROPHONE"/>
4. Grab a copy of Flashlight.js, add it to your project and reference it in index.html
:
<script type="text/javascript" src="js/plugins/Flashlight.js"></script>
5. Download the source files and copy them to your project.
iOS: Copy Flashlight.h
and Flashlight.h
to platforms/ios/<ProjectName>/Plugins
Android: Copy Flashlight.java
to platforms/android/src/nl/xservices/plugins
(create the folders)
WP8: Copy Flashlight.cs
to platforms/wp8/Plugins/nl.x-services.plugins.flashlight
(create the folders)
Flashlight works with PhoneGap build too! Compatible with PhoneGap 3.0.0 and up.
Just add the following xml to your config.xml
to always use the latest version of this plugin:
<gap:plugin name="nl.x-services.plugins.flashlight" />
or to use this exact version:
<gap:plugin name="nl.x-services.plugins.flashlight" version="2.0.0" />
Flashlight.js is brought in automatically (since v2.0.0). There is no need to change or add anything in your html.
window.plugins.flashlight.available(function(isAvailable) {
if (isAvailable) {
// switch on
window.plugins.flashlight.switchOn(); // success/error callbacks may be passed
// switch off after 3 seconds
setTimeout(function() {
window.plugins.flashlight.switchOff(); // success/error callbacks may be passed
}, 3000);
} else {
alert("Flashlight not available on this device");
}
});
As an alternative to switchOn
and switchOff
, you can use the toggle
function
window.plugins.flashlight.toggle(); // success/error callbacks may be passed
A hint for Android developers
: you'll want to make sure the torch is switched off when the app is exited via the backbutton.
Otherwise, the camera may be locked so it can't be used by other apps:
document.addEventListener("backbutton", function() {
// pass exitApp as callbacks to the switchOff method
window.plugins.flashlight.switchOff(exitApp, exitApp);
}, false);
function exitApp() {
navigator.app.exitApp();
}
- This plugin was streamlined and enhanced for Plugman / PhoneGap Build by Eddy Verbruggen.
- The Android code was inspired by the PhoneGap Torch plugin.
- Thanks to HuaHub for making me fix a camera lock issue on Android.
- The iOS code was inspired by Tom Schreck.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.