Ionic native flashlight not working

Hey there,

i do my first steps with ionic native and wanted to try a simple flashlight app.

i installed and imported this plugin: https://ionicframework.com/docs/native/flashlight/

I followed the instructions “installation” and “usage”. So i added the following simple code in home.html

<ion-content>
<button ion-button (click)="flashlight()">Flashlight</button>
</ion-content>

so i have got a simple button which should do some action (of course not at the same time :slight_smile: )

so i also added this code in home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Flashlight } from '@ionic-native/flashlight';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private flashlight: Flashlight) {}

flashlight(){
  check = this.flashlight.isSwitchedOn();
  alert(check);
}
}

The app is built without errors and i installed it on a xperia z1 compact and huawei p9 lite. the following behaviour is shown:

toggle-function - no effect

isSwitchedOn - always returns false

switchOn - fires once, but if i tried another time nothing happens. i have to deinstall the app and install it again to fire one more time - but only once again.

Has anybody an idea what i am doing wrong?

What exactly are you talking about here? Your code only shows some usage of isSwitchedOn().

i wanted to post just one example of code.

of course i tried the functions seperately:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Flashlight } from '@ionic-native/flashlight';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private flashlight: Flashlight) {}

flashlight(){
  this.flashlight.switchOn();
}
}

fires once, but if i tried another time nothing happens. i have to deinstall the app and install it again to fire one more time - but only once again.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Flashlight } from '@ionic-native/flashlight';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private flashlight: Flashlight) {}

flashlight(){
 this.flashlight.toggle();
}
}

does nothing

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Flashlight } from '@ionic-native/flashlight';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private flashlight: Flashlight) {}

flashlight(){
  check = this.flashlight.isSwitchedOn();
  alert(check);
}
}

always returns false

is it more understandable?

Yes, now I understand.

In general Native functionality has to wait for platform.ready so the plugin is actually loaded. You might want to try that.

1 Like

great. now it works.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Platform } from 'ionic-angular';
import { Flashlight } from '@ionic-native/flashlight';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private flashlight: Flashlight,
    private platform: Platform) {}
    
    flashlight(){
      this.flashlight.toggle();
      this.platform.ready().then(() => {
       var check = this.flashlight.isSwitchedOn();
       alert(check);
      });
     }
}

would be nice if the note “platform.ready recommended” would be posted on the official docs :slight_smile: but ok as long as there is an active community. thanks a lot. i´ll do more experiments with ionic and cordova :slight_smile:

The toggle() call should also be inside the Platform.ready()-protected zone. All interaction with cordova must be.

This issue here could use a +1: Ionic Native and platform.ready() · Issue #1880 · danielsogl/awesome-cordova-plugins · GitHub
Or maybe even a comment how much time that cost you to find out…