-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weightscreen.js
111 lines (99 loc) · 2.27 KB
/
Weightscreen.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
'use strict';
import React, { Component } from 'react'
import {
StyleSheet,
Text,
TextInput,
View,
TouchableHighlight,
ActivityIndicator,
Image
} from 'react-native';
const BTFacade = require('./BTFacade');
const Buffer = require('buffer/').Buffer;
var styles = StyleSheet.create({
description: {
marginBottom: 20,
fontSize: 18,
textAlign: 'center',
color: '#656565'
},
weight: {
marginBottom: 20,
fontSize: 60,
textAlign: 'center',
color: '#656565'
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
button: {
height: 36,
flex: 1,
flexDirection: 'row',
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
},
container: {
padding: 30,
marginTop: 65,
alignItems: 'center'
}
});
class Weightscreen extends Component {
constructor(props) {
super(props);
this.state = {
lastWeight: undefined,
};
this.bluetooth = new BTFacade();
}
onButtonPressed() {
this.setState({
lastWeight: this.state.lastWeight + 1,
});
}
onDeviceDiscover(error, scannedDevice) {
if (scannedDevice.name == 'Scale') {
console.log(scannedDevice);
var weightEncoded = scannedDevice.serviceData['0000181d-0000-1000-8000-00805f9b34fb'];
var bytes = new Buffer(weightEncoded, 'base64').toString();
if (bytes.charCodeAt(1) >= 128) {
this.setState({ lastWeight: 0.0 })
return
}
var intValue = bytes.charCodeAt(1)*256+bytes.charCodeAt(0);
var weight = intValue*0.005;
console.log(weight);
this.setState({
lastWeight: weight
})
}
}
componentDidMount() {
this.bluetooth.startDeviceScan(this.onDeviceDiscover.bind(this));
}
componentWillUnmount() {
this.bluetooth.destroy();
}
render() {
return (
<View style={styles.container}>
<Text style={styles.description}>
Waiting for signal...
</Text>
<Text style={styles.weight}>
{this.state.lastWeight == undefined ? "" : this.state.lastWeight.toFixed(2)}
</Text>
</View>
);
}
}
module.exports = Weightscreen;