Commit e1bfa2c2 authored by 沈翠玲's avatar 沈翠玲

蓝牙打印机对接

parent a1998bc4
...@@ -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) shortToast("没有开启蓝牙"); if(!this.state.bluetoothEnable) this.shortToast("没有开启蓝牙");
this.listenBluetoothStatus(); this.listenBluetoothStatus();
}, },
shortToast(msg) { shortToast(msg) {
...@@ -103,12 +103,12 @@ var blueToothTool = { ...@@ -103,12 +103,12 @@ var blueToothTool = {
*/ */
turnOnBluetooth() { turnOnBluetooth() {
if(btAdapter == null) { if(btAdapter == null) {
shortToast("没有蓝牙"); this.shortToast("没有蓝牙");
return; return;
} }
if(!btAdapter.isEnabled()) { if(!btAdapter.isEnabled()) {
if(activity == null) { if(activity == null) {
shortToast("未获取到activity"); this.shortToast("未获取到activity");
return; return;
} else { } else {
let intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); let intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
...@@ -117,7 +117,7 @@ var blueToothTool = { ...@@ -117,7 +117,7 @@ var blueToothTool = {
return; return;
} }
} else { } else {
shortToast("蓝牙已经打开"); this.shortToast("蓝牙已经打开");
} }
}, },
/** /**
...@@ -141,9 +141,9 @@ var blueToothTool = { ...@@ -141,9 +141,9 @@ var blueToothTool = {
if(btAdapter != null && btAdapter.isEnabled()) { if(btAdapter != null && btAdapter.isEnabled()) {
btAdapter.disable(); btAdapter.disable();
shortToast("蓝牙关闭成功"); this.shortToast("蓝牙关闭成功");
} else { } else {
shortToast("蓝牙已经关闭"); this.shortToast("蓝牙已经关闭");
} }
}, },
/** /**
...@@ -158,7 +158,7 @@ var blueToothTool = { ...@@ -158,7 +158,7 @@ var blueToothTool = {
if(btAdapter != null && btAdapter.isEnabled()) { if(btAdapter != null && btAdapter.isEnabled()) {
pairedDevicesAndroid = btAdapter.getBondedDevices(); pairedDevicesAndroid = btAdapter.getBondedDevices();
} else { } else {
shortToast("蓝牙未开启"); this.shortToast("蓝牙未开启");
} }
if(!pairedDevicesAndroid) { if(!pairedDevicesAndroid) {
...@@ -348,7 +348,50 @@ var blueToothTool = { ...@@ -348,7 +348,50 @@ var blueToothTool = {
btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID); btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);
} catch(e) { } catch(e) {
console.error(e); console.error(e);
shortToast("连接失败,获取Socket失败!"); this.shortToast("连接失败,获取Socket失败!");
callback(false)
return false;
}
try {
invoke(btSocket, "connect");
this.readData(); //读数据
this.shortToast("连接成功");
callback(true)
} catch(e) {
console.error(e);
this.shortToast("连接失败");
callback(false)
try {
btSocket.close();
btSocket = null;
} catch(e1) {
console.error(e1);
}
return false;
}
return true;
},
/**
* 根据配对连接
* @param {Stirng} address
* @return {Boolean}
*/
connPairDevice(device, callback) {
let InputStream = plus.android.importClass("java.io.InputStream");
let OutputStream = plus.android.importClass("java.io.OutputStream");
let BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");
if(btSocket != null) {
this.closeBtSocket();
}
this.state.readThreadState = false;
try {
console.log('device', device)
btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);
} catch(e) {
console.error(e);
this.shortToast("连接失败,获取Socket失败!");
callback(false) callback(false)
return false; return false;
} }
...@@ -427,6 +470,7 @@ var blueToothTool = { ...@@ -427,6 +470,7 @@ var blueToothTool = {
try { try {
btInStream = invoke(btSocket, "getInputStream"); btInStream = invoke(btSocket, "getInputStream");
btOutStream = invoke(btSocket, "getOutputStream"); btOutStream = invoke(btSocket, "getOutputStream");
console.log('btOutStream', btOutStream)
} catch(e) { } catch(e) {
console.error(e); console.error(e);
this.shortToast("创建输入输出流失败!"); this.shortToast("创建输入输出流失败!");
......
...@@ -48,28 +48,35 @@ module.exports = { ...@@ -48,28 +48,35 @@ module.exports = {
initBle(){ initBle(){
//#ifdef APP-PLUS //#ifdef APP-PLUS
// 蓝牙 // 蓝牙
this.$u.vuex('vuex_printConnect', false); try {
bluetoothTool.init({ this.$u.vuex('vuex_printConnect', false);
listenBTStatusCallback: (state)=> { bluetoothTool.init({
console.log('listenBTStatusCallback', state); listenBTStatusCallback: (state)=> {
if (state === 'STATE_OFF') { console.log('listenBTStatusCallback', state);
this.$u.vuex('vuex_printConnect', false); if (state === 'STATE_OFF') {
this.$u.vuex('vuex_printConnect', false);
this.$u.toast('未连接蓝牙')
}
},
discoveryDeviceCallback: this.onDevice,
discoveryFinishedCallback: () => {
console.log('搜索到的设备', this.devices)
},
readDataCallback: (dataByteArr) => {
this.$u.vuex('vuex_printConnect', false);
},
connExceptionCallback: (e) => {
console.log('connExceptionCallback', e);
this.$u.vuex('vuex_printConnect', false);
} }
}, });
discoveryDeviceCallback: this.onDevice, this.first = true
discoveryFinishedCallback: function() { this.searchBle()
console.log('搜索到的设备', that.devices) } catch (error) {
}, console.log('error', error)
readDataCallback: function(dataByteArr) { }
this.$u.vuex('vuex_printConnect', false);
},
connExceptionCallback: function(e) {
console.log('connExceptionCallback', e);
this.$u.vuex('vuex_printConnect', false);
}
});
this.first = true
this.searchBle()
//#endif //#endif
}, },
searchBle() { searchBle() {
...@@ -79,26 +86,41 @@ module.exports = { ...@@ -79,26 +86,41 @@ module.exports = {
uni.openBluetoothAdapter({ uni.openBluetoothAdapter({
success(res) { success(res) {
this.devices = [] this.devices = []
console.log("打开 蓝牙模块,开始搜索模式...", res)
uni.getBluetoothAdapterState({ uni.getBluetoothAdapterState({
success: function(res) { success: function(res) {
console.log(res) console.log(res)
if (res.available) { if (res.available) {
const list = bluetoothTool.getPairedDevices()
const item = list.find(v=>v.name === that.vuex_printerInfo.printerName)
if(item && item.device) {
bluetoothTool.connPairDevice(item.device,(result)=>{
if(result) {
that.$u.vuex('vuex_printConnect', true);
}
console.log('连接结果:',result)
});
}
//搜索蓝牙 //搜索蓝牙
//开始搜寻附近的蓝牙外围设备 //开始搜寻附近的蓝牙外围设备
uni.startBluetoothDevicesDiscovery({ uni.startBluetoothDevicesDiscovery({
success(res) { success(res) {
console.log('开始搜寻附近的蓝牙外围设备', res) if (!that.vuex_printConnect) {
bluetoothTool.discoveryNewDevice(); bluetoothTool.discoveryNewDevice();
}
} }
}) })
} else { } else {
console.log('本机蓝牙不可用') console.log('本机蓝牙不可用')
} }
}, },
fali: () => {
console.log('本机蓝牙不可用')
}
}) })
//that.onDevice() //that.onDevice()
},
fail: function(msg) {
console.log(msg)
} }
}) })
}, },
...@@ -110,7 +132,7 @@ module.exports = { ...@@ -110,7 +132,7 @@ module.exports = {
name: newDevice.name, name: newDevice.name,
address: newDevice.address address: newDevice.address
}) })
if (newDevice.name === 'CT-428D' && newDevice.type === 1 && this.first) { if (newDevice.name === this.vuex_printerInfo.printerName && newDevice.type === 1 && this.first) {
this.onConn(newDevice) this.onConn(newDevice)
bluetoothTool.cancelDiscovery() bluetoothTool.cancelDiscovery()
this.first = false this.first = false
......
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