1# Using the Flashlight (ArkTS) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8To use the flashlight mode, you manipulate your phone to turn on the flashlight, which then stays on persistently. 9 10When you use the flashlight mode with a camera application, the following situations may occur: 11 12- When the rear camera is used and [FlashMode](../../reference/apis-camera-kit/arkts-apis-camera-e.md#flashmode) is set to off, the flashlight cannot be turned on. 13- When the front camera is used, the flashlight can be turned on and remains steady on. 14- When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously. 15 16## How to Develop 17 18Read [Module Description](../../reference/apis-camera-kit/arkts-apis-camera.md) for the API reference. 19 201. Import the camera module, which provides camera-related attributes and methods. 21 22 ```ts 23 import { camera } from '@kit.CameraKit'; 24 import { BusinessError } from '@kit.BasicServicesKit'; 25 ``` 26 272. Call [isTorchSupported](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#istorchsupported11) in the [CameraManager](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md) class to check whether the current device supports the flashlight. 28 29 ```ts 30 function isTorchSupported(cameraManager: camera.CameraManager) : boolean { 31 let torchSupport: boolean = false; 32 try { 33 torchSupport = cameraManager.isTorchSupported(); 34 } catch (error) { 35 let err = error as BusinessError; 36 console.error('Failed to torch. errorCode = ' + err.code); 37 } 38 console.info('Returned with the torch support status:' + torchSupport); 39 return torchSupport; 40 } 41 ``` 42 433. Call [isTorchModeSupported](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#istorchmodesupported11) in the [CameraManager](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md) class to check whether a specific [TorchMode](../../reference/apis-camera-kit/arkts-apis-camera-e.md#torchmode11) is supported. 44 45 ```ts 46 function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : boolean { 47 let isTorchModeSupport: boolean = false; 48 try { 49 isTorchModeSupport = cameraManager.isTorchModeSupported(torchMode); 50 } catch (error) { 51 let err = error as BusinessError; 52 console.error('Failed to set the torch mode. errorCode = ' + err.code); 53 } 54 return isTorchModeSupport; 55 } 56 ``` 57 584. Call [setTorchMode](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#settorchmode11) in the [CameraManager](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md) class to set the flashlight mode, and then [getTorchMode](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#gettorchmode11) in the [CameraManager](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md) class to obtain the flashlight mode in use. 59 60 > **NOTE** 61 > 62 > Before using [getTorchMode](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#gettorchmode11), register a listener for the flashlight status. For details, see [Status Listening](camera-torch-use.md#status-listening). 63 64 ```ts 65 function setTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : void { 66 cameraManager.setTorchMode(torchMode); 67 let isTorchMode = cameraManager.getTorchMode(); 68 console.info(`Returned with the torch mode supportd mode: ${isTorchMode}`); 69 } 70 ``` 71 72 73## Status Listening 74 75During camera application development, you can listen for the flashlight status, including on, off, unavailable, and available. 76 77Register the **'torchStatusChange'** event and return the listening result through a callback, which carries the **TorchStatusInfo** parameter. For details about the parameter, see [TorchStatusInfo](../../reference/apis-camera-kit/arkts-apis-camera-i.md#torchstatusinfo11). 78 79 80```ts 81function onTorchStatusChange(cameraManager: camera.CameraManager): void { 82 cameraManager.on('torchStatusChange', (err: BusinessError, torchStatusInfo: camera.TorchStatusInfo) => { 83 if (err !== undefined && err.code !== 0) { 84 console.error(`Callback Error, errorCode: ${err.code}`); 85 return; 86 } 87 console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo. 88 isTorchActive}, level: ${torchStatusInfo.torchLevel}`); 89 }); 90} 91``` 92