Failed to connect to camera service

android

#1

When using tjvantoll/nativescript-flashlight plugin’s flashlight.on() or flashlight.toggle() functions, I get the following error.

An uncaught Exception occurred on "main" thread.
com.tns.NativeScriptException: 
Calling js method onClick failed

Error: java.lang.RuntimeException: Fail to connect to camera service
    android.hardware.Camera.<init>(Camera.java:532)
    android.hardware.Camera.open(Camera.java:361)
    com.tns.Runtime.callJSMethodNative(Native Method)
    com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    com.tns.Runtime.callJSMethod(Runtime.java:957)
    com.tns.Runtime.callJSMethod(Runtime.java:941)
    com.tns.Runtime.callJSMethod(Runtime.java:933)
    com.tns.gen.java.lang.Object_button_19_32_ClickListenerImpl.onClick(Object_button_19_32_ClickListenerImpl.java:12)
    android.view.View.performClick(View.java:5205)
    android.view.View$PerformClick.run(View.java:21164)
    android.os.Handler.handleCallback(Handler.java:739)
    android.os.Handler.dispatchMessage(Handler.java:95)
    android.os.Looper.loop(Looper.java:148)
    android.app.ActivityThread.main(ActivityThread.java:5417)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
File: "file:///data/data/org.nativescript.Browse/files/app/tns_modules/nativescript-flashlight/flashlight.js", line: 14, column: 35

StackTrace: 
	Frame: function:'flashlight.on', file:'file:///data/data/org.nativescript.Browse/files/app/tns_modules/nativescript-flashlight/flashlight.js', line: 14, column: 36
	Frame: function:'toggle', file:'file:///data/data/org.nativescript.Browse/files/app/tns_modules/nativescript-flashlight/flashlight-common.js', line: 8, column: 15
	Frame: function:'flash', file:'file:///data/data/org.nativescript.Browse/files/app/page2.js', line: 12, column: 20
	Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.Browse/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 110, column: 23
	Frame: function:'Observable._emit', file:'file:///data/data/org.nativescript.Browse/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 127, column: 18
	Frame: function:'ClickListenerImpl.onClick', file:'file:///data/data/org.nativescript.Browse/files/app/tns_modules/tns-core-modules/ui/button/button.js', line: 26, column: 23


	at com.tns.Runtime.callJSMethodNative(Native Method)
	at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
	at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
	at com.tns.Runtime.callJSMethod(Runtime.java:957)
	at com.tns.Runtime.callJSMethod(Runtime.java:941)
	at com.tns.Runtime.callJSMethod(Runtime.java:933)
	at com.tns.gen.java.lang.Object_button_19_32_ClickListenerImpl.onClick(Object_button_19_32_ClickListenerImpl.java:12)
	at android.view.View.performClick(View.java:5205)
	at android.view.View$PerformClick.run(View.java:21164)
	at android.os.Handler.handleCallback(Handler.java:739)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5417)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Fail to connect to camera service
	at android.hardware.Camera.<init>(Camera.java:532)
	at android.hardware.Camera.open(Camera.java:361)
	... 16 more

Why is this happening and How to fix it?