App-Android(使用App+htnl5框架,解决消息推送兼容SignalR问题)
loulijun2021
2022-10-22 40711118c33369ccad7ceda0fdd729bd9f117f65
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <meta name="HandheldFriendly" content="true"/>
        <meta name="MobileOptimized" content="320"/>
        <title>Hello H5+</title>
        <script type="text/javascript" src="../js/common.js"></script>
        <script type="text/javascript">
var bds = []; // 可连接设备列表
var deviceId = null, bconnect = false;
var bss = [];    // 连接设备服务列表
var serviceId = null;
var bscs = [];    // 连接设备服务对应的特征值列表
var characteristicId = null;
var bscws = [];  // 可写特征值列表
var wcharacteristicId = null;
// 重设数据 
function resetDevices(d,s){
    d||(bds=[],deviceId=null,document.getElementById('deivce').value='');
    s||(bss=[],serviceId=null,document.getElementById('service').value='');
    bscs=[],bscws=[],characteristicId=null,wcharacteristicId=null,document.getElementById('characteristic').value='',document.getElementById('wcharacteristic').value='';
}
 
// 页面初始化操作 
document.addEventListener('plusready', function(e){
    // 监听蓝牙适配器状态变化
    plus.bluetooth.onBluetoothAdapterStateChange(function(e){
        outLine('onBluetoothAdapterStateChange: '+JSON.stringify(e));
    });
    //  监听搜索到新设备 
    plus.bluetooth.onBluetoothDeviceFound(function(e){
        var devices = e.devices;
        outLine('onBluetoothDeviceFound: '+devices.length);
        for(var i in devices){
            outLine(JSON.stringify(devices[i]));
            var device = devices[i];
            if(device.deviceId/*&&device.name&&device.name.length>0&&device.name!='null'*/){
                bds.push(device);
            }
        }
        if(!bconnect && bds.length>0){    // 默认选择最后一个
            var n = bds[bds.length-1].name;
            if(!n || n.length<=0){
                n = bds[bds.length-1].deviceId;
            }
            document.getElementById('deivce').value = n;
            deviceId = bds[bds.length-1].deviceId;
        }
    });
    //  监听低功耗蓝牙设备连接状态变化 
    plus.bluetooth.onBLEConnectionStateChange(function(e){
        outLine('onBLEConnectionStateChange: '+JSON.stringify(e));
        if(deviceId == e.deviceId){    // 更新连接状态
            bconnect = e.connected;
        }
    });
    // 监听低功耗蓝牙设备的特征值变化 
    plus.bluetooth.onBLECharacteristicValueChange(function(e){
        outLine('onBLECharacteristicValueChange: '+JSON.stringify(e));
        var value = buffer2hex(e.value);
        outLine('value(hex) = '+value);
        if(characteristicId == e.characteristicId){
            // 更新到页面显示
            document.getElementById('readvalue').value = value;
        }else if(wcharacteristicId == e.characteristicId){
            plus.nativeUI.toast(value);
        }
    });
}, false);
 
function buffer2hex(value){
    var t='';
    if(value){
        var v=new Uint8Array(value);
        for(var i in v){
            t += '0x'+v[i].toString(16)+' ';
        }
    }else{
        t='无效值';
    }
    return t;
}
 
// 打开蓝牙 
function openBluetooth(){
    outSet('打开蓝牙适配器:');
    plus.bluetooth.openBluetoothAdapter({
        success: function(e){
            outLine('打开成功!');
        },
        fail: function(e){
            outLine('打开失败! '+JSON.stringify(e));
        }
    });
}
 
// 开始搜索蓝牙设备 
function startDiscovery(){
    outSet('开始搜索蓝牙设备:');
    resetDevices();
    plus.bluetooth.startBluetoothDevicesDiscovery({
        success: function(e){
            outLine('开始搜索成功!');
        },
        fail: function(e){
            outLine('开始搜索失败! '+JSON.stringify(e));
        }
    });
}
 
// 停止搜索蓝牙设备 
function stopDiscovery(){
    outSet('停止搜索蓝牙设备:');
    plus.bluetooth.stopBluetoothDevicesDiscovery({
        success: function(e){
            outLine('停止搜索成功!');
        },
        fail: function(e){
            outLine('停止搜索失败! '+JSON.stringify(e));
        }
    });
}
 
// 选择蓝牙设备 
function selectDevice(){
    if(bds.length <= 0){
        plus.nativeUI.toast('未搜索到有效蓝牙设备!');
        return;
    }
    var bts=[];
    for(var i in bds){
        var t = bds[i].name;
        if(!t || t.length<=0){
            t = bds[i].deviceId;
        }        
        bts.push({title:t});
    }
    plus.nativeUI.actionSheet({title:"选择蓝牙设备",cancel:"取消",buttons:bts}, function(e){
        if(e.index>0){
            document.getElementById('deivce').value = bds[e.index-1].name;
            deviceId = bds[e.index-1].deviceId;
            outLine('选择了"'+bds[e.index-1].name+'"');
        }
    });
}
 
// 连接蓝牙设备 
function connectDevice(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    outSet('连接设备: '+deviceId);
    plus.bluetooth.createBLEConnection({
        deviceId: deviceId,
        success: function(e){
            outLine('连接成功!');
        },
        fail: function(e){
            outLine('连接失败! '+JSON.stringify(e));
        }
    });
}
 
// 获取设备服务 
function getServices(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    if(!bconnect){
        plus.nativeUI.toast('未连接蓝牙设备!');
        return;
    }
    resetDevices(true);
    outSet('获取蓝牙设备服务:');
    plus.bluetooth.getBLEDeviceServices({
        deviceId: deviceId,
        success: function(e){
            var services = e.services;
            outLine('获取服务成功! '+services.length);
            if(services.length>0){
                for(var i in services){
                    bss.push(services[i]);
                    outLine(JSON.stringify(services[i]));
                }
                if(bss.length>0){    // 默认选择最后一个服务
                    document.getElementById('service').value = serviceId = bss[bss.length-1].uuid;
                }
            }else{
                outLine('获取服务列表为空?');
            }
        },
        fail: function(e){
            outLine('获取服务失败! '+JSON.stringify(e));
        }
    });
}
 
// 选择服务 
function selectService(){
    if(bss.length <= 0){
        plus.nativeUI.toast('未获取到有效蓝牙服务!');
        return;
    }
    var bts=[];
    for(var i in bss){
        bts.push({title:bss[i].uuid});
    }
    plus.nativeUI.actionSheet({title:"选择服务",cancel:"取消",buttons:bts}, function(e){
        if(e.index>0){
            document.getElementById('service').value = serviceId = bss[e.index-1].uuid;
            outLine('选择了服务: "'+serviceId+'"');
        }
    });
}
 
// 获取服务的特征值 
function getCharacteristics(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    if(!bconnect){
        plus.nativeUI.toast('未连接蓝牙设备!');
        return;
    }
    if(!serviceId){
        plus.nativeUI.toast('未选择服务!');
        return;
    }
    resetDevices(true, true);
    outSet('获取蓝牙设备指定服务的特征值:');
    plus.bluetooth.getBLEDeviceCharacteristics({
        deviceId: deviceId,
        serviceId: serviceId,
        success: function(e){
            var characteristics = e.characteristics;
            outLine('获取特征值成功! '+characteristics.length);
            if(characteristics.length>0){
                for(var i in characteristics){
                    var characteristic = characteristics[i];
                    outLine(JSON.stringify(characteristic));
                    if(characteristic.properties){
                        if(characteristic.properties.read){
                            bscs.push(characteristics[i]);
                        }
                        if(characteristic.properties.write){
                            bscws.push(characteristics[i]);
                            if(characteristic.properties.notify||characteristic.properties.indicate){
                                plus.bluetooth.notifyBLECharacteristicValueChange({    //监听数据变化
                                    deviceId: deviceId,
                                    serviceId: serviceId,
                                    characteristicId: characteristic.uuid,
                                    success: function(e){
                                        outLine('notifyBLECharacteristicValueChange '+characteristic.uuid+' success.');
                                    },
                                    fail: function(e){
                                        outLine('notifyBLECharacteristicValueChange '+characteristic.uuid+' failed! '+JSON.stringify(e));
                                    }
                                });
                            }
                        }
                    }
                }
                if(bscs.length>0){    // 默认选择最后特征值
                    document.getElementById('characteristic').value = characteristicId = bscs[bscs.length-1].uuid;
                }
                if(bscws.length>0){    // 默认选择最后一个可写特征值
                    document.getElementById('wcharacteristic').value = wcharacteristicId = bscws[bscws.length-1].uuid;
                }
            }else{
                outLine('获取特征值列表为空?');
            }
        },
        fail: function(e){
            outLine('获取特征值失败! '+JSON.stringify(e));
        }
    });
}
 
// 选择特征值(读取) 
function selectCharacteristic(){
    if(bscs.length <= 0){
        plus.nativeUI.toast('未获取到有效可读特征值!');
        return;
    }
    var bts=[];
    for(var i in bscs){
        bts.push({title:bscs[i].uuid});
    }
    plus.nativeUI.actionSheet({title:'选择特征值',cancel:'取消',buttons:bts}, function(e){
        if(e.index>0){
            document.getElementById('characteristic').value = characteristicId = bscs[e.index-1].uuid;
            outLine('选择了特征值: "'+characteristicId+'"');
        }
    });
}
 
// 读取特征值数据 
function readValue(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    if(!bconnect){
        plus.nativeUI.toast('未连接蓝牙设备!');
        return;
    }
    if(!serviceId){
        plus.nativeUI.toast('未选择服务!');
        return;
    }
    if(!characteristicId){
        plus.nativeUI.toast('未选择读取的特征值!');
        return;
    }
    outSet('读取蓝牙设备的特征值数据: ');
    plus.bluetooth.readBLECharacteristicValue({
        deviceId: deviceId,
        serviceId: serviceId,
        characteristicId: characteristicId,
        success: function(e){
            outLine('读取数据成功!');
        },
        fail: function(e){
            outLine('读取数据失败! '+JSON.stringify(e));
        }
    });
}
 
// 选择特征值(写入) 
function selectwCharacteristic(){
    if(bscws.length <= 0){
        plus.nativeUI.toast('未获取到有效可写特征值!');
        return;
    }
    var bts=[];
    for(var i in bscws){
        bts.push({title:bscws[i].uuid});
    }
    plus.nativeUI.actionSheet({title:'选择特征值',cancel:'取消',buttons:bts}, function(e){
        if(e.index>0){
            document.getElementById('wcharacteristic').value = wcharacteristicId = bscws[e.index-1].uuid;
            outLine('选择了特征值: "'+wcharacteristicId+'"');
        }
    });
}
 
// 写入特征值数据 
function writeValue(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    if(!bconnect){
        plus.nativeUI.toast('未连接蓝牙设备!');
        return;
    }
    if(!serviceId){
        plus.nativeUI.toast('未选择服务!');
        return;
    }
    if(!wcharacteristicId){
        plus.nativeUI.toast('未选择写入的特征值!');
        return;
    }
    var value = document.getElementById('writevalue').value;
    if(!value || value==''){
        plus.nativeUI.toast('请输入需要写入的数据');
        document.getElementById('writevalue').focus();
        return;
    }
    // 转换为ArrayBuffer写入蓝牙
    str2ArrayBuffer(value, function(buffer){
        outSet('写入蓝牙设备的特征值数据: ');
        plus.bluetooth.writeBLECharacteristicValue({
            deviceId: deviceId,
            serviceId: serviceId,
            characteristicId: wcharacteristicId,
            value: buffer,
            success: function(e){
                outLine('写入数据成功!');
            },
            fail: function(e){
                outLine('写入数据失败! '+JSON.stringify(e));
            }
        });
    });
}
 
function str2ArrayBuffer(s,f) {
    var b = new Blob([s],{type:'text/plain'});
    var r = new FileReader();
    r.readAsArrayBuffer(b);
    r.onload = function(){if(f)f.call(null,r.result)}
}
 
 
// 断开蓝牙设备
function disconnectDevice(){
    if(!deviceId){
        plus.nativeUI.toast('未选择设备!');
        return;
    }
    resetDevices(true);
    outSet('断开蓝牙设备连接:');
    plus.bluetooth.closeBLEConnection({
        deviceId: deviceId,
        success: function(e){
            outLine('断开连接成功!');
        },
        fail: function(e){
            outLine('断开连接失败! '+JSON.stringify(e));
        }
    });
}
 
// 关闭蓝牙
function closeBluetooth(){
    outSet('关闭蓝牙适配器:');
    resetDevices();
    plus.bluetooth.closeBluetoothAdapter({
        success: function(e){
            outLine('关闭成功!');
            bconnect = false;
        },
        fail: function(e){
            outLine('关闭失败! '+JSON.stringify(e));
        }
    });
}
        </script>
        <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
    </head>
    <body>
        <br/>
        <div class="button" onclick="openBluetooth()">初始化蓝牙模块</div>
        <div class="button" onclick="startDiscovery()">开始搜索蓝牙设备</div>
        <div class="button" onclick="stopDiscovery()">停止搜索蓝牙设备</div>
        设备:<input id="deivce" type="text" disabled="disabled"></input>
        <a href="#" onclick="selectDevice()">选择设备</a>
        <div class="button" onclick="connectDevice()">连接蓝牙设备</div>
        <div class="button" onclick="getServices()">获取设备服务</div>
        服务:<input id="service" type="text" disabled="disabled"></input>
        <a href="#" onclick="selectService()">选择服务</a>
        <div class="button" onclick="getCharacteristics()">获取服务的特征值</div>
        读取特征值:<input id="characteristic" type="text" disabled="disabled"></input>
        <a href="#" onclick="selectCharacteristic()">选择</a>
        <div class="button" onclick="readValue()">读取特征值数据</div>
        读取数据:<input id="readvalue" type="text" disabled="disabled" style="width:60%"></input>
        <hr/>
        <br/>
        写入特征值:<input id="wcharacteristic" type="text" disabled="disabled"></input>
        <a href="#" onclick="selectwCharacteristic()">选择</a>
        <div class="button" onclick="writeValue()">写入特征值数据</div>
        写入数据:<input id="writevalue" type="text" style="width:60%;-webkit-user-select:text" value="test"></input>
        <div class="button" onclick="disconnectDevice()">断开蓝牙设备</div>
        <div class="button" onclick="closeBluetooth()">关闭蓝牙模块</div>
        <div id="outpos"/>
        <div id="output">
Bluetooth用于管理蓝牙设备,搜索附近蓝牙设备、连接实现数据通信等。
        </div>
    </body>
</html>