forked from Tintef/react-native-headphone-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (37 loc) · 1.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useState, useEffect } from 'react';
import { NativeModules, NativeEventEmitter, EventSubscription } from 'react-native';
export interface ConnectedResult {
audioJack: boolean
bluetooth: boolean
}
const RNHeadphoneDetection = NativeModules.RNHeadphoneDetection;
const eventEmitter = new NativeEventEmitter(RNHeadphoneDetection);
export interface IHeadphoneDetection {
isAudioDeviceConnected: () => Promise<ConnectedResult>
AUDIO_DEVICE_CHANGED_NOTIFICATION: string
addListener: (callback: (connection: ConnectedResult) => void) => EventSubscription
}
const HeadphoneDetection: IHeadphoneDetection = {
...RNHeadphoneDetection,
addListener(callback) {
return eventEmitter.addListener(
RNHeadphoneDetection.AUDIO_DEVICE_CHANGED_NOTIFICATION,
callback
);
},
};
export const useHeadphonesDetection = () => {
const [result, setResult] = useState<ConnectedResult>({
audioJack: false,
bluetooth: false
})
useEffect(() => {
HeadphoneDetection.isAudioDeviceConnected().then(setResult);
const subscription = HeadphoneDetection.addListener(setResult);
return () => {
subscription.remove();
};
}, []);
return result;
}
export default HeadphoneDetection;