Commit 0f257f5c by liangjianmin

连接蓝牙设备

parent 10d758f8
...@@ -53,6 +53,9 @@ ...@@ -53,6 +53,9 @@
.t2{ .t2{
font-size: 26rpx; font-size: 26rpx;
color: #197ADB; color: #197ADB;
&.active{
color: #6E767A;
}
} }
} }
} }
......
...@@ -23,9 +23,14 @@ ...@@ -23,9 +23,14 @@
</view> </view>
</view> </view>
<view class="list"> <view class="list">
<view class="box row verCenter bothSide" v-for="(item, index) in list" :key="index" @click="connectChange(item.name, item.deviceId, index)"> <view class="box row verCenter bothSide" v-for="(item, index) in list" :key="index" @click="connectChange(item.name, item.deviceId, index)" :key="index">
<text class="t1">{{ item.name }}</text> <text class="t1">{{ item.name }}</text>
<text class="t2">连接</text> <template v-if="textArr[index] == '已连接'">
<text class="t2 active">{{ textArr[index] }}</text>
</template>
<template v-else>
<text class="t2">{{ textArr[index] }}</text>
</template>
</view> </view>
</view> </view>
</view> </view>
...@@ -41,7 +46,9 @@ export default { ...@@ -41,7 +46,9 @@ export default {
deviceName: '', deviceName: '',
list: [], list: [],
isOpenBle: false, isOpenBle: false,
deviceId: '' deviceId: '',
textArr: [],
current: -1
}; };
}, },
onLoad() { onLoad() {
...@@ -65,6 +72,9 @@ export default { ...@@ -65,6 +72,9 @@ export default {
}); });
}, },
methods: { methods: {
/**
* 开始搜索蓝牙
*/
startBluetoothDeviceDiscovery() { startBluetoothDeviceDiscovery() {
uni.showLoading({ uni.showLoading({
title: '搜索蓝牙设备', title: '搜索蓝牙设备',
...@@ -83,7 +93,7 @@ export default { ...@@ -83,7 +93,7 @@ export default {
uni.hideLoading(); uni.hideLoading();
console.log('查找设备失败!'); console.log('查找设备失败!');
uni.showToast({ uni.showToast({
icon: 'none', icon: 'error',
title: '查找设备失败!', title: '查找设备失败!',
duration: 3000 duration: 3000
}); });
...@@ -100,8 +110,6 @@ export default { ...@@ -100,8 +110,6 @@ export default {
* 发现外围设备 * 发现外围设备
*/ */
onBluetoothDeviceFound() { onBluetoothDeviceFound() {
console.log('监听寻找新设备');
// this.getBluetoothDevices();
uni.onBluetoothDeviceFound(el => { uni.onBluetoothDeviceFound(el => {
console.log('开始监听寻找到新设备的事件'); console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices(); this.getBluetoothDevices();
...@@ -111,21 +119,22 @@ export default { ...@@ -111,21 +119,22 @@ export default {
* 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。 * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/ */
getBluetoothDevices() { getBluetoothDevices() {
console.log('获取蓝牙设备');
uni.getBluetoothDevices({ uni.getBluetoothDevices({
success: res => { success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg); console.log('获取蓝牙设备成功:' + res.errMsg);
console.log(res);
var devices = []; var devices = [];
var num = 0; var num = 0;
var textArr = [];
for (var i = 0; i < res.devices.length; i++) { for (var i = 0; i < res.devices.length; i++) {
if (res.devices[i].name != '未知设备') { if (res.devices[i].name != '未知设备') {
textArr.push('连接');
devices[num] = res.devices[i]; devices[num] = res.devices[i];
num++; num++;
} }
} }
this.list = devices; this.list = devices;
if (this.list.length > 0) { this.textArr = textArr;
if (devices.length > 0) {
uni.hideLoading(); uni.hideLoading();
} }
} }
...@@ -144,32 +153,64 @@ export default { ...@@ -144,32 +153,64 @@ export default {
} }
}); });
}, },
/**
* 获取蓝牙服务
*/
getBLEDeviceServices() { getBLEDeviceServices() {
//来获取蓝牙设备所有服务 //来获取蓝牙设备所有服务
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('连接成功', JSON.stringify(res.errMsg)); console.log('连接成功', JSON.stringify(res.errMsg));
uni.showToast({
title: '蓝牙连接成功',
icon: 'success'
});
this.status = true; this.status = true;
uni.hideLoading(); this.$set(this.textArr, this.current, '已连接'); //改变数组蓝牙连接状态
uni.setStorageSync('device', { name: this.deviceName, deviceId: this.deviceId });
},
fail: error => {
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
} }
}); });
}, },
connectChange(name, deviceId) { /**
* 连接蓝牙
*/
connectChange(name, deviceId, index) {
uni.showLoading({ uni.showLoading({
title: '连接设备中' title: '连接设备中'
}); });
//先断开现有蓝牙连接
uni.closeBLEConnection({ uni.closeBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg)); console.log('断开蓝牙连接:', JSON.stringify(res.errMsg));
this.deviceName = name; this.deviceName = name; //设备名称
this.deviceId = deviceId; this.deviceId = deviceId; //设备id
uni.setStorageSync('device', { name: name, deviceId: deviceId }); this.current = index;
this.stopBluetoothDevicesDiscovery();
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
//恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
}
this.textArr = tempArr;
}
} }
}); });
}, },
/**
* 刷新蓝牙列表
*/
refresh() { refresh() {
uni.stopBluetoothDevicesDiscovery({ uni.stopBluetoothDevicesDiscovery({
success: res => { success: res => {
...@@ -179,14 +220,30 @@ export default { ...@@ -179,14 +220,30 @@ export default {
} }
}); });
}, },
/**
* 断开连接
*/
closeBLEConnection() { closeBLEConnection() {
uni.closeBLEConnection({ uni.closeBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg)); console.log('断开蓝牙连接成功:', JSON.stringify(res.errMsg));
this.status = false; this.status = false;
this.deviceId = ''; this.deviceId = '';
this.deviceName = ''; this.deviceName = '';
//恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
}
this.textArr = tempArr;
}
uni.removeStorageSync('device'); //移除存储的设备信息
uni.showToast({
title: '已断开蓝牙',
icon: 'success'
});
} }
}); });
} }
......
...@@ -59,6 +59,7 @@ export default { ...@@ -59,6 +59,7 @@ export default {
print_number: 0, print_number: 0,
serviceId: '', serviceId: '',
deviceId: '', deviceId: '',
device_name: '',
characteristics: '', characteristics: '',
text: '总箱数', text: '总箱数',
index: -1, index: -1,
...@@ -70,7 +71,6 @@ export default { ...@@ -70,7 +71,6 @@ export default {
sendData64: '', sendData64: '',
printLabelData: [], printLabelData: [],
print_text: '总箱数', print_text: '总箱数',
device_name: '',
form: { form: {
erp_order_sn_pre: '', erp_order_sn_pre: '',
print_type: 1, print_type: 1,
...@@ -81,10 +81,13 @@ export default { ...@@ -81,10 +81,13 @@ export default {
}; };
}, },
onShow() { onShow() {
const deviceData = uni.getStorageSync('device') || ''; const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息
if (deviceData) { if (deviceData) {
this.deviceId = deviceData.deviceId; this.deviceId = deviceData.deviceId;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else {
this.deviceId = '';
this.device_name = '';
} }
}, },
created() { created() {
...@@ -112,6 +115,13 @@ export default { ...@@ -112,6 +115,13 @@ export default {
uni.setStorageSync('tray_sn_value', this.traySelectOption[e.target.value].wstylt_id); uni.setStorageSync('tray_sn_value', this.traySelectOption[e.target.value].wstylt_id);
}, },
submit() { submit() {
if (!this.device_name) {
uni.showToast({
icon: 'error',
title: '请连接蓝牙设备'
});
return false;
}
if (!this.form.erp_order_sn_pre) { if (!this.form.erp_order_sn_pre) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
...@@ -160,14 +170,7 @@ export default { ...@@ -160,14 +170,7 @@ export default {
this.request(API.printLabel, 'POST', this.form, false).then(res => { this.request(API.printLabel, 'POST', this.form, false).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
if (!this.device_name) { this.getBLEDeviceServices(); //根据获取的设备信息,连接设备
uni.showToast({
icon: 'error',
title: '请连接蓝牙设备'
});
} else {
this.getBLEDeviceServices();
}
} else { } else {
uni.showToast({ uni.showToast({
title: res.err_msg, title: res.err_msg,
...@@ -176,60 +179,13 @@ export default { ...@@ -176,60 +179,13 @@ export default {
} }
}); });
}, },
openBluetoothAdapter() { /**
//先查看系统蓝牙状态 * 来获取蓝牙设备所有服务
let that = this; */
uni.openBluetoothAdapter({
success(res) {
that.startBluetoothDevicesDiscovery();
},
fail() {
uni.showToast({
title: '请打开蓝牙',
icon: 'error'
});
}
});
},
startBluetoothDevicesDiscovery() {
//搜索附近的蓝牙设备并且监听寻找到新设备的事件
let that = this;
uni.startBluetoothDevicesDiscovery({
success: function(res) {
console.log('搜索设备start', JSON.stringify(res.errMsg));
uni.onBluetoothDeviceFound(function(el) {
//找到对应蓝牙设备名字
if (el.devices[0].name == that.device_name) {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log('成功搜索设备', el.devices[0].deviceId);
}
});
},
fail: function() {
uni.hideLoading();
uni.showToast({
title: '请断开蓝牙,重新连接',
icon: 'error'
});
}
});
},
stopBluetoothDevicesDiscovery() {
//成功找到对应蓝牙设备后 停止搜寻附近的蓝牙外围设备
let that = this;
uni.stopBluetoothDevicesDiscovery({
success(res) {
console.log('停止搜索', JSON.stringify(res.errMsg));
that.getBLEDeviceServices();
}
});
},
getBLEDeviceServices() { getBLEDeviceServices() {
//来获取蓝牙设备所有服务
let that = this; let that = this;
uni.showLoading({ uni.showLoading({
title: '连接设备中...' title: '设备打印中'
}); });
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
...@@ -246,11 +202,21 @@ export default { ...@@ -246,11 +202,21 @@ export default {
} }
}); });
}, 1000); }, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
} }
}); });
}, },
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() { getBLEDeviceCharacteristics() {
//获取蓝牙设备某个服务中所有特征值
let that = this; let that = this;
uni.getBLEDeviceCharacteristics({ uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId, deviceId: that.deviceId,
...@@ -261,13 +227,20 @@ export default { ...@@ -261,13 +227,20 @@ export default {
console.log('服务功能特征值', JSON.stringify(res.errMsg)); console.log('服务功能特征值', JSON.stringify(res.errMsg));
}, },
fail: res => { fail: res => {
console.log('失败', res); uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
} }
}); });
}, },
/**
* 向蓝牙发送数据
*/
sendDataChange() { sendDataChange() {
console.log('第' + this.print_number + '次打印'); console.log('第' + this.print_number + '次打印');
//发送数据 二进制
let data = []; let data = [];
if (this.printLabelData.length > 0) { if (this.printLabelData.length > 0) {
data.push('! 60 200 200 250 1\r\n'); data.push('! 60 200 200 250 1\r\n');
...@@ -291,8 +264,10 @@ export default { ...@@ -291,8 +264,10 @@ export default {
this.writeBLECharacteristicValue(); this.writeBLECharacteristicValue();
console.log(data.join('')); console.log(data.join(''));
}, },
/**
* 写入二进制数据
*/
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
//写入二进制数据
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: that.deviceId,
...@@ -317,7 +292,7 @@ export default { ...@@ -317,7 +292,7 @@ export default {
that.sendData64 = ''; that.sendData64 = '';
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '提交成功,请查看标签打印机', content: '打印成功,请查看标签打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment