-
Notifications
You must be signed in to change notification settings - Fork 5
/
iot_sdk_config.html
181 lines (152 loc) · 7.01 KB
/
iot_sdk_config.html
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
<!doctype html>
<html>
<head>
<title>nRF IoT SDK Commissioning</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="description" content="nRF IoT SDK">
<link rel="icon" type="image/x-icon" href="../beacons/images/favicon.ico" />
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/paper-button/paper-button.html">
<link rel="import" href="bower_components/paper-card/paper-card.html">
<link rel="import" href="bower_components/paper-input/paper-input.html">
<link rel="import" href="bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="bower_components/iron-icons/iron-icons.html">
<link rel="import" href="bower_components/platinum-bluetooth/platinum-bluetooth-device.html">
<link rel="import" href="bower_components/platinum-bluetooth/platinum-bluetooth-characteristic.html">
<style is="custom-style">
paper-button {
display: block;
margin-bottom: 20px;
margin-top: 20px;
}
paper-button.colorful {
color: #4285f4;
}
paper-button[raised].colorful {
background: #4285f4;
color: #fff;
}
body {
background-color: var(--paper-grey-50);
}
#cards {
margin-left: auto;
margin-right: auto;
max-width: 400px;
}
paper-card {
margin-bottom: 16px;
width: 100%;
}
</style>
</head>
<body unresolved>
<div id="cards">
<paper-card heading="Node Configuration">
<div class="card-content">
<paper-input id="ssid" label="SSID" char-counter maxlength="16"></paper-input>
<paper-input id="keys-store" label="Password" char-counter maxlength="8" auto-validate allowed-pattern="[0-9]">></paper-input>
</div>
<div class="card-actions">
<paper-button raised id="commission" class="colorful"><iron-icon icon="icons:cloud-upload"></iron-icon></paper-button>
<paper-button raised id="identify">IDENTIFY node</paper-button>
</div>
</paper-card>
</div>
<paper-dialog id="no-bluetooth">
<h2>No Web Bluetooth</h2>
<p>The Web Bluetooth API is missing. Please enable it at
chrome://flags/#enable-web-bluetooth and try again.</p>
</paper-dialog>
<platinum-bluetooth-device id="ble-device"
services-filter='["54207799-8f40-4fe5-bebe-6bb7022d3e73"]'>
<platinum-bluetooth-service service="54207799-8f40-4fe5-bebe-6bb7022d3e73">
<platinum-bluetooth-characteristic id="ssid-characteristic"
characteristic="542077a9-8f40-4fe5-bebe-6bb7022d3e73">
</platinum-bluetooth-characteristic>
<platinum-bluetooth-characteristic id ="keys-store-characteristic"
characteristic="542077b9-8f40-4fe5-bebe-6bb7022d3e73">
</platinum-bluetooth-characteristic>
<platinum-bluetooth-characteristic id="control-point-characteristic"
characteristic="542077c9-8f40-4fe5-bebe-6bb7022d3e73">
</platinum-bluetooth-characteristic>
</platinum-bluetooth-service>
</platinum-bluetooth-device>
<script>
"use strict";
document.addEventListener('WebComponentsReady', function() {
console.log("WebCompReady!");
var commissionButton = document.querySelector('#commission');
var identifyButton = document.querySelector('#identify');
var ssid = document.querySelector('#ssid');
var password = document.querySelector('#keys-store');
var controlPoint = document.querySelector('#control-point');
var bleDevice = document.querySelector('#ble-device');
var ssidCharacteristic = document.querySelector('#ssid-characteristic');
var passwordCharacteristic = document.querySelector('#keys-store-characteristic');
var controlCharacteristic = document.querySelector('#control-point-characteristic');
commissionButton.addEventListener('click', connect);
identifyButton.addEventListener('click', logTest);
// check if bluetooth is supported
if (navigator.bluetooth == undefined) {
document.getElementById("no-bluetooth").style.display = "block";
document.getElementById("no-bluetooth").open();
}
function connect(){
console.log("connect()");
bleDevice.request().then(function() {
var encoder = new TextEncoder("utf-8");
console.log("SSID: " + ssid.value + " (string)");
var ssidData = encoder.encode(ssid.value);
console.log('SSID: ' + ssidData + "(utf-8)"); // ascii compatible
// encoded data returns error when attempting a write
// convert to Uint8Array before writing
// var ssidDataArray = strToUint8Array(ssid.value);
// does not work either. let's try manual mode
var ssidDataArray = new Uint8Array([0x01,0xA0,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x02]);
return ssidCharacteristic.write(ssidDataArray).then(function() {
console.log('Characteristic write done!');
});
})
.catch(function(error) {
console.log(error);
});
};
function logTest(){
// It is really easy to send the data as a utf-8 encoded array (ascii compatible)
// How does this compute on the receiving device end?
// TextEncoder FTW?
var encoder = new TextEncoder("utf-8");
console.log("SSID: " + ssid.value + " -" + typeof(ssid.value));
var ssidData = encoder.encode(ssid.value);
console.log('SSID: ' + ssidData + " -" + typeof(ssidData) + " containing " + typeof(ssidData[0]) +"s"); // ascii compatible
console.log("PWD: " + password.value + " -" + typeof(password.value));
var pwdData = encoder.encode(password.value);
console.log('PWD: ' + pwdData + " -" + typeof(pwdData) + " containing " + typeof(pwdData[0]) +"s"); // ascii compatible
var passwordArray = strToUint8Array(password.value);
console.log("PWD: " + passwordArray + " -" + typeof(passwordArray) + " containing " + typeof(passwordArray[0]) +"s");
var passwordArray2 = strToUint8ArrayInt(password.value);
console.log("PWD: " + passwordArray2 + " -" + typeof(passwordArray2) + " containing " + typeof(passwordArray2[0]) +"s");
var controlPointData = new Uint8Array([0x01,0xA0,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x02]);
console.log('Control Point write: ' + controlPointData + " -" + typeof(controlPointData) + " containing " + typeof(controlPointData[0]) +"s");
};
function strToUint8Array(str){
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
};
function strToUint8ArrayInt(str){
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i)-48);
}
return bytes;
};
});
</script>
</body>
</html>