-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
246 lines (233 loc) · 7.24 KB
/
App.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
NativeEventEmitter,
NativeModules,
Platform,
PermissionsAndroid,
TextInput
} from 'react-native';
import BleManager from 'react-native-ble-manager';
import { bytesToString, stringToBytes } from 'convert-string';
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
const deviceName = 'VEEPEAK';
// const App: () => React$Node = () => {
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
device: '',
service: 'FFF0',
notify: 'FFF1',
write: 'FFF2',
message: 'Press CONNECT to start!',
output: 'See the command result here!',
text: '',
value: '',
};
}
componentDidMount() {
BleManager.start({ showAlert: false });
if (Platform.OS === 'android' && Platform.Version >= 23) {
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
.then((result) => {
if (result) {
console.log('Permission is OK!');
} else {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
.then((result) => {
if (result) {
console.log('User accept!');
} else {
console.log('User refuse!');
}
});
}
});
}
this.handleUpdate = bleManagerEmitter.addListener(
'BleManagerDidUpdateValueForCharacteristic', this.handleUpdate);
}
clear = async () => {
this.setState({ text: '', value: '' });
try {
await BleManager.refreshCache(this.state.device);
console.log('Cache refreshed!')
} catch (error) {
console.log('Error: ', error);
}
}
connect = async () => {
this.setState({ message: 'Connecting to the device!' });
await BleManager.enableBluetooth();
console.log('The bluetooth is already enabled or the user confirm!');
const bondedPeripheralsArray = await BleManager.getBondedPeripherals([]);
bondedPeripheralsArray.forEach((element) => {
if (element.name === deviceName) {
this.setState({ device: element.id });
}
});
try {
await BleManager.connect(this.state.device);
console.log('Connected to ' + this.state.device);
this.setState({ message: 'Connected!' });
} catch (error) {
console.log('Error: ', error);
this.setState({ output: 'Error: ' + error, message: 'Press CONNECT to start!' });
}
}
disconnect = async () => {
try {
await BleManager.disconnect(this.state.device);
console.log('Disconnected!');
this.setState({
device: '',
message: 'Disconnected! Press CONNECT again!',
value: ''
});
} catch (error) {
console.log('Error: ', error);
this.setState({ output: 'Error: ' + error });
}
}
handleUpdate = (data) => {
console.log('Received data from ' + data.peripheral + ' characteristic ' + data.characteristic, data.value);
const tempValue = this.state.value;
const value = bytesToString(data.value);
const output = tempValue + 'Response: ' + value + '\n';
this.setState({ value: output })
}
write = async () => {
const command = this.state.text + '\r';
const data = stringToBytes(command);
try {
await BleManager.startNotification(this.state.device,
this.state.service, this.state.notify);
await BleManager.writeWithoutResponse(this.state.device,
this.state.service, this.state.write, data);
console.log('Writed: ' + data);
} catch (error) {
console.log('Error: ', error);
this.setState({ output: 'Error: ' + error });
}
}
render() {
return (
<>
{/* <StatusBar barStyle="dark-content" /> */}
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
{/* <Header /> */}
{/* {global.HermesInternal == null ? null : (
<View style={styles.engine}>
<Text style={styles.footer}>Engine: Hermes</Text>
</View>
)} */}
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Test OBD</Text>
<Text style={styles.sectionDescription}>
{this.state.message}
</Text>
</View>
<View style={styles.sectionContainer}>
<View style={{ padding: 10, borderColor: '#DAE1E7', borderWidth: 1, borderRadius: 10 }}>
<TextInput
style={{ height: 40 }}
placeholder='Type your command here to test!'
onChangeText={(text) => this.setState({ text })}
value={this.state.text}
/>
</View>
</View>
<View style={styles.sectionContainer}>
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
<Button
title='Clear'
onPress={this.clear}
/>
<Button
onPress={this.state.device ? this.disconnect : this.connect}
title={this.state.device ? 'Disconnect' : 'Connect'}
/>
<Button
title='Test'
onPress={this.write}
/>
</View>
</View>
<View style={styles.sectionContainer}>
<View style={{ padding: 10, borderColor: '#DAE1E7', borderWidth: 1, borderRadius: 10 }}>
<TextInput
style={{ height: '60%' }}
placeholder={this.state.output}
editable={false}
multiline={true}
numberOfLines={25}
onChangeText={(text) => this.setState({ text })}
value={this.state.value}
/>
</View>
</View>
{/* <View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Learn More</Text>
<Text style={styles.sectionDescription}>
Read the docs to discover what to do next:
</Text>
</View> */}
{/* <LearnMoreLinks /> */}
</View>
</ScrollView>
</SafeAreaView>
</>
);
}
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: '#F3F3F3',
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: '#1292B4',
},
sectionContainer: {
marginTop: 42,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: '#000',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: '#444',
},
highlight: {
fontWeight: '700',
},
footer: {
color: '#444',
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;