Commit f2b558bd by liangjianmin

bug

parent 6513638c
...@@ -137,7 +137,7 @@ ...@@ -137,7 +137,7 @@
<textarea @input="inputChange()" maxlength="200" placeholder="请输入理货备注" placeholder-style="color:#6E767A;" v-model="form.sort_goods_remark"></textarea> <textarea @input="inputChange()" maxlength="200" placeholder="请输入理货备注" placeholder-style="color:#6E767A;" v-model="form.sort_goods_remark"></textarea>
<text class="text">{{ limitword }}/200</text> <text class="text">{{ limitword }}/200</text>
</view> </view>
<view class="box row verCenter" style="border-bottom: none;"> <!-- <view class="box row verCenter" style="border-bottom: none;">
<text class="label">放置托盘</text> <text class="label">放置托盘</text>
<view class="uni-list-cell-db"> <view class="uni-list-cell-db">
<text class="iconfont icon-sanjiaoxing1"></text> <text class="iconfont icon-sanjiaoxing1"></text>
...@@ -145,7 +145,7 @@ ...@@ -145,7 +145,7 @@
<view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view> <view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view>
</picker> </picker>
</view> </view>
</view> </view> -->
</view> </view>
</view> </view>
<view class="btn row rowCenter verCenter" @click="submit()">确认收货</view> <view class="btn row rowCenter verCenter" @click="submit()">确认收货</view>
...@@ -203,12 +203,26 @@ export default { ...@@ -203,12 +203,26 @@ export default {
}, },
onLoad(options) { onLoad(options) {
this.form.number = options.number || ''; //获取参数入仓号 this.form.number = options.number || ''; //获取参数入仓号
this.getTraySelectOption(); //获取托盘数据 //停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try {
uni.removeStorageSync('device');
} catch (e) {
// error
}
}, },
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.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -370,15 +384,6 @@ export default { ...@@ -370,15 +384,6 @@ export default {
return false; return false;
} }
if (!this.form.wstylt_id) {
uni.showModal({
title: '提示',
content: '请选择放置托盘',
showCancel: false
});
return false;
}
//截取入仓号 //截取入仓号
if (this.form.number) { if (this.form.number) {
this.printForm.erp_order_sn_pre = this.form.number.substr(0, 1); this.printForm.erp_order_sn_pre = this.form.number.substr(0, 1);
...@@ -430,16 +435,28 @@ export default { ...@@ -430,16 +435,28 @@ export default {
*/ */
print() { print() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
this.request(API.printLabel, 'POST', this.printForm, true).then(res => { this.request(API.printLabel, 'POST', this.printForm, true).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showToast({
title: res.err_msg, title: res.err_msg,
...@@ -449,66 +466,10 @@ export default { ...@@ -449,66 +466,10 @@ export default {
}); });
}, },
/** /**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res.errMsg));
//需延时连接,不然会报错
setTimeout(() => {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', JSON.stringify(res.errMsg));
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
try {
console.log('第' + this.print_number + '次打印'); console.log('第' + this.print_number + '次打印');
let data = []; let data = [];
if (this.printLabelData.length > 0) { if (this.printLabelData.length > 0) {
...@@ -532,6 +493,15 @@ export default { ...@@ -532,6 +493,15 @@ export default {
this.sendData64 = arrayBuffer; this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue(); this.writeBLECharacteristicValue();
console.log(data.join('')); console.log(data.join(''));
} catch (e) {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
}
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -539,22 +509,17 @@ export default { ...@@ -539,22 +509,17 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
that.sendDataChange();
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({
deviceId: that.deviceId,
success(res) {
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg));
//清空数据 //清空数据
that.print_number = 0; that.print_number = 0;
that.printLabelData = []; that.printLabelData = [];
...@@ -573,14 +538,12 @@ export default { ...@@ -573,14 +538,12 @@ export default {
} }
}); });
} }
});
}
}, },
fail() { fail() {
uni.hideLoading(); uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '无法打印,请查看标签打印机连接状态', content: '打印失败,请检查打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
...@@ -45,8 +45,9 @@ export default { ...@@ -45,8 +45,9 @@ export default {
status: false, status: false,
deviceName: '', deviceName: '',
list: [], list: [],
isOpenBle: false,
deviceId: '', deviceId: '',
serviceId: '',
characteristics: '',
textArr: [], textArr: [],
current: -1, current: -1,
refreshTransition: false refreshTransition: false
...@@ -65,18 +66,15 @@ export default { ...@@ -65,18 +66,15 @@ export default {
//在页面加载时候初始化蓝牙适配器 //在页面加载时候初始化蓝牙适配器
uni.openBluetoothAdapter({ uni.openBluetoothAdapter({
success: e => { success: res => {
console.log('初始化蓝牙成功:' + e.errMsg); console.log('初始化蓝牙成功:' + res.errMsg);
this.isOpenBle = true;
console.log(this.isOpenBle);
// 初始化完毕开始搜索 // 初始化完毕开始搜索
this.startBluetoothDeviceDiscovery(); this.startBluetoothDeviceDiscovery();
}, },
fail: e => { fail: res => {
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
wx.showModal({ wx.showModal({
title: '提示', title: '提示',
content: '蓝牙初始化失败,请到设置打开蓝牙', content: '蓝牙初始化失败,请打开系统蓝牙',
showCancel: false showCancel: false
}); });
} }
...@@ -84,48 +82,40 @@ export default { ...@@ -84,48 +82,40 @@ export default {
}, },
methods: { methods: {
/** /**
* 开始搜索蓝牙 * 搜索附近的蓝牙设备 并且监听寻找到新设备的事件
*/ */
startBluetoothDeviceDiscovery() { startBluetoothDeviceDiscovery() {
console.log('搜寻智能设备');
uni.showLoading({ uni.showLoading({
title: '搜索蓝牙设备', title: '搜索蓝牙设备',
mask: true mask: true
}); });
//在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备 setTimeout(() => {
let self = this;
setTimeout(function() {
if (self.isOpenBle) {
console.log('开始搜寻智能设备');
uni.startBluetoothDevicesDiscovery({ uni.startBluetoothDevicesDiscovery({
success: res => { success: res => {
self.onBluetoothDeviceFound(); uni.onBluetoothDeviceFound(el => {
this.getBluetoothDevices();
});
}, },
fail: res => { fail: res => {
uni.hideLoading(); uni.hideLoading();
console.log('查找设备失败!');
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '查找设备失败', content: '查找设备失败',
showCancel: false, showCancel: false,
confirmText: '关闭' confirmText: '关闭',
}); success: r => {
if (r.confirm) {
console.log('用户点击确定');
this.startBluetoothDeviceDiscovery();
} else if (r.cancel) {
console.log('用户点击取消');
}
} }
}); });
} else {
console.log('未初始化蓝牙是配饰器:' + self.isOpenBle);
} }
}, 300);
},
/**
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(el => {
console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices();
}); });
}, 300);
}, },
/** /**
* 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。 * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
...@@ -134,10 +124,11 @@ export default { ...@@ -134,10 +124,11 @@ export default {
uni.getBluetoothDevices({ uni.getBluetoothDevices({
success: res => { success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg); console.log('获取蓝牙设备成功:' + res.errMsg);
var devices = []; var devices = []; //临时存储设备列表
var textArr = []; //连接状态显示
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('连接'); textArr.push('连接');
devices[num] = res.devices[i]; devices[num] = res.devices[i];
...@@ -146,6 +137,7 @@ export default { ...@@ -146,6 +137,7 @@ export default {
} }
this.list = devices; this.list = devices;
this.textArr = textArr; this.textArr = textArr;
if (devices.length > 0) { if (devices.length > 0) {
uni.hideLoading(); uni.hideLoading();
} }
...@@ -156,36 +148,38 @@ export default { ...@@ -156,36 +148,38 @@ export default {
* 停止搜索蓝牙设备 * 停止搜索蓝牙设备
*/ */
stopBluetoothDevicesDiscovery() { stopBluetoothDevicesDiscovery() {
//成功找到对应蓝牙设备后 停止搜寻附近的蓝牙外围设备
let that = this;
uni.stopBluetoothDevicesDiscovery({ uni.stopBluetoothDevicesDiscovery({
success(res) { success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg)); console.log('停止搜索', JSON.stringify(res.errMsg));
that.getBLEDeviceServices(); this.getBLEDeviceServices();
} }
}); });
}, },
/** /**
* 获取蓝牙服务 * 来获取蓝牙设备所有服务
*/ */
getBLEDeviceServices() { getBLEDeviceServices() {
//来获取蓝牙设备所有服务 //来获取蓝牙设备所有服务
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('连接成功', JSON.stringify(res.errMsg)); //需延时连接,不然会报错
uni.showToast({ setTimeout(() => {
title: '蓝牙连接成功', uni.getBLEDeviceServices({
icon: 'success' deviceId: this.deviceId,
success: res => {
console.log('获取服务', JSON.stringify(res.errMsg));
this.serviceId = res.services[0].uuid;
this.getBLEDeviceCharacteristics();
}
}); });
this.status = true; }, 1000);
this.$set(this.textArr, this.current, '已连接'); //改变数组蓝牙连接状态
uni.setStorageSync('device', { name: this.deviceName, deviceId: this.deviceId });
}, },
fail: error => { fail: error => {
uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '连接设备失败', content: '连接设备失败,请重试',
showCancel: false, showCancel: false,
confirmText: '关闭' confirmText: '关闭'
}); });
...@@ -193,23 +187,76 @@ export default { ...@@ -193,23 +187,76 @@ export default {
}); });
}, },
/** /**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
uni.getBLEDeviceCharacteristics({
deviceId: this.deviceId,
serviceId: this.serviceId,
success: res => {
this.characteristics = res.characteristics[0].uuid; //存储蓝牙所有特征值
this.status = true; //连接状态
this.$set(this.textArr, this.current, '已连接'); //改变数组蓝牙连接状态
//存储蓝牙数据
uni.setStorageSync('device', {
name: this.deviceName,
deviceId: this.deviceId,
serviceId: this.serviceId,
characteristics: this.characteristics
});
uni.showToast({
title: '蓝牙连接成功',
icon: 'success'
});
setTimeout(() => {
uni.navigateBack({
delta: 1
});
}, 2000);
console.log('蓝牙特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
content: '连接设备失败,请重试',
duration: 3000
});
}
});
},
/**
* 连接蓝牙 * 连接蓝牙
*/ */
connectChange(name, deviceId, index) { connectChange(name, deviceId, index) {
//判断设备当前是否有连接
if (this.textArr[index] == '连接') {
uni.showLoading({ uni.showLoading({
title: '连接设备中' title: '连接设备中'
}); });
if (!this.deviceId) {
this.deviceName = name; //设备名称
this.deviceId = deviceId; //设备id
this.current = index; //记录当前连接设备索引
//判断设备当前是否有连接 this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
if (this.deviceId) {
//先断开现有蓝牙连接 //恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
}
this.textArr = tempArr;
}
} else {
uni.closeBLEConnection({ uni.closeBLEConnection({
deviceId: deviceId, deviceId: 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; //设备id this.deviceId = deviceId; //设备id
this.current = index; this.current = index; //记录当前连接设备索引
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索 this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
...@@ -232,21 +279,12 @@ export default { ...@@ -232,21 +279,12 @@ export default {
}); });
} }
}); });
} else {
this.deviceName = name; //设备名称
this.deviceId = deviceId; //设备id
this.current = index;
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
//恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
}
this.textArr = tempArr;
} }
} else {
uni.showToast({
title: '已连接状态',
icon: 'none'
});
} }
}, },
/** /**
...@@ -257,7 +295,7 @@ export default { ...@@ -257,7 +295,7 @@ export default {
success: res => { success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg)); console.log('停止搜索', JSON.stringify(res.errMsg));
this.list = []; this.list = [];
this.startBluetoothDeviceDiscovery(); this.startBluetoothDeviceDiscovery(); //重新搜索蓝牙
//刷新图标交互 //刷新图标交互
this.refreshTransition = true; this.refreshTransition = true;
...@@ -271,13 +309,19 @@ export default { ...@@ -271,13 +309,19 @@ export default {
* 断开连接 * 断开连接
*/ */
closeBLEConnection() { closeBLEConnection() {
uni.showLoading({
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.status = false; this.status = false;
this.deviceId = ''; this.deviceId = '';
this.deviceName = ''; this.deviceName = '';
//恢复蓝牙连接状态的初始值 //恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) { if (this.textArr.length > 0) {
var tempArr = []; var tempArr = [];
...@@ -291,6 +335,15 @@ export default { ...@@ -291,6 +335,15 @@ export default {
title: '已断开蓝牙', title: '已断开蓝牙',
icon: 'success' icon: 'success'
}); });
},
fail: res => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '断开设备失败',
showCancel: false,
confirmText: '关闭'
});
} }
}); });
} }
......
...@@ -36,14 +36,14 @@ ...@@ -36,14 +36,14 @@
<view class="label">{{ text }}</view> <view class="label">{{ text }}</view>
<input type="number" v-model="form.label_num" placeholder-style="font-size:24rpx;color:#404547;" class="uni-input" placeholder="请输入" /> <input type="number" v-model="form.label_num" placeholder-style="font-size:24rpx;color:#404547;" class="uni-input" placeholder="请输入" />
</view> </view>
<view class="box row verCenter"> <!-- <view class="box row verCenter">
<view class="label">放置托盘</view> <view class="label">放置托盘</view>
<view class="el-select row verCenter"> <view class="el-select row verCenter">
<picker @change="bindPickerChange" :value="index" :range="traySelectOption" range-key="tray_sn"> <picker @change="bindPickerChange" :value="index" :range="traySelectOption" range-key="tray_sn">
<view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view> <view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view>
</picker> </picker>
</view> </view>
</view> </view> -->
</view> </view>
<view class="btn row rowCenter verCenter" @click="submit()">提交打印</view> <view class="btn row rowCenter verCenter" @click="submit()">提交打印</view>
</view> </view>
...@@ -81,7 +81,13 @@ export default { ...@@ -81,7 +81,13 @@ export default {
}; };
}, },
onLoad(option) { onLoad(option) {
this.getTraySelectOption(); //停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try { try {
uni.removeStorageSync('device'); uni.removeStorageSync('device');
} catch (e) { } catch (e) {
...@@ -89,9 +95,12 @@ export default { ...@@ -89,9 +95,12 @@ 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.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -121,12 +130,24 @@ export default { ...@@ -121,12 +130,24 @@ export default {
}, },
submit() { submit() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙设备' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
if (!this.form.erp_order_sn_pre) { if (!this.form.erp_order_sn_pre) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
...@@ -165,78 +186,16 @@ export default { ...@@ -165,78 +186,16 @@ export default {
return false; return false;
} }
if (!this.form.tray_remark) {
uni.showToast({
icon: 'error',
title: '请选择放置托盘'
});
return false;
}
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;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showModal({
title: res.err_msg, title: '提示',
icon: 'error' content: res.err_msg,
}); showCancel: false,
} confirmText: '关闭'
});
},
/**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res.errMsg));
//需延时连接,不然会报错
setTimeout(() => {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', JSON.stringify(res.errMsg));
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
}); });
} }
}); });
...@@ -245,6 +204,10 @@ export default { ...@@ -245,6 +204,10 @@ export default {
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
try {
uni.showLoading({
title: '打印中...'
});
console.log('第' + this.print_number + '次打印'); console.log('第' + this.print_number + '次打印');
let data = []; let data = [];
if (this.printLabelData.length > 0) { if (this.printLabelData.length > 0) {
...@@ -268,6 +231,15 @@ export default { ...@@ -268,6 +231,15 @@ export default {
this.sendData64 = arrayBuffer; this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue(); this.writeBLECharacteristicValue();
console.log(data.join('')); console.log(data.join(''));
} catch (e) {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
}
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -275,22 +247,17 @@ export default { ...@@ -275,22 +247,17 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
that.sendDataChange();
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({
deviceId: that.deviceId,
success(res) {
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg));
//清空数据 //清空数据
that.print_number = 0; that.print_number = 0;
that.printLabelData = []; that.printLabelData = [];
...@@ -309,14 +276,12 @@ export default { ...@@ -309,14 +276,12 @@ export default {
} }
}); });
} }
});
}
}, },
fail() { fail() {
uni.hideLoading(); uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '无法打印,请查看标签打印机连接状态', content: '打印失败,请检查打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
...@@ -63,10 +63,27 @@ export default { ...@@ -63,10 +63,27 @@ export default {
} }
}; };
}, },
onLoad(option) {
//停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try {
uni.removeStorageSync('device');
} catch (e) {
// error
}
},
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.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -94,9 +111,20 @@ export default { ...@@ -94,9 +111,20 @@ export default {
}, },
submit() { submit() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙设备' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
...@@ -142,7 +170,7 @@ export default { ...@@ -142,7 +170,7 @@ export default {
this.request(API.getPrintLabel, 'POST', { erp_order_sn: this.form.erp_order_sn_pre + this.form.erp_order_sn_number, label_sort: this.form.label_sort }, true).then(res => { this.request(API.getPrintLabel, 'POST', { erp_order_sn: this.form.erp_order_sn_pre + this.form.erp_order_sn_number, label_sort: this.form.label_sort }, true).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showToast({
title: res.err_msg, title: res.err_msg,
...@@ -152,66 +180,13 @@ export default { ...@@ -152,66 +180,13 @@ export default {
}); });
}, },
/** /**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res));
//需延时连接,不然会报错
setTimeout(function() {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', res);
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', res);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
try {
uni.showLoading({
title: '打印中...'
});
console.log('第' + this.print_number + '次打印'); console.log('第' + this.print_number + '次打印');
let data = []; let data = [];
if (this.printLabelData.length > 0) { if (this.printLabelData.length > 0) {
...@@ -234,6 +209,15 @@ export default { ...@@ -234,6 +209,15 @@ export default {
this.sendData64 = arrayBuffer; this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue(); this.writeBLECharacteristicValue();
console.log(data.join('')); console.log(data.join(''));
} catch (e) {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
}
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -241,24 +225,17 @@ export default { ...@@ -241,24 +225,17 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
setTimeout(function() {
that.sendDataChange();
}, 0);
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({
deviceId: that.deviceId,
success(res) {
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg));
//清空数据 //清空数据
that.print_number = 0; that.print_number = 0;
that.printLabelData = []; that.printLabelData = [];
...@@ -277,14 +254,12 @@ export default { ...@@ -277,14 +254,12 @@ export default {
} }
}); });
} }
});
}
}, },
fail() { fail() {
uni.hideLoading(); uni.hideLoading();
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