forked from priyanka-Sadh/react-native-ble-plx-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
282 lines (260 loc) · 11.9 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { Component } from 'react';
import { Platform, View, NativeModules, NativeEventEmitter,PermissionsAndroid } from 'react-native';
import { BleManager } from 'react-native-ble-plx';
import { Container, Header, Content, Footer, FooterTab, Button, Icon, Text, Card, CardItem, Body,Toast,Root} from 'native-base';
import base64 from 'react-native-base64';
import moment from 'moment';
let count = 0;
const transactionId ="moniter";
import showToast from './toast'
export default class Ble_test extends Component {
constructor() {
super()
this.manager = new BleManager()
this.state = {
deviceid : '', serviceUUID:'', characteristicsUUID : '', text1 : '',makedata : [],showToast: false,
notificationReceiving : false
}
}
componentWillUnmount() {
this.manager.cancelTransaction(transactionId)
this.manager.stopDeviceScan();
this.manager.destroy();
delete this.manager;
}
UNSAFE_componentWillMount() {
this.manager = new BleManager()
if (Platform.OS === 'android' && Platform.Version >= 23) {
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
if (result) {
console.log("Permission is OK");
// this.retrieveConnected()
} else {
PermissionsAndroid.requestPermission(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
if (result) {
console.log("User accept");
} else {
console.log("User refuse");
}
});
}
});
}
}
getServicesAndCharacteristics(device) {
return new Promise((resolve, reject) => {
device.services().then(services => {
const characteristics = []
console.log("ashu_1",services)
services.forEach((service, i) => {
service.characteristics().then(c => {
console.log("service.characteristics")
characteristics.push(c)
console.log(characteristics)
if (i === services.length - 1) {
const temp = characteristics.reduce(
(acc, current) => {
return [...acc, ...current]
},
[]
)
const dialog = temp.find(
characteristic =>
characteristic.isWritableWithoutResponse
)
if (!dialog) {
reject('No writable characteristic')
}
resolve(dialog)
}
})
})
})
})
}
stopNotication(){
this.manager.cancelTransaction(transactionId)
this.setState({notificationReceiving:false})
}
disconnect(){
return new Promise((resolve, reject) => {
this.manager.cancelDeviceConnection(this.state.deviceid).
then(rest=>{
console.log(rest);
let cleanState = {};
Object.keys(this.state).forEach(x => {
if(x=='makedata'){cleanState[x] = []} else{cleanState[x] = null}
});
this.setState(cleanState);
})
.catch((err)=>console.log("error on cancel connection",err))
})
}
async writeMesage(code, message){
this.manager.cancelTransaction(transactionId)
var device= this.state.device;
const senddata = base64.encode(message);
if(device)
{
device.writeCharacteristicWithResponseForService(this.state.serviceUUID, this.state.characteristicsUUID, senddata).then((characteristic) => {
console.log("write response");
console.log(characteristic);
this.alert(message,"success")
//Sent message and start receiving data
console.log("device")
console.log(this.state.serviceUUID,"device",this.state.characteristicsUUID)
console.log(this.state.device)
let snifferService = null
var SERVICE_SNIFFER_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
var SNIFFER_VOLTAGE_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
device.services().then(services => {
let voltageCharacteristic = null
snifferService = services.filter(service => service.uuid === this.state.serviceUUID)[0]
snifferService.characteristics().then(characteristics => {
console.log("characteristics characteristics")
console.log(characteristics)
this.setState({notificationReceiving:true})
// voltageCharacteristic is retrieved correctly and data is also seems correct
voltageCharacteristic = characteristics.filter(c => c.uuid === characteristics[0].uuid)[0]
voltageCharacteristic.monitor((error, c) => {
// RECEIVED THE ERROR HERE (voltageCharacteristic.notifiable === true)
if(error){
console.log("error in monitering",error)
return;
}
else{
// console.log("c",base64.decode(c.value))
const data1 = base64.decode(c.value);
var s = data1.split(" ");
var s1 = parseInt(s[1]);
if(isNaN(s1)) {count++;}
else{
if(count == 1){
this.state.makedata.push(<Text key={moment().valueOf()}>{s[0]} : {s1/1000} {"\n"} </Text>);
this.setState({dateTime : "Data Received at : "+moment().format("MMMM Do, h:mm:ss a"),makedata:this.state.makedata});
}
if(count == 3){count = 0;this.setState({makedata:[]})}
}
}
},transactionId)
}).catch(error => console.log(error))
})
return
}).catch((error) => {
this.alert("error in writing"+JSON.stringify(error))
})
}
else{
this.alert("No device is connected")
}
}
alert(message,type="danger"){
Toast.show({
text: message,
buttonText: 'Okay',
duration: 5000,
type: type,
Animated : false
})
}
async scanAndConnect() {
this.setState({text1:"Scanning..."})
this.manager.startDeviceScan(null, null, (error, device) => {
console.log("Scanning...");
if (null) {
console.log('null')
}
if (error) {
this.alert("Error in scan=> "+error)
this.setState({text1:""})
this.manager.stopDeviceScan();
return
}
if( /[_]/g.test( device.name ) )
{
let nameSplit = device.name.split('_');
if(nameSplit[0] == 'TAPP'){ //T3X1 //TAPP
const serviceUUIDs= device.serviceUUIDs[0]
this.setState({text1:"Connecting to "+device.name})
this.manager.stopDeviceScan();
//listener for disconnection
/* this.manager.onDeviceDisconnected(device.id, (error, device) => {
console.log(error);
console.log("errordddd",device);
// if(this.props.device.isConnected) {
// this.scanAndConnect()
// }
});*/
this.manager.connectToDevice(device.id, {autoConnect:true}).then((device) => {
(async () => {
const services = await device.discoverAllServicesAndCharacteristics()
const characteristic = await this.getServicesAndCharacteristics(services)
console.log("characteristic")
console.log(characteristic)
console.log("Discovering services and characteristics",characteristic.uuid);
this.setState({"deviceid":device.id, serviceUUID:serviceUUIDs, characteristicsUUID : characteristic.uuid,device:device })
this.setState({text1:"Conneted to "+device.name})
})();
this.setState({device:device})
return device.discoverAllServicesAndCharacteristics()
}).then((device) => {
// return this.setupNotifications(device)
}).then(() => {
console.log("Listening...")
}, (error) => {
this.alert("Connection error"+JSON.stringify(error))
})
}
}
});
}
render() {
return (
<Root>
<Content padder>
<View>
{this.state.deviceid ?
(
<Button warning block onPress={()=>this.disconnect()}>
<Text>Disconnect</Text>
</Button>
) : (
<Button block onPress={()=>this.scanAndConnect()}>
<Text>Scan for a device</Text>
</Button>
)
}
</View>
<View style={{alignItems:'center',marginVertical : 10}}>
<Text>{this.state.text1}</Text>
</View>
<Card>
<CardItem>
<Body>
<Text>{this.state.dateTime}{'\n'}{this.state.makedata}</Text>
</Body>
</CardItem>
</Card>
</Content>
{this.state.notificationReceiving==true ? (
<Button warning block onPress={()=>this.stopNotication()}>
<Text>Stop Notification</Text>
</Button>
) : null}
<Footer>
<FooterTab>
<Button onPress={()=>this.writeMesage("ACK","ACK Writted")}>
<Text>ACK</Text>
</Button>
<Button onPress={()=>this.writeMesage("ris 0","ris 0 Writted")}>
<Text>RIS 0</Text>
</Button>
<Button onPress={()=>this.writeMesage("ris 1","ris 1 Writted")}>
<Text>RIS 1</Text>
</Button>
</FooterTab>
</Footer>
</Root>
)
}
}