Skip to content

Commit 746476d

Browse files
Merge pull request #7 from SimformSolutionsPvtLtd/refactor/UNT-T38911_audit_changes
refactor(UNT-T38911): Unify play/stop operations with Promises for android
2 parents b7d70a6 + 72be37c commit 746476d

File tree

3 files changed

+285
-24
lines changed

3 files changed

+285
-24
lines changed

README.md

Lines changed: 255 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,135 @@
22

33
# react-native-haptic-patterns
44

5-
[![react-native-haptic-patterns on npm](https://img.shields.io/npm/v/react-native-haptic-patterns.svg?style=flat)](https://www.npmjs.com/package/react-native-haptic-patterns)
6-
[![react-native-haptic-patterns downloads](https://img.shields.io/npm/dm/react-native-haptic-patterns)](https://www.npmtrends.com/react-native-haptic-patterns)
7-
[![react-native-haptic-patterns install size](https://packagephobia.com/badge?p=react-native-haptic-patterns)](https://packagephobia.com/result?p=react-native-haptic-patterns)
8-
[![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com)
9-
[![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios)
10-
[![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
5+
[![react-native-haptic-patterns on npm](https://img.shields.io/npm/v/react-native-haptic-patterns.svg?style=flat)](https://www.npmjs.com/package/react-native-haptic-patterns) [![react-native-haptic-patterns downloads](https://img.shields.io/npm/dm/react-native-haptic-patterns)](https://www.npmtrends.com/react-native-haptic-patterns) [![react-native-haptic-patterns install size](https://packagephobia.com/badge?p=react-native-haptic-patterns)](https://packagephobia.com/result?p=react-native-haptic-patterns) [![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com) [![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios) [![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
116

127
A React Native library for creating and playing customizable haptic feedback patterns on iOS and Android. Supports advanced pattern recording and playback, enabling developers to deliver rich, tactile experiences in their mobile applications.
138

149
---
1510

11+
## ✨ Features
12+
13+
-**Cross-platform support** - Works seamlessly on both iOS and Android
14+
-**Custom haptic patterns** - Create your own vibration patterns with precise control
15+
-**Pattern recording & playback** - Record and replay complex haptic sequences
16+
-**Core Haptics API** - Leverage iOS's advanced haptic engine (iOS 13+)
17+
-**TypeScript support** - Full type definitions included
18+
-**Simple API** - Easy-to-use methods with Promise-based interface
19+
-**Lightweight** - Minimal dependencies and small bundle size
20+
21+
---
22+
1623
## Quick Access
1724

18-
[Installation](#installation) | [Usage](#usage) | [Methods](#methods) | [Example Code](#import-and-use-in-your-app) | [License](#license)
25+
[Features](#-features) | [Requirements](#requirements) | [Installation](#installation) | [Usage](#usage) | [Methods](#methods) | [Types](#types) | [Examples](#examples) | [Troubleshooting](#troubleshooting) | [License](#license)
26+
27+
---
28+
29+
## Requirements
30+
31+
- **React Native** >= 0.70.0
32+
- **iOS** >= 13.0 (for Core Haptics support)
33+
- **Android** API >= 29 (Android 9.0+)
34+
35+
### Permissions
36+
37+
#### Android
38+
39+
Add the following permission to your `android/app/src/main/AndroidManifest.xml`:
40+
41+
```xml
42+
<uses-permission android:name="android.permission.VIBRATE" />
43+
```
44+
45+
---
1946

2047
## Getting Started
2148

2249
Here's how to get started with `react-native-haptic-patterns` in your React Native project:
2350

2451
### Installation
2552

26-
#### Install the package
53+
**1. Install the package**
2754

2855
```sh
2956
npm install react-native-haptic-patterns
3057
```
3158

32-
Using `Yarn`:
59+
Or using Yarn:
3360

3461
```sh
3562
yarn add react-native-haptic-patterns
3663
```
3764

38-
Install pod for iOS
65+
**2. Install iOS dependencies**
66+
67+
```sh
68+
cd ios && pod install && cd ..
69+
```
70+
71+
Or using npx:
3972

4073
```sh
4174
npx pod-install
4275
```
4376

77+
**3. Configure Android permissions**
78+
79+
Add the vibration permission to `android/app/src/main/AndroidManifest.xml`:
80+
81+
```xml
82+
<uses-permission android:name="android.permission.VIBRATE" />
83+
```
84+
4485
## Usage
4586

87+
### Basic Example
88+
4689
Import and use the library in your React Native app:
4790

48-
```jsx
91+
```tsx
92+
import React from 'react';
93+
import { Button, View } from 'react-native';
94+
import { HapticPatterns } from 'react-native-haptic-patterns';
95+
96+
const MyComponent = () => {
97+
const handlePress = async () => {
98+
try {
99+
// Check if device supports haptics
100+
const isSupported = await HapticPatterns.checkForHapticSupport();
101+
102+
if (isSupported) {
103+
// Play a 200ms haptic pattern
104+
HapticPatterns.playHapticPattern(200);
105+
} else {
106+
console.log('Haptics not supported on this device');
107+
}
108+
} catch (error) {
109+
console.error('Haptic error:', error);
110+
}
111+
};
112+
113+
return (
114+
<View>
115+
<Button onPress={handlePress} title="Feel the Haptic" />
116+
</View>
117+
);
118+
};
119+
```
120+
121+
### Quick Start
122+
123+
```tsx
49124
import { HapticPatterns } from 'react-native-haptic-patterns';
50125

51126
// Check haptic support
52-
await HapticPatterns.checkForHapticSupport();
127+
const isSupported = await HapticPatterns.checkForHapticSupport();
53128

54129
// Play a simple haptic pattern
55-
await HapticPatterns.playHapticPattern(200); // Vibrate for 200ms
130+
HapticPatterns.playHapticPattern(200); // Vibrate for 200ms
56131

57132
// Stop the current haptic pattern
58-
await HapticPatterns.stopHapticPattern();
133+
HapticPatterns.stopHapticPattern();
59134

60135
// Play a recorded pattern
61136
const recordedEvents = [
@@ -67,6 +142,47 @@ const recordedEvents = [
67142
await HapticPatterns.playRecordedPattern(recordedEvents);
68143
```
69144

145+
### Advanced Pattern Example
146+
147+
```tsx
148+
import {
149+
HapticPatterns,
150+
RecordedEventType,
151+
} from 'react-native-haptic-patterns';
152+
153+
const playCustomPattern = () => {
154+
// Create a custom haptic pattern
155+
const pattern: RecordedEventType[] = [
156+
{ startTime: 0, endTime: 100, isPause: false }, // Short vibration
157+
{ startTime: 100, endTime: 200, isPause: true }, // Pause
158+
{ startTime: 200, endTime: 400, isPause: false }, // Longer vibration
159+
{ startTime: 400, endTime: 500, isPause: true }, // Pause
160+
{ startTime: 500, endTime: 600, isPause: false }, // Final vibration
161+
];
162+
163+
try {
164+
HapticPatterns.playRecordedPattern(pattern);
165+
console.log('Pattern playback completed');
166+
} catch (error) {
167+
console.error('Pattern playback error:', error);
168+
}
169+
};
170+
171+
// Use in different scenarios
172+
const provideSuccessFeedback = () => {
173+
HapticPatterns.playHapticPattern(50); // Quick tap
174+
};
175+
176+
const provideErrorFeedback = async () => {
177+
const errorPattern: RecordedEventType[] = [
178+
{ startTime: 0, endTime: 100, isPause: false },
179+
{ startTime: 100, endTime: 150, isPause: true },
180+
{ startTime: 150, endTime: 250, isPause: false },
181+
];
182+
await HapticPatterns.playRecordedPattern(errorPattern);
183+
};
184+
```
185+
70186
## Methods
71187

72188
### checkForHapticSupport
@@ -151,12 +267,136 @@ Returns:
151267

152268
- A Promise that resolves when the entire pattern has finished playing.
153269

270+
---
271+
272+
## Types
273+
274+
### RecordedEventType
275+
276+
Represents a single event in a recorded haptic pattern.
277+
278+
```ts
279+
interface RecordedEventType {
280+
startTime: number; // Start time in milliseconds
281+
endTime: number; // End time in milliseconds
282+
isPause: boolean; // Whether this is a pause (true) or haptic event (false)
283+
}
284+
```
285+
286+
**Example:**
287+
288+
```ts
289+
const pattern: RecordedEventType[] = [
290+
{ startTime: 0, endTime: 100, isPause: false }, // Vibrate for 100ms
291+
{ startTime: 100, endTime: 200, isPause: true }, // Pause for 100ms
292+
{ startTime: 200, endTime: 300, isPause: false }, // Vibrate for 100ms
293+
];
294+
```
295+
296+
---
297+
154298
## Examples
155299

156300
To better understand how to use these methods in a real-world scenario, refer to the following full working example project:
157301

158302
[Example App](./example/src/App.tsx): Demonstrates how to record, play, and reset custom haptic patterns using the library's API in a React Native application.
159303

304+
### Common Use Cases
305+
306+
**Button Press Feedback**
307+
308+
```tsx
309+
<TouchableOpacity
310+
onPress={() => {
311+
HapticPatterns.playHapticPattern(50);
312+
// Handle button action
313+
}}>
314+
<Text>Press Me</Text>
315+
</TouchableOpacity>
316+
```
317+
318+
**Success/Error Notifications**
319+
320+
```tsx
321+
const showSuccessNotification = () => {
322+
HapticPatterns.playHapticPattern(100); // Single haptic
323+
// Show success message
324+
};
325+
326+
const showErrorNotification = async () => {
327+
const errorPattern: RecordedEventType[] = [
328+
{ startTime: 0, endTime: 50, isPause: false },
329+
{ startTime: 50, endTime: 100, isPause: true },
330+
{ startTime: 100, endTime: 150, isPause: false },
331+
];
332+
await HapticPatterns.playRecordedPattern(errorPattern);
333+
// Show error message
334+
};
335+
```
336+
337+
**Long Press Detection**
338+
339+
```tsx
340+
<TouchableOpacity
341+
onLongPress={() => {
342+
HapticPatterns.playHapticPattern(150);
343+
// Handle long press
344+
}}>
345+
<Text>Long Press Me</Text>
346+
</TouchableOpacity>
347+
```
348+
349+
---
350+
351+
## Troubleshooting
352+
353+
### iOS
354+
355+
**Haptics not working on simulator**
356+
357+
- Core Haptics only works on physical devices. Test on a real iPhone (iPhone 8 or newer recommended for best haptic support).
358+
359+
**Build errors after installation**
360+
361+
- Run `cd ios && pod install && cd ..` to ensure CocoaPods are properly installed.
362+
- Clean build folder in Xcode: `Product` > `Clean Build Folder` (Shift + Cmd + K)
363+
- Delete derived data: `rm -rf ~/Library/Developer/Xcode/DerivedData`
364+
365+
**Haptics not working on device**
366+
367+
- Ensure the device supports Core Haptics (iPhone 8 and newer)
368+
- Check that haptic feedback is enabled in device settings
369+
- Verify iOS version is 13.0 or higher
370+
371+
### Android
372+
373+
**Vibration not working**
374+
375+
- Ensure you've added the `VIBRATE` permission to `AndroidManifest.xml`
376+
- Check that vibration is enabled in device settings
377+
- Some devices may have battery optimization that affects vibration
378+
379+
**Build errors**
380+
381+
- Try cleaning the build: `cd android && ./gradlew clean && cd ..`
382+
- Delete build folders: `rm -rf android/app/build`
383+
- Invalidate caches in Android Studio: `File` > `Invalidate Caches / Restart`
384+
385+
**Permission denied errors**
386+
387+
- Verify the `VIBRATE` permission is in the correct location in `AndroidManifest.xml`
388+
- Check that the permission is not being removed by other configurations
389+
390+
### General
391+
392+
**Module not found errors**
393+
394+
- Ensure the package is properly installed: `npm install` or `yarn install`
395+
- Try resetting Metro bundler cache: `npx react-native start --reset-cache`
396+
- Rebuild the app completely: `npx react-native run-ios` or `npx react-native run-android`
397+
398+
---
399+
160400
## Acknowledgements
161401

162402
This library uses and modifies the iOS implementation from [react-native-core-haptics-api](https://github.com/insanj/react-native-core-haptics-api) for customization.
@@ -167,7 +407,7 @@ Support it by joining [stargazers](https://github.com/SimformSolutionsPvtLtd/rea
167407

168408
## Bugs / Feature requests / Feedbacks
169409

170-
For bugs, feature requests, and discussion please use [GitHub Issues](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=bug&late=BUG_REPORT.md&title=%5BBUG%5D%3A), [GitHub New Feature](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=enhancement&late=FEATURE_REQUEST.md&title=%5BFEATURE%5D%3A), [GitHub Feedback](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=enhancement&late=FEATURE_REQUEST.md&title=%5BFEEDBACK%5D%3A)
410+
For bugs, feature requests, and discussion please use [GitHub Issues](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=bug&template=BUG_REPORT.md&title=%5BBUG%5D%3A), [GitHub New Feature](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=enhancement&template=FEATURE_REQUEST.md&title=%5BFEATURE%5D%3A), [GitHub Feedback](https://github.com/SimformSolutionsPvtLtd/react-native-haptic-patterns/issues/new?labels=enhancement&template=FEATURE_REQUEST.md&title=%5BFEEDBACK%5D%3A)
171411

172412
## 🤝 How to Contribute
173413

0 commit comments

Comments
 (0)