这篇写好的文章丢了TT。。。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
92constructor(
private ble: BLE,
public plt: Platform,
private router: Router,
private cdf: ChangeDetectorRef
) {}
scan() {
this.ble.scan([], 10).subscribe(
device => {
this.device = device;
this.devices.push(device);
this.cdf.markForCheck(); // 进行标注
this.cdf.detectChanges(); // 要多加一行这个 执行一次变化检测
},
err => {
alert(JSON.stringify(err));
}
);
}
onConnect(device) {
this.ble.connect(device.id).subscribe(
result => {
alert(JSON.stringify(result));
this.status = result;
this.getBleInfo();
this.deviceId = device.id;
},
err => {
alert(JSON.stringify(err));
}
);
}
getBleInfo() {
const array = this.status.characteristics;
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (element.properties.indexOf("Write") > -1) {
this.serviceUUID = element.service;
this.characteristicUUID = element.characteristic;
return false;
}
}
}
print(content) {
const intarray: number[] = [];
for (let index = 0; index < content.length; index++) {
const u = content.charCodeAt(index);
const uindex = this.encode.wUnicodes.indexOf(u);
if (uindex === -1) {
intarray.push(u);
} else {
const g = this.encode.wGBKs[uindex];
const gstr = g.toString(2); // 转二进制字符串
const pint = gstr.substring(0, 8); // 截取前八位
intarray.push(parseInt(pint, 2));
if (gstr.length > 8) {
const lint = gstr.substring(8);
intarray.push(parseInt(lint, 2));
}
}
}
const array = new Uint8Array(intarray.length);
for (let index = 0; index < intarray.length; index++) {
const element = intarray[index];
array[index] = element;
}
this.ble
.write(
this.deviceId,
this.serviceUUID,
this.characteristicUUID,
array.buffer
)
.then(e => {
alert("suceess:" + JSON.stringify(e));
})
.catch(e => {
alert("error:" + JSON.stringify(e));
});
}