1# Using the Flashlight (ArkTS)
2
3To use the flashlight mode, you manipulate your phone to turn on the flashlight, which then stays on persistently.
4
5When you use the flashlight mode with a camera application, the following situations may occur:
6- When the rear camera is used and [FlashMode](../../reference/apis-camera-kit/js-apis-camera.md#flashmode) is set to off, the flashlight cannot be turned on.
7- When the front camera is used, the flashlight can be turned on and remains steady on.
8- When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously.
9
10## How to Develop
11
12Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference.
13
141. Import the camera module, which provides camera-related attributes and methods.
15
16    ```ts
17    import { camera } from '@kit.CameraKit';
18    import { BusinessError } from '@kit.BasicServicesKit';
19    ```
20
212. Call [isTorchSupported](../../reference/apis-camera-kit/js-apis-camera.md#istorchsupported11) in the [CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to check whether the current device supports the flashlight.
22
23    ```ts
24    function isTorchSupported(cameraManager: camera.CameraManager) : boolean {
25        let torchSupport: boolean = false;
26        try {
27            torchSupport = cameraManager.isTorchSupported();
28        } catch (error) {
29            let err = error as BusinessError;
30            console.error('Failed to torch. errorCode = ' + err.code);
31        }
32        console.info('Returned with the torch support status:' + torchSupport);
33        return torchSupport;
34    }
35    ```
36
373. Call [isTorchModeSupported](../../reference/apis-camera-kit/js-apis-camera.md#istorchmodesupported11) in the [CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to check whether a specific [TorchMode](../../reference/apis-camera-kit/js-apis-camera.md#torchmode11) is supported.
38
39    ```ts
40    function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : boolean {
41        let isTorchModeSupport: boolean = false;
42        try {
43            isTorchModeSupport = cameraManager.isTorchModeSupported(torchMode);
44        } catch (error) {
45            let err = error as BusinessError;
46            console.error('Failed to set the torch mode. errorCode = ' + err.code);
47        }
48        return isTorchModeSupport;
49    }
50    ```
51
524. Call [setTorchMode](../../reference/apis-camera-kit/js-apis-camera.md#settorchmode11) in the [CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to set the flashlight mode, and then [getTorchMode](../../reference/apis-camera-kit/js-apis-camera.md#gettorchmode11) in the [CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to obtain the flashlight mode in use.
53
54    ```ts
55    function setTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : void {
56        cameraManager.setTorchMode(torchMode);
57        let isTorchMode = cameraManager.getTorchMode();
58        console.info(`Returned with the torch mode supportd mode: ${isTorchMode}`);
59    }
60    ```
61
62## Status Listening
63
64During camera application development, you can listen for the flashlight status, including on, off, unavailable, and available.
65
66Register 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/js-apis-camera.md#torchstatusinfo11).
67
68
69```ts
70function onTorchStatusChange(cameraManager: camera.CameraManager): void {
71    cameraManager.on('torchStatusChange', (err: BusinessError, torchStatusInfo: camera.TorchStatusInfo) => {
72        if (err !== undefined && err.code !== 0) {
73            console.error(`Callback Error, errorCode: ${err.code}`);
74            return;
75        }
76        console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo.
77            isTorchActive}, level: ${torchStatusInfo.torchLevel}`);
78    });
79}
80```
81