Commit 91c2163d authored by 沈翠玲's avatar 沈翠玲

增加蓝牙连接验证

parent 98cd840f
...@@ -70,7 +70,7 @@ var blueToothTool = { ...@@ -70,7 +70,7 @@ var blueToothTool = {
init(setOptions) { init(setOptions) {
Object.assign(this.options, setOptions); Object.assign(this.options, setOptions);
this.state.bluetoothEnable = this.getBluetoothStatus(); this.state.bluetoothEnable = this.getBluetoothStatus();
if(!this.state.bluetoothEnable) this.shortToast("没有开启蓝牙"); if(!this.state.bluetoothEnable) this.options.listenBTStatusCallback && this.options.listenBTStatusCallback('STATE_OFF_First');
this.listenBluetoothStatus(); this.listenBluetoothStatus();
}, },
shortToast(msg) { shortToast(msg) {
...@@ -245,6 +245,7 @@ var blueToothTool = { ...@@ -245,6 +245,7 @@ var blueToothTool = {
} }
} }
let options = this.options let options = this.options
let that = this
btFindReceiver = plus.android.implements("io.dcloud.android.content.BroadcastReceiver", { btFindReceiver = plus.android.implements("io.dcloud.android.content.BroadcastReceiver", {
"onReceive": function(context, intent) { "onReceive": function(context, intent) {
plus.android.importClass(context); plus.android.importClass(context);
...@@ -262,8 +263,12 @@ var blueToothTool = { ...@@ -262,8 +263,12 @@ var blueToothTool = {
options.discoveryDeviceCallback && options.discoveryDeviceCallback(newDevice); options.discoveryDeviceCallback && options.discoveryDeviceCallback(newDevice);
} }
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { // 搜索完成 if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { // 搜索完成
cancelDiscovery(); that.cancelDiscovery();
options.discoveryFinishedCallback && options.discoveryFinishedCallback(); if (btSocket) {
options.discoveryFinishedCallback && options.discoveryFinishedCallback(btSocket.isConnected())
} else {
options.discoveryFinishedCallback && options.discoveryFinishedCallback()
}
} }
} }
}); });
...@@ -353,10 +358,8 @@ var blueToothTool = { ...@@ -353,10 +358,8 @@ var blueToothTool = {
return false; return false;
} }
try { try {
invoke(btSocket, "connect"); btSocket.connect()
this.readData(); //读数据 this.readData();
this.shortToast("连接成功");
callback(true)
} catch(e) { } catch(e) {
console.error(e); console.error(e);
this.shortToast("连接失败"); this.shortToast("连接失败");
......
...@@ -144,7 +144,9 @@ export default { ...@@ -144,7 +144,9 @@ export default {
uni.$on('changeWorkunit', (indexCode) => { uni.$on('changeWorkunit', (indexCode) => {
this.handleCommand('workunit'); this.handleCommand('workunit');
}); });
uni.$on('BLEstatus', (status) => {
this.BLEBindbyStatus(status);
});
// this.checkWorkstation(); // this.checkWorkstation();
await this.getWorkUnitList(); await this.getWorkUnitList();
this.setWorkUnit(this.workUnitList[0]) this.setWorkUnit(this.workUnitList[0])
...@@ -152,6 +154,7 @@ export default { ...@@ -152,6 +154,7 @@ export default {
destroyed() { destroyed() {
uni.$off('switchTab'); uni.$off('switchTab');
uni.$off('changeWorkunit'); uni.$off('changeWorkunit');
uni.$off('BLEstatus');
}, },
methods: { methods: {
handleTest() { handleTest() {
...@@ -217,6 +220,17 @@ export default { ...@@ -217,6 +220,17 @@ export default {
uni.$emit('switchWorkunit', station); uni.$emit('switchWorkunit', station);
}, },
materialRequestFormShow(row) { materialRequestFormShow(row) {
if(row.connectionType !== 'bluetooth') {
this.confirmBIND(row)
} else {
uni.showLoading({
title: '绑定中'
})
this.currentRow = row
this.initBle()
}
},
confirmBIND(row){
this.$u.api this.$u.api
.addUserPrint({ .addUserPrint({
userId: this.vuex_user.userId, userId: this.vuex_user.userId,
...@@ -247,9 +261,7 @@ export default { ...@@ -247,9 +261,7 @@ export default {
printerName: res.data.printerName, printerName: res.data.printerName,
connectionType: res.data.connectionType connectionType: res.data.connectionType
}); });
if (res.data.connectionType === 'bluetooth') { if (res.data.connectionType !== 'bluetooth') {
this.initBle()
} else {
this.createSocket() this.createSocket()
} }
} }
...@@ -261,6 +273,17 @@ export default { ...@@ -261,6 +273,17 @@ export default {
} }
}); });
}, },
BLEBindbyStatus(status) {
if(!this.currentRow) return
if(status && this.cuDevice.name === this.currentRow.printerName) {
uni.hideLoading()
this.confirmBIND(this.currentRow)
} else if (!status) {
uni.hideLoading()
this.currentRow = null
this.$u.toast('蓝牙连接失败')
}
},
handleCommand(command) { handleCommand(command) {
if (command === 'exit') { if (command === 'exit') {
this.loading = true; this.loading = true;
......
...@@ -30,19 +30,23 @@ module.exports = { ...@@ -30,19 +30,23 @@ module.exports = {
data() { data() {
return { return {
devices: [], devices: [],
currDev: null, cuDevice: null,
connId: '',
first:true, first:true,
firstDraw: true, currentRow: null,
tableDomId: '',
tableImgPath: '',
canvasWidth: 800, canvasWidth: 800,
canvasHeight: 600 canvasHeight: 600
} }
}, },
computed: { computed: {
// 将vuex的state中的所有变量,解构到全局混入的mixin中 // 将vuex的state中的所有变量,解构到全局混入的mixin中
...mapState($uStoreKey) ...mapState($uStoreKey),
currentBleName() {
if(this.currentRow && this.currentRow.printerName){
return this.currentRow.printerName
} else {
return this.vuex_printerInfo.printerName
}
}
}, },
methods: { methods: {
initBle(){ initBle(){
...@@ -55,12 +59,23 @@ module.exports = { ...@@ -55,12 +59,23 @@ module.exports = {
console.log('listenBTStatusCallback', state); console.log('listenBTStatusCallback', state);
if (state === 'STATE_OFF') { if (state === 'STATE_OFF') {
this.$u.vuex('vuex_printConnect', false); this.$u.vuex('vuex_printConnect', false);
this.$u.toast('未连接蓝牙') }
if (state === 'STATE_OFF_First') {
this.$u.vuex('vuex_printConnect', false);
uni.$emit('BLEstatus', false)
} }
}, },
discoveryDeviceCallback: this.onDevice, discoveryDeviceCallback: this.onDevice,
discoveryFinishedCallback: () => { discoveryFinishedCallback: (status) => {
console.log('搜索到的设备', this.devices) console.log('搜索完成', this.devices, status)
if(!this.cuDevice) {
this.$u.vuex('vuex_printConnect', false);
uni.$emit('BLEstatus', false)
}
if (typeof status === 'boolean') {
this.$u.vuex('vuex_printConnect', status);
uni.$emit('BLEstatus', status)
}
}, },
readDataCallback: (dataByteArr) => { readDataCallback: (dataByteArr) => {
this.$u.vuex('vuex_printConnect', false); this.$u.vuex('vuex_printConnect', false);
...@@ -71,6 +86,7 @@ module.exports = { ...@@ -71,6 +86,7 @@ module.exports = {
} }
}); });
this.first = true this.first = true
this.cuDevice = null
this.searchBle() this.searchBle()
} catch (error) { } catch (error) {
console.log('error', error) console.log('error', error)
...@@ -91,11 +107,15 @@ module.exports = { ...@@ -91,11 +107,15 @@ module.exports = {
console.log(res) console.log(res)
if (res.available) { if (res.available) {
const list = bluetoothTool.getPairedDevices() const list = bluetoothTool.getPairedDevices()
const item = list.find(v=>v.name === that.vuex_printerInfo.printerName) const item = list.find(v=>v.name === that.currentBleName)
if(item && item.device) { if(item && item.device) {
that.cuDevice = item
bluetoothTool.connPairDevice(item.device,(result)=>{ bluetoothTool.connPairDevice(item.device,(result)=>{
if(result) { if(result) {
that.$u.vuex('vuex_printConnect', true); that.$u.vuex('vuex_printConnect', true);
uni.$emit('BLEstatus', true)
} else {
uni.$emit('BLEstatus', false)
} }
console.log('连接结果:',result) console.log('连接结果:',result)
}); });
...@@ -117,7 +137,6 @@ module.exports = { ...@@ -117,7 +137,6 @@ module.exports = {
console.log('本机蓝牙不可用') console.log('本机蓝牙不可用')
} }
}) })
//that.onDevice()
}, },
fail: function(msg) { fail: function(msg) {
console.log(msg) console.log(msg)
...@@ -132,9 +151,9 @@ module.exports = { ...@@ -132,9 +151,9 @@ module.exports = {
name: newDevice.name, name: newDevice.name,
address: newDevice.address address: newDevice.address
}) })
if (newDevice.name === this.vuex_printerInfo.printerName && newDevice.type === 1 && this.first) { if (newDevice.name === this.currentBleName && (newDevice.type === 1 || newDevice.type === 3) && this.first) {
this.cuDevice = newDevice
this.onConn(newDevice) this.onConn(newDevice)
bluetoothTool.cancelDiscovery()
this.first = false this.first = false
} }
} }
...@@ -149,6 +168,9 @@ module.exports = { ...@@ -149,6 +168,9 @@ module.exports = {
bluetoothTool.connDevice(item.address,(result)=>{ bluetoothTool.connDevice(item.address,(result)=>{
if(result) { if(result) {
this.$u.vuex('vuex_printConnect', true); this.$u.vuex('vuex_printConnect', true);
uni.$emit('BLEstatus', true)
} else {
uni.$emit('BLEstatus', false)
} }
console.log('连接结果:',result) console.log('连接结果:',result)
}); });
...@@ -175,7 +197,6 @@ module.exports = { ...@@ -175,7 +197,6 @@ module.exports = {
width: that.canvasWidth, width: that.canvasWidth,
height: that.canvasHeight, height: that.canvasHeight,
success: (res)=> { success: (res)=> {
this.firstDraw = false
var command = tsc.jpPrinter.createNew() var command = tsc.jpPrinter.createNew()
command.init() command.init()
command.setSize(100, 80) command.setSize(100, 80)
...@@ -185,7 +206,6 @@ module.exports = { ...@@ -185,7 +206,6 @@ module.exports = {
command.setPagePrint() command.setPagePrint()
console.log('数据发送: \n',command.getRawData()) console.log('数据发送: \n',command.getRawData())
let data = command.getData() let data = command.getData()
console.log('data', data)
bluetoothTool.sendByteData(data) bluetoothTool.sendByteData(data)
console.log('发送完毕') console.log('发送完毕')
}, },
......
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 to comment