1# Pan-sensor Subsystem Changelog
2
3
4## cl.vibrator.1 Added Attributes Related to Custom Vibration
5
6The attributes **VibrateFromFile** (custom vibration effect) and **HapticFileDescriptor** (file descriptor of the custom vibration configuration file) are added. The vibration effect supported by **startVibration** is extended from **VibrateEffect = VibrateTime | VibratePreset** to **VibrateEffect = VibrateTime | VibratePreset | VibrateFromFile**.
7
8**Change Impact**
9
10When developing applications based on OpenHarmony4.0.8.2 and later SDK versions, you can use the **VibrateFromFile** attribute to enable devices that support custom vibration to trigger vibration according to the vibration sequence configured in the custom vibration configuration file.
11
12**Key API/Component Changes**
13
14Added the **VibrateFromFile** and **HapticFileDescriptor** attributes to **@ohos.vibrator.d.ts**.
15
16| Module| Class| Method/Attribute/Enum/Constant| Change Type|
17|  -- | -- | -- | -- |
18| @ohos.vibrator.d.ts | vibrator | HapticFileDescriptor | Added|
19| @ohos.vibrator.d.ts | vibrator | VibrateFromFile | Added|
20
21**Adaptation Guide**
22
23Obtain the resources in the vibration configuration file through the resource management API, and start or stop custom vibration as required.
24
25```ts
26import vibrator from '@ohos.vibrator';
27import resourceManager from '@ohos.resourceManager';
28
29const FILE_NAME = "xxx.json";
30
31async function openResource(fileName) {
32    let fileDescriptor = undefined;
33    let mgr = await resourceManager.getResourceManager();
34    await mgr.getRawFd(fileName).then(value => {
35        fileDescriptor = {fd: value.fd, offset: value.offset, length: value.length};
36        console.log('openResource success fileName: ' + fileName);
37    }).catch(error => {
38        console.log('openResource err: ' + error);
39    });
40    return fileDescriptor;
41}
42
43async function closeResource(fileName) {
44    let mgr = await resourceManager.getResourceManager();
45    await mgr.closeRawFd(fileName).then(()=> {
46        console.log('closeResource success fileName: ' + fileName);
47    }).catch(error => {
48        console.log('closeResource err: ' + error);
49    });
50}
51
52// Obtain the file descriptor of the vibration configuration file.
53let rawFd = openResource(FILE_NAME);
54// To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission.
55try {
56    // Start custom vibration.
57    vibrator.startVibration({
58        type: "file",
59        hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
60    }, {
61        usage: "alarm"
62    }).then(() => {
63        console.info('startVibration success');
64    }, (error) => {
65        console.info('startVibration error');
66    });
67    // Stop vibration in all modes.
68    vibrator.stopVibration(function (error) {
69        if (error) {
70            console.log('error.code' + error.code + 'error.message' + error.message);
71            return;
72        }
73        console.log('Callback returned to indicate successful.');
74    })
75} catch (error) {
76    console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
77}
78// Close the vibration configuration file.
79closeResource(FILE_NAME);
80```
81