For this problem you should:
Check whether the flashlight is available or not.
If so then Turn Off/On
If not then you can do whatever, according to your app needs.
To determine whether the device has flash:
You can use the following:
This will return true in the presence of a flash and false otherwise.