Commit 0f257f5c by liangjianmin

连接蓝牙设备

parent 10d758f8
......@@ -53,6 +53,9 @@
.t2{
font-size: 26rpx;
color: #197ADB;
&.active{
color: #6E767A;
}
}
}
}
......
......@@ -23,9 +23,14 @@
</view>
</view>
<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="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>
......@@ -41,7 +46,9 @@ export default {
deviceName: '',
list: [],
isOpenBle: false,
deviceId: ''
deviceId: '',
textArr: [],
current: -1
};
},
onLoad() {
......@@ -65,6 +72,9 @@ export default {
});
},
methods: {
/**
* 开始搜索蓝牙
*/
startBluetoothDeviceDiscovery() {
uni.showLoading({
title: '搜索蓝牙设备',
......@@ -83,7 +93,7 @@ export default {
uni.hideLoading();
console.log('查找设备失败!');
uni.showToast({
icon: 'none',
icon: 'error',
title: '查找设备失败!',
duration: 3000
});
......@@ -100,8 +110,6 @@ export default {
* 发现外围设备
*/
onBluetoothDeviceFound() {
console.log('监听寻找新设备');
// this.getBluetoothDevices();
uni.onBluetoothDeviceFound(el => {
console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices();
......@@ -111,21 +119,22 @@ export default {
* 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/
getBluetoothDevices() {
console.log('获取蓝牙设备');
uni.getBluetoothDevices({
success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg);
console.log(res);
var devices = [];
var num = 0;
var textArr = [];
for (var i = 0; i < res.devices.length; i++) {
if (res.devices[i].name != '未知设备') {
textArr.push('连接');
devices[num] = res.devices[i];
num++;
}
}
this.list = devices;
if (this.list.length > 0) {
this.textArr = textArr;
if (devices.length > 0) {
uni.hideLoading();
}
}
......@@ -144,32 +153,64 @@ export default {
}
});
},
/**
* 获取蓝牙服务
*/
getBLEDeviceServices() {
//来获取蓝牙设备所有服务
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res.errMsg));
uni.showToast({
title: '蓝牙连接成功',
icon: 'success'
});
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({
title: '连接设备中'
});
//先断开现有蓝牙连接
uni.closeBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg));
this.deviceName = name;
this.deviceId = deviceId;
uni.setStorageSync('device', { name: name, deviceId: deviceId });
this.stopBluetoothDevicesDiscovery();
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;
}
}
});
},
/**
* 刷新蓝牙列表
*/
refresh() {
uni.stopBluetoothDevicesDiscovery({
success: res => {
......@@ -179,14 +220,30 @@ export default {
}
});
},
/**
* 断开连接
*/
closeBLEConnection() {
uni.closeBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg));
console.log('断开蓝牙连接成功:', JSON.stringify(res.errMsg));
this.status = false;
this.deviceId = '';
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 {
print_number: 0,
serviceId: '',
deviceId: '',
device_name: '',
characteristics: '',
text: '总箱数',
index: -1,
......@@ -70,7 +71,6 @@ export default {
sendData64: '',
printLabelData: [],
print_text: '总箱数',
device_name: '',
form: {
erp_order_sn_pre: '',
print_type: 1,
......@@ -81,10 +81,13 @@ export default {
};
},
onShow() {
const deviceData = uni.getStorageSync('device') || '';
const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息
if (deviceData) {
this.deviceId = deviceData.deviceId;
this.device_name = deviceData.name;
} else {
this.deviceId = '';
this.device_name = '';
}
},
created() {
......@@ -112,6 +115,13 @@ export default {
uni.setStorageSync('tray_sn_value', this.traySelectOption[e.target.value].wstylt_id);
},
submit() {
if (!this.device_name) {
uni.showToast({
icon: 'error',
title: '请连接蓝牙设备'
});
return false;
}
if (!this.form.erp_order_sn_pre) {
uni.showToast({
icon: 'error',
......@@ -160,14 +170,7 @@ export default {
this.request(API.printLabel, 'POST', this.form, false).then(res => {
if (res.err_code === 0) {
this.printLabelData = res.data;
if (!this.device_name) {
uni.showToast({
icon: 'error',
title: '请连接蓝牙设备'
});
} else {
this.getBLEDeviceServices();
}
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备
} else {
uni.showToast({
title: res.err_msg,
......@@ -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() {
//来获取蓝牙设备所有服务
let that = this;
uni.showLoading({
title: '连接设备中...'
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
......@@ -246,11 +202,21 @@ export default {
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
//获取蓝牙设备某个服务中所有特征值
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
......@@ -261,13 +227,20 @@ export default {
console.log('服务功能特征值', JSON.stringify(res.errMsg));
},
fail: res => {
console.log('失败', res);
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 向蓝牙发送数据
*/
sendDataChange() {
console.log('第' + this.print_number + '次打印');
//发送数据 二进制
let data = [];
if (this.printLabelData.length > 0) {
data.push('! 60 200 200 250 1\r\n');
......@@ -291,8 +264,10 @@ export default {
this.writeBLECharacteristicValue();
console.log(data.join(''));
},
/**
* 写入二进制数据
*/
writeBLECharacteristicValue() {
//写入二进制数据
let that = this;
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
......@@ -317,7 +292,7 @@ export default {
that.sendData64 = '';
uni.showModal({
title: '提示',
content: '提交成功,请查看标签打印机',
content: '打印成功,请查看标签打印机',
showCancel: false,
confirmText: '关闭',
success: function(res) {
......
<template>
<view class="print-index">
<navigator class="device-box row bothSide verCenter" url="/pages/tag/deviceList">
<view class="row verCenter">
<text class="iconfont icon-juxing7"></text>
<text class="t1">连接设备</text>
</view>
<view class="row verCenter">
<template v-if="device_name != ''">
<text class="t2 active">{{ device_name }}</text>
</template>
<template v-else>
<text class="t2">未连接</text>
</template>
<text class="iconfont icon-sanjiaoxing2"></text>
</view>
</navigator>
<view class="print-form">
<view class="box row verCenter">
<view class="label">订单类型</view>
......@@ -31,6 +46,7 @@ export default {
print_number: 0,
serviceId: '',
deviceId: '',
device_name: '',
characteristics: '',
text: '标签数量',
index_erp_order_sn_pre: -1,
......@@ -47,6 +63,16 @@ export default {
}
};
},
onShow() {
const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息
if (deviceData) {
this.deviceId = deviceData.deviceId;
this.device_name = deviceData.name;
} else {
this.deviceId = '';
this.device_name = '';
}
},
methods: {
onKeyInput: function(event) {
if (event.target.value.length > 3) {
......@@ -67,6 +93,13 @@ export default {
});
},
submit() {
if (!this.device_name) {
uni.showToast({
icon: 'error',
title: '请连接蓝牙设备'
});
return false;
}
if (!this.form.erp_order_sn_pre) {
uni.showToast({
icon: 'error',
......@@ -109,7 +142,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 => {
if (res.err_code === 0) {
this.printLabelData = res.data;
this.openBluetoothAdapter();
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备
} else {
uni.showToast({
title: res.err_msg,
......@@ -118,91 +151,14 @@ export default {
}
});
},
openBluetoothAdapter() {
//先查看系统蓝牙状态
let that = this;
uni.openBluetoothAdapter({
success(res) {
that.startBluetoothDevicesDiscovery();
},
fail() {
uni.showToast({
title: '请打开蓝牙',
icon: 'error'
});
}
});
},
startBluetoothDevicesDiscovery() {
//搜索附近的蓝牙设备并且监听寻找到新设备的事件
/**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '连接设备中',
mask: true
title: '设备打印中'
});
uni.startBluetoothDevicesDiscovery({
success: function(res) {
console.log('搜索设备start', res);
uni.onBluetoothDeviceFound(function(el) {
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-647a') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log('成功搜索设备', el);
console.log(el.devices[0].deviceId);
}
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-9fb6') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log('成功搜索设备', el.devices[0].deviceId);
}
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-bfe3') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log('成功搜索设备', el.devices[0].deviceId);
}
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-c0a6') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log('成功搜索设备', el.devices[0].deviceId);
}
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-be61') {
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('停止搜索', res);
that.getBLEDeviceServices();
}
});
},
getBLEDeviceServices() {
//来获取蓝牙设备所有服务
let that = this;
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
......@@ -218,11 +174,21 @@ export default {
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
//获取蓝牙设备某个服务中所有特征值
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
......@@ -233,39 +199,20 @@ export default {
console.log('服务功能特征值', res);
},
fail: res => {
console.log('失败', res);
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
sendDataChangeAll() {
console.log('第' + this.print_number + '次打印');
//发送数据 二进制
let data = [];
var h = 0; //记录高度增量
data.push('! 10 200 200 ' + this.printLabelData.length * 250 + ' 1\r\n');
for (let i = 0; i < this.printLabelData.length; i++) {
data.push('T 8 0 10 ' + (50 * (i + 1) - 30 * 1 + h) + ' 入仓号:' + this.printLabelData[i].erp_order_sn + '\r\n');
if (this.print_type == 1) {
data.push('T 8 0 10 ' + (50 * (i + 2) - 30 * 1 + h) + ' 总箱数:' + this.printLabelData[0].label_num + '箱\r\n');
} else {
data.push('T 8 0 10 ' + (50 * (i + 2) - 30 * 1 + h) + ' 箱号:' + this.printLabelData[i].label_sort + '/' + this.printLabelData[0].label_num + '\r\n');
}
data.push('T 8 0 10 ' + (50 * (i + 3) - 30 * 1 + h) + ' 时间:' + this.printLabelData[i].create_time_cn + '\r\n');
data.push('T 8 0 10 ' + (50 * (i + 4) - 30 * 1 + h) + ' 仓库代码:' + i + '\r\n');
data.push('B QR 290 ' + (50 * (i + 1) - 30 * 1 + h) + ' M 2 U 8\r\n');
data.push('MA,' + this.printLabelData[i].erp_order_sn + '\r\n');
data.push('ENDQR\r\n');
h += 210;
}
data.push('PRINT\r\n');
let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue();
console.log(data.join(''));
},
/**
* 向蓝牙发送数据
*/
sendDataChange() {
console.log('第' + this.print_number + '次打印');
//发送数据 二进制
let data = [];
if (this.printLabelData.length > 0) {
data.push('! 60 200 200 200 1\r\n');
......@@ -288,8 +235,10 @@ export default {
this.writeBLECharacteristicValue();
console.log(data.join(''));
},
/**
* 写入二进制数据
*/
writeBLECharacteristicValue() {
//写入二进制数据
let that = this;
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
......@@ -297,8 +246,9 @@ export default {
characteristicId: that.characteristics,
value: that.sendData64,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg);
that.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg));
console.log('打印计数:' + that.print_number);
if (that.print_number < that.printLabelData.length) {
setTimeout(function() {
that.sendDataChange();
......@@ -315,7 +265,7 @@ export default {
that.sendData64 = '';
uni.showModal({
title: '提示',
content: '提交成功,请查看标签打印机',
content: '打印成功,请查看标签打印机',
showCancel: false,
confirmText: '关闭',
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