forked from alexa-samples/ama-sample-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamaRx.js
100 lines (79 loc) · 2.39 KB
/
amaRx.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
/*
Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
SPDX-License-Identifier: LicenseRef-.amazon.com.-AmznSL-1.0
Licensed under the Amazon Software License (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
'use strict';
var util = require('util');
var bleno = require('bleno');
var generalUtils = require('./generalUtils.js');
function AmaRxCharacteristic() {
bleno.Characteristic.call(this, {
uuid: '2BEEA05B-1879-4BB4-8A2F-72641F82420B',
properties: [ 'read', 'notify'],
value: null
});
this._updateValueCallback = null;
}
util.inherits(AmaRxCharacteristic, bleno.Characteristic);
AmaRxCharacteristic.prototype.onReadRequest = function(offset, updateValueCallback) {
// console.log('Phone is trying to read. Why???');
updateValueCallback(this.RESULT_SUCCESS, new Buffer(0));
}
AmaRxCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
console.log('Connection established...');
this._updateValueCallback = updateValueCallback;
var a = buildFirstPacket();
var buf = Buffer.from(a);
this.sendData(buf);
/*
console.log('Buffer: ', buf);
updateValueCallback(buf);
*/
};
AmaRxCharacteristic.prototype.onUnsubscribe = function() {
console.log('AmaRxCharacteristic - onUnsubscribe');
this._updateValueCallback = null;
};
AmaRxCharacteristic.prototype.sendData = function(data) {
if (data.length < 80) {
var s = generalUtils.arrayToHexString(data);
console.log("S: ", s);
}else {
// console.log("S: %d bytes", data.length)
}
this._updateValueCallback(data);
}
function buildFirstPacket() {
var a = new Uint8Array([
0xFE,
0x03,
0x01, // Major version
0x00,
0x03, // transport packet size 800 = 0x0320
0x20,
0x13, // max data size 5000 = 0x1388
0x88,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
]);
return a;
}
module.exports = AmaRxCharacteristic;