1.VUE如何使用H5+PLUS
在Main.js中加入如下代码,此代码是为了把plus方式绑定到Vue原型链上,避免无法使用Plus得情况
Vue.prototype.$plusExtends = fn => {
if (window.plus) {
setTimeout(fn, 0)
} else {
document.addEventListener('plusready', fn, false)
}
}
2.页面中如何调取PLUS方式
找到任意Vue文件中methods方法,,以下已扫描二维码为例,注意这里使用得plus方式前一定要加入window,不要直接使用plus得相关方法
this.$plusExtends(() => {
this.barcode = new window.plus.barcode.Barcode(
'qrCode',
[window.plus.barcode.QR, window.plus.barcode.CODE128]
)
this.barcode.onmarked = (type, result) => {
console.log(result)
this.barcode.close()
}
this.barcode.onerror = (err) => {
console.log(err)
this.barcode.close()
}
this.barcode.start()
})
3.如何使用5+plus配合蓝牙打印机
分为两种方式,请看下面两段代码,第一种固定mac地址,无需搜索
this.$plusExtends(() => {
let main = window.plus.android.runtimeMainActivity()
let BluetoothAdapter = window.plus.android.importClass('android.bluetooth.BluetoothAdapter')
let UUID = window.plus.android.importClass('java.util.UUID')
let uuid = UUID.fromString('00001101-0000-1000-8000-00805F9B34FB')
let BAdapter = BluetoothAdapter.getDefaultAdapter()
let device = BAdapter.getRemoteDevice(localStorage.mac) // 这里填写已知得mac地址
window.plus.android.importClass(device)
let bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
window.plus.android.importClass(bluetoothSocket)
bluetoothSocket.connect()
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream()
window.plus.android.importClass(outputStream)
var newBytes = '打印测试\r\n';
var bytes = window.plus.android.invoke(newBytes, 'getBytes', 'GBK')
outputStream.write(bytes)
outputStream.flush()
device = null
bluetoothSocket.close()
}
})
这里有三个坑,希望能帮到你
1.传入得newBytes 需要按照打印机得指令来传递,有的打印机直接认识汉字可以直接传递,有的打印机必须要先设置一些指令集得属性,才能得到反应
2.部分打印机链接完电脑后需要重启,才能正常匹配蓝牙,最好不要边链接蓝牙,边用电脑打印,说不定会出现各种奇怪的问题
3.网上得大部分DEMO都没有说字符集得问题,这里用的是GBK得,你要看看打印机是不是支持这种字符集,有的可能只支持UTF-8
第二种无固定mac地址,需要搜索,链接代码同上,目前给出搜索匹配mac地址代码
function searchDevices() {
//注册类
var main = window.plus.android.runtimeMainActivity();
var IntentFilter = window.plus.android.importClass('android.content.IntentFilter');
var BluetoothAdapter = window.plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BluetoothDevice = window.plus.android.importClass("android.bluetooth.BluetoothDevice");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
console.log("开始搜索设备");
var filter = new IntentFilter();
var bdevice = new BluetoothDevice();
BAdapter.startDiscovery(); //开启搜索
var receiver;
receiver = window.plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
window.plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
console.log(intent.getAction()); //获取action
if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED"){
main.unregisterReceiver(receiver);//取消监听
console.log("搜索结束")
} else {
BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
console.log('BleDevice': BleDevice.getAddress()) // 获取到mac地址
}
}
});
filter.addAction(bdevice.ACTION_FOUND);
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BAdapter.ACTION_STATE_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
}
欢迎留言讨论,谢谢各位。