-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
161 lines (142 loc) · 4.16 KB
/
App.js
File metadata and controls
161 lines (142 loc) · 4.16 KB
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from "react";
import { Platform, StyleSheet, Text, View, TouchableOpacity } from "react-native";
import { Buffer } from "buffer";
import { BleManager } from "react-native-ble-plx";
const instructions = Platform.select({
ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
android:
"Double tap R on your keyboard to reload,\n" +
"Shake or press menu button for dev menu"
});
const PiServiceID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const PiCharacteristicIDButton = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
const PiCharacteristicIDSend = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.manager = new BleManager();
this.device = null;
this.state = {
ble: "Unknown",
connected: false
};
this.onPress = this.onPress.bind(this);
}
componentDidMount() {
this.manager.onStateChange(state => {
// returns (const) subscription, which could be used to cancel event subscription
console.log("BLE state:", state);
this.setState({ ble: state });
if (state === "PoweredOn") {
this.scanAndConnect();
//subscription.remove();
}
}, true);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Bluetooth: {this.state.ble}</Text>
<Text style={styles.welcome}>
Device: {this.state.connected ? "Connected" : "Not Connected"}
</Text>
<View
style={this.state.buttonPressed ? styles.buttonOn : styles.buttonOff}
/>
<TouchableOpacity onPress={this.onPress}>
<Text>Send Hej</Text>
</TouchableOpacity>
</View>
);
}
scanAndConnect() {
console.log("Scanning for MarcusBLE");
this.setState({ connected: false });
this.device = null;
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
console.log(error);
// Handle error (scanning will be stopped automatically)
return;
}
// Check if it is a device you are looking for based on advertisement data
// or other criteria.
if (device.name === "MarcusBLE") {
// Stop scanning as it's not necessary if you are scanning for one device.
this.manager.stopDeviceScan();
// Proceed with connection.
device
.connect()
.then(device => {
console.log("Connected to MarcusBLE");
this.device = device;
return device.discoverAllServicesAndCharacteristics();
})
.then(device => {
this.setState({ connected: true });
this.manager.monitorCharacteristicForDevice(
device.id,
PiServiceID,
PiCharacteristicIDButton,
(error, characteristic) => this.onButton1(error, characteristic)
);
})
.catch(error => {
// Handle errors
console.log(error);
});
}
});
}
onButton1(error, characteristic) {
if (error && error.errorCode === 201) {
return this.scanAndConnect();
}
const value = Buffer.from(characteristic.value, "base64").toString("ascii");
console.log("Button 1 ", value);
this.setState({ buttonPressed: true });
setTimeout(() => this.setState({ buttonPressed: false }), 500);
}
onPress() {
const value = "Hej";
const valueEncoded = Buffer.from(value, "ascii").toString("base64");
if (this.device) {
this.manager.writeCharacteristicWithResponseForDevice(
this.device.id,
PiServiceID,
PiCharacteristicIDSend,
valueEncoded
);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10
},
buttonOn: {
width: 200,
height: 30,
backgroundColor: "green"
},
buttonOff: {
width: 200,
height: 30
}
});