|
@@ -57,12 +57,16 @@ export default {
|
|
|
},
|
|
},
|
|
|
//初始化蓝牙
|
|
//初始化蓝牙
|
|
|
searchBle() {
|
|
searchBle() {
|
|
|
|
|
+ uni.showToast({
|
|
|
|
|
+ title: '搜索中...',
|
|
|
|
|
+ duration: 2000
|
|
|
|
|
+ });
|
|
|
var that = this;
|
|
var that = this;
|
|
|
that.isAutoBle = false;
|
|
that.isAutoBle = false;
|
|
|
uni.openBluetoothAdapter({
|
|
uni.openBluetoothAdapter({
|
|
|
success(res) {
|
|
success(res) {
|
|
|
console.log(res,"打开 蓝牙模块");
|
|
console.log(res,"打开 蓝牙模块");
|
|
|
- that.onDevice();//监听搜索新设备
|
|
|
|
|
|
|
+ //that.onDevice();//监听搜索新设备
|
|
|
//获取本机蓝牙适配器状态。
|
|
//获取本机蓝牙适配器状态。
|
|
|
uni.getBluetoothAdapterState({
|
|
uni.getBluetoothAdapterState({
|
|
|
success: function(res) {
|
|
success: function(res) {
|
|
@@ -77,6 +81,7 @@ export default {
|
|
|
uni.startBluetoothDevicesDiscovery({
|
|
uni.startBluetoothDevicesDiscovery({
|
|
|
success(res) {
|
|
success(res) {
|
|
|
console.log(res);
|
|
console.log(res);
|
|
|
|
|
+ that.getBluetoothDevices()
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
} else {
|
|
} else {
|
|
@@ -87,36 +92,80 @@ export default {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
- onDevice() {
|
|
|
|
|
- var that = this;
|
|
|
|
|
- //监听寻找到新设备的事件
|
|
|
|
|
- uni.onBluetoothDeviceFound(function(devices) {
|
|
|
|
|
- console.log(devices,'设备列表');
|
|
|
|
|
- var re = JSON.parse(JSON.stringify(devices));
|
|
|
|
|
- let name = re.devices[0].name?re.devices[0].name:'未知设备';
|
|
|
|
|
- if (name != "未知设备" && name != "") {
|
|
|
|
|
- let deviceId = re.devices[0].deviceId;
|
|
|
|
|
- switch(uni.getSystemInfoSync().platform){
|
|
|
|
|
- case 'android':
|
|
|
|
|
- that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
- break;
|
|
|
|
|
- case 'ios':
|
|
|
|
|
- for (let i=0;i<that.devices.length;i++){
|
|
|
|
|
- console.log(that.devices[i].name,'----',name)
|
|
|
|
|
- if (that.devices[i].name==name) {return}
|
|
|
|
|
|
|
+ getBluetoothDevices(){
|
|
|
|
|
+ let that = this;
|
|
|
|
|
+ uni.getBluetoothDevices({
|
|
|
|
|
+ success: re => {
|
|
|
|
|
+ //let devices = res.devices;
|
|
|
|
|
+ console.log(re,'设备列表');
|
|
|
|
|
+ //var re = JSON.parse(JSON.stringify(devices));
|
|
|
|
|
+ if(re.devices && re.devices.length > 0){
|
|
|
|
|
+
|
|
|
|
|
+ re.devices.forEach(function(item){
|
|
|
|
|
+
|
|
|
|
|
+ //code something
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ let name = item.name?item.name:'未知设备';
|
|
|
|
|
+ if (name != "未知设备" && name != "") {
|
|
|
|
|
+ let deviceId = item.deviceId;
|
|
|
|
|
+ switch(uni.getSystemInfoSync().platform){
|
|
|
|
|
+ case 'android':
|
|
|
|
|
+ that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'ios':
|
|
|
|
|
+ for (let i=0;i<that.devices.length;i++){
|
|
|
|
|
+ console.log(that.devices[i].name,'----',name)
|
|
|
|
|
+ if (that.devices[i].name==name) {return}
|
|
|
|
|
+ }
|
|
|
|
|
+ that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
- that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+ }else{
|
|
|
|
|
+ console.log('name',name)
|
|
|
|
|
+ console.log('re.devices[0]',re.devices[0])
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- // that.$nextTick(()=>{
|
|
|
|
|
- // that.$util.unique(that.devices);
|
|
|
|
|
- // })
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
|
|
+ // onDevice() {
|
|
|
|
|
+ // var that = this;
|
|
|
|
|
+ // //监听寻找到新设备的事件
|
|
|
|
|
+ // uni.onBluetoothDeviceFound(function(devices) {
|
|
|
|
|
+ // console.log(devices,'设备列表');
|
|
|
|
|
+ // var re = JSON.parse(JSON.stringify(devices));
|
|
|
|
|
+ // let name = re.devices[0].name?re.devices[0].name:'未知设备';
|
|
|
|
|
+ // if (name != "未知设备" && name != "") {
|
|
|
|
|
+ // let deviceId = re.devices[0].deviceId;
|
|
|
|
|
+ // switch(uni.getSystemInfoSync().platform){
|
|
|
|
|
+ // case 'android':
|
|
|
|
|
+ // that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
+ // break;
|
|
|
|
|
+ // case 'ios':
|
|
|
|
|
+ // for (let i=0;i<that.devices.length;i++){
|
|
|
|
|
+ // console.log(that.devices[i].name,'----',name)
|
|
|
|
|
+ // if (that.devices[i].name==name) {return}
|
|
|
|
|
+ // }
|
|
|
|
|
+ // that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
|
|
+ // break;
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // // that.$nextTick(()=>{
|
|
|
|
|
+ // // that.$util.unique(that.devices);
|
|
|
|
|
+ // // })
|
|
|
|
|
+
|
|
|
|
|
+ // }
|
|
|
|
|
+ // });
|
|
|
|
|
+ // },
|
|
|
//自动链接蓝牙-初始化蓝牙模块
|
|
//自动链接蓝牙-初始化蓝牙模块
|
|
|
autoLinkBle(BleVal){
|
|
autoLinkBle(BleVal){
|
|
|
this.BleVal = BleVal;
|
|
this.BleVal = BleVal;
|