-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.android.tsx
89 lines (80 loc) · 2.13 KB
/
index.android.tsx
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useEffect } from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';
import {
startFileTransfer,
sendMessage,
watchEvents,
} from 'react-native-wear-connectivity';
import type {
ReplyCallback,
ErrorCallback,
} from 'react-native-wear-connectivity';
import AudioPlayer from '../AudioPlayer';
function CounterScreen() {
const [disabled, setDisabled] = React.useState(false);
const [count, setCount] = React.useState(0);
useEffect(() => {
const unsubscribe = watchEvents.on('message', () => {
setCount((prevCount) => prevCount + 1);
});
return () => {
unsubscribe();
};
}, []);
const onSuccess: ReplyCallback = (result) => {
setDisabled(false);
console.log(result);
};
const onError: ErrorCallback = (error) => console.log(error);
const sendMessageToWear = () => {
setDisabled(true);
const json = { text: 'hello' };
sendMessage(json, onSuccess, onError);
};
const sendFileToWear = async () => {
try {
// @ts-ignore
const result = await launchImageLibrary();
if (!result.assets || result.assets.length === 0) {
console.log('No asset selected');
return;
}
const asset = result.assets[0] || { uri: undefined };
if (asset.uri) {
const filePath = asset.uri.replace('file://', '');
await startFileTransfer(filePath, {});
}
} catch (error) {
console.error('Error in sendFileToWear:', error);
}
};
return (
<View style={styles.container}>
<AudioPlayer />
<Text style={styles.counter}>{count}</Text>
<Button
disabled={disabled}
title="increase counter"
onPress={sendMessageToWear}
/>
<Button title="send file" onPress={sendFileToWear} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 8,
backgroundColor: 'red',
},
counter: {
height: 100,
width: 400,
textAlign: 'center',
fontSize: 50,
fontWeight: 'bold',
},
});
export default CounterScreen;