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

增加蓝牙连接验证

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