Topic locked

check if flash light is present in device

March 29, 2014 - 3:29am #1

Hello Developers!

I want to check whether an android device has flash LED light or not and couldn't find any method in vuforia (Unity3D project). So I wrote a small plugin to check the same. Following is the android code,

package com.company.product;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
//import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

public class TorchStatus extends UnityPlayerActivity {

    static boolean s = false;
    
    @Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        Log.v("goes in ","on create method");
        
        Context context = this;
		PackageManager packageManager = context.getPackageManager();
 
		if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
			s = true;
		}
		else
		{
			s = false;
		}
    }

        public  String sendFlashStatus()
	{
		if(s==true)
		{
			return "has flash";
		}
		else
		{
			return "no flash";
		}
	}
}

Following is the AndroidManifest file,

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
	android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

	<supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>
	
	<uses-permission android:name="android.permission.CAMERA" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
	
	<application
		android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
	<activity android:name="com.company.product.TorchStatus"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />
			<category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
	</activity>
	
  </application>
</manifest>

And following is the C# code to check if the device has flash,

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;

public class torchtest : MonoBehaviour
{
	AndroidJavaClass testClass;
	string hasFlash;
	
	void Start ()
	{
		testClass = new AndroidJavaClass("com.company.product.TorchStatus");
		hasFlash = testClass.CallStatic<String>("sendFlashStatus");
		Debug.Log ("11111 "+hasFlash);
	}
	
	void OnGUI ()
	{
		GUI.Label (new Rect (150, 20, 100, 60), ""+hasFlash);
    }
}

The above code returns whether the device has flash or not correctly when used independently in my unity project, but when I use it in my Vuforia project, it doesn't return the correct status. To know if the "onCreate" method from android jar is called or not, I have kept a "Log". I don't get the log message when the plugin is used with vuforia.

How do I check whether or not a device has flash LED?

Thanks in advance!

check if flash light is present in device

April 1, 2014 - 2:15am #6

Ok, cool!

check if flash light is present in device

April 1, 2014 - 2:12am #5

Hi Alessandro!

The trick worked. Thanks! Now it looks as if the app has this feature where LED flashes once, everytime the app starts! I can control it via PlayerPrefs but this looks kinda cool and am thinking to keep it as it is.. :)

check if flash light is present in device

March 30, 2014 - 11:50pm #4

I see.  One trick could be to use the Vuforia API anyway, by calling this combination, as soon as the Camera is started:

bool flashIsWorking = CameraDevice.Instance.SetFlashTorchMode ( true );

CameraDevice.Instance.SetFlashTorchMode ( false ); // switch off immediately after

 

and chech the value of flashActivated..  if this is false, it means that the the Flash is not available. 

Then, by storing the flashIsWorking  variable in a private class variable, you could know if the flash functionalty is available, and later trigger it from a Button.

 

check if flash light is present in device

March 30, 2014 - 10:22pm #3

Hello Alessandro!

I have used the method that you mentioned to activate and deactivate torch, but the thing is, I want to display the torch button ONLY IF the device has LED. For example, some Android tablets don't have LED. So in that case, the button should not be displayed on the screen.

I have implemented this in iOS using a *.h and *.m file. But am facing this hurdle for Android.

check if flash light is present in device

March 29, 2014 - 7:17am #2

Have you considered just using the Vuforia API for Flash Torch ?

In Unity, you can simply call:

bool flashActivated = CameraDevice.Instance.SetFlashTorchMode ( true ); 

This should return a boolean value, which is true if the flash was successfully activated, false otherwise.

See also:

https://developer.vuforia.com/resources/api/unity/class_camera_device

 

Topic locked