添加蓝牙通讯握手
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
export default {
|
||||
featrueValueCallback: null,//蓝牙特征变化回调
|
||||
BleChangeCallback:null,//蓝牙状态变化回调
|
||||
featrueValueCallback: null, //蓝牙特征变化回调
|
||||
BleChangeCallback: null, //蓝牙状态变化回调
|
||||
//引导用户打开蓝牙
|
||||
showBluetoothGuide: function(showTip) {
|
||||
let platform = process.env.UNI_PLATFORM;
|
||||
@ -89,7 +89,7 @@ export default {
|
||||
uni.getBluetoothAdapterState({
|
||||
success(res1) {
|
||||
|
||||
console.log("当前蓝牙适配器状态:" + JSON.stringify(res1))
|
||||
//console.log("当前蓝牙适配器状态:" + JSON.stringify(res1))
|
||||
if (callback) {
|
||||
callback(res1);
|
||||
}
|
||||
@ -126,8 +126,8 @@ export default {
|
||||
callback();
|
||||
}
|
||||
uni.onBluetoothAdapterStateChange(function(state) {
|
||||
console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
if(this.BleChangeCallback){
|
||||
//console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
if (this.BleChangeCallback) {
|
||||
this.BleChangeCallback()
|
||||
}
|
||||
})
|
||||
@ -232,7 +232,7 @@ export default {
|
||||
StopSearch: function() {
|
||||
uni.stopBluetoothDevicesDiscovery({
|
||||
success: (res) => {
|
||||
console.log("停止搜索蓝牙设备成功")
|
||||
//console.log("停止搜索蓝牙设备成功")
|
||||
},
|
||||
fail() {
|
||||
console.log("无法停止蓝牙搜索")
|
||||
@ -241,8 +241,7 @@ export default {
|
||||
},
|
||||
//获取已连接的设备
|
||||
getLinkBlue: function(callback) {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
services: ["0xFFE0"],
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success: (res) => {
|
||||
if (callback) {
|
||||
callback(res);
|
||||
@ -250,7 +249,7 @@ export default {
|
||||
}
|
||||
},
|
||||
fail: function(ex) {
|
||||
console.log("获取已连接设备异常");
|
||||
console.log("获取已连接设备异常",ex);
|
||||
if (callback) {
|
||||
callback({
|
||||
devices: []
|
||||
@ -261,7 +260,7 @@ export default {
|
||||
},
|
||||
//连接某个设备
|
||||
LinkBlue: function(deviceId, callback, error) {
|
||||
|
||||
//console.log("deviceId="+deviceId)
|
||||
this.StopSearch();
|
||||
var these = this;
|
||||
let key = "linkedDevices";
|
||||
@ -274,54 +273,58 @@ export default {
|
||||
var str = uni.getStorageSync(key);
|
||||
if (str) {
|
||||
linkedDevices = JSON.parse(str);
|
||||
}else{
|
||||
linkedDevices=[];
|
||||
} else {
|
||||
linkedDevices = [];
|
||||
}
|
||||
|
||||
}
|
||||
//连接成功的回调
|
||||
var lindedCallback = function () {
|
||||
var lindedCallback = function(id,flag) {
|
||||
|
||||
let c = linkedDevices.find(function (v) {
|
||||
let c = linkedDevices.find(function(v) {
|
||||
return v.deviceId == deviceId;
|
||||
});
|
||||
|
||||
if (c) {
|
||||
console.log("连接成功开始监听特征变化")
|
||||
if (c && !flag) {
|
||||
console.log("连接成功开始监听特征变化,deviceid="+deviceId+',serviceId='+c.notifyServiceid+',characteristicId='+c.notifyCharactId)
|
||||
//监听设备的特征变化
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
deviceId: deviceId,
|
||||
serviceId: c.notifyServiceid,
|
||||
characteristicId: c.notifyCharactId,
|
||||
state: true,
|
||||
success: function (res) {
|
||||
console.log("开始监听成功。。。。")
|
||||
if(res.errCode=='0'){
|
||||
//订阅特征值
|
||||
uni.onBLECharacteristicValueChange(function(data){
|
||||
// data.characteristicId
|
||||
// data.deviceId
|
||||
// data.serviceId
|
||||
// data.value
|
||||
console.log("监听到特征值:"+JSON.stringify(data));
|
||||
setTimeout(()=>{
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
deviceId: deviceId,
|
||||
serviceId: c.notifyServiceid,
|
||||
characteristicId: c.notifyCharactId,
|
||||
state: true,
|
||||
success: function(res) {
|
||||
console.log("开始监听成功。。。。",res)
|
||||
//订阅特征值
|
||||
|
||||
if(these.featrueValueCallback){
|
||||
these.featrueValueCallback(data);
|
||||
}
|
||||
});
|
||||
uni.onBLECharacteristicValueChange(function(data) {
|
||||
// data.characteristicId
|
||||
// data.deviceId
|
||||
// data.serviceId
|
||||
// data.value
|
||||
console.log("监听到特征值:" + JSON.stringify(data));
|
||||
|
||||
if (these.featrueValueCallback) {
|
||||
these.featrueValueCallback(data);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
},1000);
|
||||
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(deviceId);
|
||||
callback(deviceId,flag);
|
||||
}
|
||||
}
|
||||
|
||||
var linkState = function(res) {
|
||||
console.log("获取已连接的设备回调" + JSON.stringify(res))
|
||||
//console.log("获取已连接的设备回调" + JSON.stringify(res))
|
||||
let flag = res.devices.find(function(v) {
|
||||
if (v.deviceId == deviceId) {
|
||||
return true;
|
||||
@ -329,82 +332,97 @@ export default {
|
||||
return false;
|
||||
});
|
||||
if (flag) {
|
||||
console.log("设备状态已连接");
|
||||
|
||||
lindedCallback(deviceId);
|
||||
|
||||
//console.log("设备状态已连接");
|
||||
|
||||
lindedCallback(deviceId,true);
|
||||
|
||||
return;
|
||||
} else {
|
||||
console.log("设备未连接");
|
||||
console.log("设备未连接:"+deviceId);
|
||||
linkDevice(deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
var linkDevice = function(id) {
|
||||
console.log("正在连接"+id);
|
||||
var linkDevice = function() {
|
||||
console.log("正在连接" + deviceId);
|
||||
uni.createBLEConnection({
|
||||
deviceId: id,
|
||||
deviceId: deviceId,
|
||||
timeout: 30000,
|
||||
success: function(info) {
|
||||
|
||||
console.log("连接成功");
|
||||
|
||||
uni.setBLEMTU({
|
||||
deviceId: id,
|
||||
mtu: 512,
|
||||
success: () => {
|
||||
console.log("mtu设置成功");
|
||||
if(linkedDevices){
|
||||
console.log("11111"+JSON.stringify(linkedDevices));
|
||||
f = linkedDevices.find(function (v) {
|
||||
return v.deviceId == id;
|
||||
});
|
||||
}else{
|
||||
console.log("22222")
|
||||
f=null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!f) {
|
||||
console.log("缓存中没有找到该设备")
|
||||
|
||||
these.getLinkBlue(function (res) {
|
||||
if (res.devices && res.devices.length) {
|
||||
let f = res.devices.find(function (v) {
|
||||
return v.deviceId == id;
|
||||
});
|
||||
linkedDevices.push(f);
|
||||
uni.setStorageSync(key, JSON.stringify(linkedDevices));
|
||||
|
||||
getService(id);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
console.log("缓存中已连接过");
|
||||
if (!f.services) {
|
||||
getService(id);
|
||||
} else {
|
||||
|
||||
lindedCallback(id);
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: function() {
|
||||
console.log("mtu设置失败")
|
||||
var call = () => {
|
||||
if (linkedDevices) {
|
||||
console.log("11111" + JSON.stringify(linkedDevices));
|
||||
f = linkedDevices.find(function(v) {
|
||||
return v.deviceId == deviceId;
|
||||
});
|
||||
} else {
|
||||
console.log("22222")
|
||||
f = null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (!f) {
|
||||
console.log("缓存中没有找到该设备")
|
||||
|
||||
these.getLinkBlue(function(res) {
|
||||
if (res.devices && res.devices.length) {
|
||||
let f = res.devices.find(function(v) {
|
||||
return v.deviceId == deviceId;
|
||||
});
|
||||
linkedDevices.push(f);
|
||||
uni.setStorageSync(key, JSON.stringify(
|
||||
linkedDevices));
|
||||
|
||||
getService(deviceId);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
console.log("缓存中已连接过");
|
||||
if (!f.services) {
|
||||
getService(deviceId);
|
||||
} else {
|
||||
|
||||
lindedCallback(deviceId,false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
let os = uni.getSystemInfoSync().osName;
|
||||
if (os == 'android') {
|
||||
uni.setBLEMTU({
|
||||
deviceId: deviceId,
|
||||
mtu: 512,
|
||||
success: () => {
|
||||
//console.log("mtu设置成功");
|
||||
},
|
||||
fail: function() {
|
||||
console.log("mtu设置失败")
|
||||
},
|
||||
complete: function() {
|
||||
call();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
call();
|
||||
}
|
||||
|
||||
},
|
||||
fail: function(ex) {
|
||||
if (error) {
|
||||
console.log("蓝牙连接失败" + JSON.stringify(error));
|
||||
error(ex);
|
||||
if (ex) {
|
||||
console.log("蓝牙连接失败" + JSON.stringify(ex));
|
||||
if(error){
|
||||
error(ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -435,9 +453,9 @@ export default {
|
||||
Promise.all(promises)
|
||||
.then(results => {
|
||||
console.log('所有操作成功完成', results);
|
||||
|
||||
lindedCallback(id);
|
||||
|
||||
|
||||
lindedCallback(id,false);
|
||||
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('至少一个操作失败', error);
|
||||
@ -447,9 +465,9 @@ export default {
|
||||
} else {
|
||||
repeatCnt++;
|
||||
if (repeatCnt > 5) {
|
||||
|
||||
lindedCallback(id);
|
||||
|
||||
|
||||
lindedCallback(id,false);
|
||||
|
||||
return;
|
||||
}
|
||||
setTimeout(function() {
|
||||
@ -498,10 +516,14 @@ export default {
|
||||
if (notiChar) {
|
||||
v.notifyServiceid = serviceId;
|
||||
v.notifyCharactId = notiChar.uuid;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
uni.setStorageSync(key, JSON.stringify(linkedDevices));
|
||||
resolve(res);
|
||||
},
|
||||
@ -522,9 +544,9 @@ export default {
|
||||
}
|
||||
});
|
||||
|
||||
console.log("正在获取蓝牙适配器状态")
|
||||
//console.log("正在获取蓝牙适配器状态")
|
||||
this.CheckBlue((res) => {
|
||||
console.log("蓝牙状态:" + JSON.stringify(res));
|
||||
//console.log("蓝牙状态:" + JSON.stringify(res));
|
||||
if (res.available) {
|
||||
this.getLinkBlue(linkState);
|
||||
} else {
|
||||
@ -580,13 +602,13 @@ export default {
|
||||
reject(`deviceid为空,请输入要发送的设备`);
|
||||
return;
|
||||
}
|
||||
console.log("准备发送数据包");
|
||||
console.log("准备发送数据包");
|
||||
let key = "linkedDevices";
|
||||
var store = uni.getStorageInfoSync();
|
||||
var f = store.keys.find(function(v) {
|
||||
return v == key;
|
||||
});
|
||||
console.log("倒计时:5");
|
||||
console.log("倒计时:5");
|
||||
var linkedDevices = [];
|
||||
if (f) {
|
||||
var str = uni.getStorageSync(key);
|
||||
@ -595,35 +617,19 @@ export default {
|
||||
}
|
||||
|
||||
}
|
||||
console.log("倒计时:4");
|
||||
console.log("倒计时:4");
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
console.log("倒计时:3");
|
||||
console.log("倒计时:3");
|
||||
f = linkedDevices.find(function(v) {
|
||||
return v.deviceId == deviceid;
|
||||
|
||||
});
|
||||
console.log("f=" + JSON.stringify(f));
|
||||
// console.log("deviceid=" + deviceid);
|
||||
console.log("倒计时:2");
|
||||
console.log("倒计时:2");
|
||||
if (f) {
|
||||
console.log("倒计时:1");
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: f.deviceId,
|
||||
serviceId: f.writeServiceId,
|
||||
characteristicId: f.wirteCharactId,
|
||||
value: buffer,
|
||||
success: () => {
|
||||
console.log("发送数据成功");
|
||||
resolve();
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log("发送数据失败" + JSON.stringify(err));
|
||||
reject(`发送数据失败: ${err.errMsg}`);
|
||||
},
|
||||
complete: function() {
|
||||
console.log("发送数据complete");
|
||||
}
|
||||
});
|
||||
console.log("倒计时:1");
|
||||
these.sendDataNew(f.deviceId,f.writeServiceId,f.wirteCharactId,buffer).then(succ).catch(err);
|
||||
} else {
|
||||
reject(`已连接设备中无法找到此设备`);
|
||||
// console.log("警报:已连接设备中无法找到此设备")
|
||||
@ -636,36 +642,67 @@ export default {
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
sendDataNew: function(deviceid, serviceId, characteristicId, buffer) {
|
||||
|
||||
console.log("准备向设备发送数据,deviceid=" + deviceid);
|
||||
//console.log("准备向设备发送数据,deviceid=" + deviceid+',serviceId='+serviceId+',characteristicId='+characteristicId);
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: deviceid,
|
||||
serviceId: serviceId,
|
||||
characteristicId: characteristicId,
|
||||
value: buffer,
|
||||
success: () => {
|
||||
console.log("发送数据成功");
|
||||
resolve();
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log("发送数据失败" + JSON.stringify(err));
|
||||
reject(`发送数据失败: ${err.errMsg}`);
|
||||
},
|
||||
complete: function() {
|
||||
console.log("发送数据complete");
|
||||
}
|
||||
let promise = new Promise((succ, err) => {
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: deviceid,
|
||||
serviceId: serviceId,
|
||||
characteristicId: characteristicId,
|
||||
value: buffer,
|
||||
success: () => {
|
||||
//console.log("发送数据成功");
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
console.log("发送数据失败" + JSON.stringify(ex));
|
||||
err(ex);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
if (uni.getSystemInfoSync().osName.toLowerCase() == 'ios') {
|
||||
|
||||
//专业给IOS填坑,uni.writeBLECharacteristicValue在IOS上不进入任何回调
|
||||
|
||||
function timeout(ms) {
|
||||
return new Promise((_, err) => {
|
||||
setTimeout(() => {
|
||||
err({
|
||||
code: -1,
|
||||
errMsg: '超时了'
|
||||
})
|
||||
}, ms);
|
||||
});
|
||||
}
|
||||
|
||||
Promise.race([promise, timeout(50)]).then(resolve).catch((ex) => {
|
||||
console.log("ex=", ex);
|
||||
if (ex.code == -1) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(ex);
|
||||
}
|
||||
|
||||
}).finally(() => {
|
||||
console.log("完成了")
|
||||
});
|
||||
} else {
|
||||
promise.then(resolve).catch(reject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user