# Flashlight Implementation

**URL:** <https://forums.fusetools.com/t/flashlight-implementation/4922>\
**Category:** General\
**Created:** [November 18, 2016, 6:15pm UTC](https://forums.fusetools.com/t/flashlight-implementation/4922 "2016-11-18T18:15:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ana\_Almeida](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Ana\_Almeida](https://forums.fusetools.com/u/Ana_Almeida)\
**Post date:** [November 18, 2016, 6:15pm UTC](https://forums.fusetools.com/t/flashlight-implementation/4922/1 "2016-11-18T18:15:58Z")

</div>

Hi all, I’m new to Fuse and just to get started with Uno and Foreign Code I thought of making a simple app which turns on and off the flashlight. I’m starting with Android and until now I only can ask for permissions and check if the device has a camera. I’m hoping someone could give me some tips how to do this or tell me what I’m doing wrong.

My uno code:

´´´  
public class Flashlight : NativeModule  
{  
bool isFlashlightOn = false;  
bool permissionFlashlight = false;

```
public Flashlight(){
    if defined(Android){
        //Ask for permissions
        Permissions.Request(Permissions.Android.CAMERA).Then(PermissionAccepted, PermissionRejected);
    }
    
    //Members
    AddMember(new NativeFunction("getPermissionStatus", (NativeCallback)GetPermissionStatus));
    AddMember(new NativeFunction("hasFlashlight", (NativeCallback)HasFlashlight));
    AddMember(new NativeFunction("turnFlashlightOn", (NativeCallback)TurnFlashlightOn));
    AddMember(new NativeFunction("turnFlashlightOff", (NativeCallback)TurnFlashlightOff));
}

//If permission was accepted
private void PermissionAccepted(PlatformPermission permission){
    permissionFlashlight = true;
    debug_log "Permited: " + permission;
}

//If permission was denied
private void PermissionRejected(Exception reason){
    permissionFlashlight = false;
    debug_log "Error: " + reason.Message;
}

//Get the status of the permissions. true if accepted, otherwise false
object GetPermissionStatus(Context c, object[] args){
    return permissionFlashlight;                
}

//Check if the device supports flashlight
object HasFlashlight(Context c, object[] args){
    return HasFlashlight();                
}

//Turn the flashlight on
object TurnFlashlightOn(Context c, object[] args){
    TurnFlashlightOn();  
    return null;           
}

//Turn the flashlight off
object TurnFlashlightOff(Context c, object[] args){
    TurnFlashlightOff();  
    return null;        
}
    
[Foreign(Language.Java)]
static extern(Android) bool HasFlashlight()
@{
    PackageManager pm = com.fuse.Activity.getRootActivity().getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
@}

[Foreign(Language.Java)]
static extern(Android) void TurnFlashlightOn()
@{
    Camera cam = Camera.open();
    Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
@}

[Foreign(Language.Java)]
static extern(Android) void TurnFlashlightOff()
@{
    Camera cam = Camera.open();
    Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_OFF);
    cam.setParameters(p);
    cam.stopPreview();
@}  

```

static extern(!(iOS||Android)) bool HasFlashlight(){  
return false;  
}

```
static extern(!(iOS||Android)) void TurnFlashlightOn(){
    return false;
}

static extern(!(iOS||Android)) void TurnFlashlightOff(){
    return false;
}

```

}  
´´´

(sorry for the formatting, dunno what happened.)

---

<div class="post-metadata">

**Author:** ![Anders\_Schau\_Knatten](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_schau_knatten/32/41_2.png) [@Anders\_Schau\_Knatten](https://forums.fusetools.com/u/Anders_Schau_Knatten)\
**Post date:** [November 29, 2016, 3:15pm UTC](https://forums.fusetools.com/t/flashlight-implementation/4922/2 "2016-11-29T15:15:01Z")

</div>

Hi, what exactly is the problem you’re having?
