//建立本地作为蓝牙低功耗外围设备的服务端,可创建多个。
wx.createBLEPeripheralServer({
success: (res) => {
console.info('createBLEPeripheralServer successed', res);
// let server = res.server;
server = res.server;
console.log(DeviceName);
let uuid1 = '0000af8e-0000-1000-8000-00805f9b34fb';
let uuid2 = '0000aa8e-0000-2000-8f00-00405f9b34fa';
let buff = [0x1E, 0xFF, 0x0C, 0x00, 0x01, 0x12, 0x21, 0x33, 0xDF, 0x09];
// let buff = [0x1E, 0xFF, 0x0C, 0x00, 0x02, 0x12, 0x21, 0x33, 0xDF, 0x09];
var crc16data = crc16(buff)
console.log(crc16data);
var manufacturerSpecificData = [0x01, 0x12, 0x21, 0x33, 0xDF, 0x09, Number(crc16data[0]), Number(crc16data[1]), 0x00, 0x00, 0xCC, 0x27, 0x45, 0x67, 0xF7, 0xDB, 0x34, 0xC4, 0x03, 0x8E, 0x5C, 0x0B, 0xAA, 0x97, 0x30, 0x56, 0xE6]
// [0x01, 0x11, 0x22,0x33,0XDF,0X09,0xCA, 0X55, 0x00, 0x00, 0xCC, 0x27, 0x45, 0x67, 0xF7, 0xDB, 0x34, 0xC4, 0x03, 0x8E, 0x5C, 0x0B, 0xAA, 0x97, 0x30, 0x56, 0xE6] 测试数据
// let hex = "0x01112233DF09CA550000CC274567F7DB34C4038E5COBAA973056E6"
// let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
// return parseInt(h, 16)
// }))
// console.log(typedArray);
// //开始广播本地创建的外围设备。
console.log(manufacturerSpecificData);
server.stopAdvertising()
var advertiseRequest = {}
if(platform=='android'){
advertiseRequest = {
connectable: false,
manufacturerData: [{
manufacturerId: 0x000C,
manufacturerSpecificData: manufacturerSpecificData //规定长度 <=31位
}]
}
}else{
advertiseRequest = {
connectable: false,
deviceName: DeviceName,
serviceUuids:manufacturerSpecificData,//广播包可以看出来包含的数据 但是E6不在里面
// serviceUuids:['1101','3322','09DF','55CA','0000','27CC','6745','DBF7','C434','8E03','0B5C','97AA','5630','E600'],//广播包可以看出来包含的数据 但是E6不在里面
// serviceUuids:['010C','2211','DF33','CA09','0055','CC00','4527','F767','34DB','03C4','5C8E','AA0B','3097','E656'],//广播包可以看出来包含的数据 但是E6不在里面
// serviceUuids:[uuid1,uuid2],//广播包和发的的uuid看不出来包含uuid里面的内容
}
}
server.startAdvertising({
advertiseRequest,
powerLevel: 'high',
}).then(
(res) => {
wx.showToast({
title: '开始广播成功',
icon: 'success',
duration: 2000
})
console.log('Adverstising.. ', res);
},
(res) => {
console.warn('Advertising failed', res);
});
},
fail: (res) => {
wx.showToast({
title: '开始广播失败',
icon: 'error',
duration: 2000
})
console.warn('createBLEPeripheralServer failed');
},
complete: (res) => {
console.debug('createBLEPeripheralServer completed');
}
});
},
fail: (res) => {
app.getSetting('scope.bluetooth')
},
complete: (res) => {
wx.onBluetoothAdapterStateChange(function (res) { //监听蓝牙打开关闭
console.log(res);
})
},
});