uniapp调用芯烨xprinter打印机安卓sdk
【摘要】
因为xprinter的安卓sdk需要在安卓项目里配置service,但是uniapp里云打包无法配置service,所以只能用原生插件离线打包的形式开发。https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
UniPlugin-Hello-AS...
因为xprinter的安卓sdk需要在安卓项目里配置service,但是uniapp里云打包无法配置service,所以只能用原生插件离线打包的形式开发。https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
UniPlugin-Hello-AS安卓项目引入 PosPrinterSDK.jar
1.android自定义插件代码
-
package com.yf.xprinter;
-
-
import android.app.Activity;
-
import android.content.ComponentName;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.IBinder;
-
-
import com.taobao.weex.annotation.JSMethod;
-
-
import net.posprinter.posprinterface.IMyBinder;
-
import net.posprinter.posprinterface.ProcessData;
-
import net.posprinter.posprinterface.TaskCallback;
-
import net.posprinter.service.PosprinterService;
-
import net.posprinter.utils.PosPrinterDev;
-
import net.posprinter.utils.StringUtils;
-
-
import java.util.HashMap;
-
import java.util.List;
-
-
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
-
import io.dcloud.feature.uniapp.common.UniModule;
-
-
public class print extends UniModule {
-
@JSMethod(uiThread = false)
-
public String getVersion() {
-
return "V20210622";
-
}
-
-
private static IMyBinder myBinder;
-
private static ServiceConnection mSerconnection;
-
@JSMethod(uiThread = false)
-
public boolean serviceConnection(final UniJSCallback callback) {
-
mSerconnection= new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
myBinder= (IMyBinder) service;
-
callback.invokeAndKeepAlive("connect");
-
}
-
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
callback.invokeAndKeepAlive("disconnect");
-
}
-
};
-
-
//bind service,get imyBinder
-
Intent intent =new Intent(mWXSDKInstance.getContext(), PosprinterService.class);
-
-
Activity activity = (Activity )mWXSDKInstance.getContext();
-
activity.bindService(intent,mSerconnection,Activity.BIND_AUTO_CREATE);
-
return true;
-
}
-
-
@JSMethod(uiThread = false)
-
public List<String> getUsbPathNames() {
-
return PosPrinterDev.GetUsbPathNames(mWXSDKInstance.getContext());
-
}
-
-
@JSMethod(uiThread = false)
-
public boolean connectUsbPort(String usbAddress,final UniJSCallback callback) {
-
myBinder.ConnectUsbPort(mWXSDKInstance.getContext(),usbAddress, new TaskCallback() {
-
@Override
-
public void OnSucceed() {
-
callback.invoke("succeed");
-
}
-
-
@Override
-
public void OnFailed() {
-
callback.invoke("failed");
-
}
-
});
-
return true;
-
}
-
-
@JSMethod(uiThread = false)
-
public boolean testWrite(final UniJSCallback callback) {
-
byte[] bytes= StringUtils.strTobytes("1234567890qwertyuiopakjbdscm nkjdv mcdskjb");
-
myBinder.Write(bytes, new TaskCallback() {
-
@Override
-
public void OnSucceed() {
-
callback.invoke("succeed");
-
}
-
-
@Override
-
public void OnFailed() {
-
callback.invoke("failed");
-
}
-
});
-
return true;
-
}
-
-
@JSMethod(uiThread = false)
-
public boolean write(byte[] bytes,final UniJSCallback callback) {
-
myBinder.Write(bytes, new TaskCallback() {
-
@Override
-
public void OnSucceed() {
-
callback.invoke("succeed");
-
}
-
-
@Override
-
public void OnFailed() {
-
callback.invoke("failed");
-
}
-
});
-
return true;
-
}
-
-
@JSMethod(uiThread = false)
-
public boolean writeSendData(final List<byte[]> byteList, final UniJSCallback callback) {
-
myBinder.WriteSendData(new TaskCallback() {
-
@Override
-
public void OnSucceed() {
-
callback.invoke("succeed");
-
}
-
-
@Override
-
public void OnFailed() {
-
callback.invoke("failed");
-
}
-
}, new ProcessData() {
-
@Override
-
public List<byte[]> processDataBeforeSend() {
-
return byteList;
-
}
-
});
-
return true;
-
}
-
-
-
}
2.uniapp页面代码
-
<template>
-
<view>
-
<view>
-
<button @click="getversion()">getversion</button>
-
<button @click="serviceConnection()">serviceConnection</button>
-
<input style="width: 100%;" v-model="usbport"/>
-
<button @click="getUsbPathNames()">getUsbPathNames</button>
-
<button @click="connectUsbPort()">connectUsbPort</button>
-
<button @click="testWrite()">testWrite</button>
-
<button @click="printText()">printText</button>
-
</view>
-
<text v-for="(item,index) in msglist">{{item}}\n</text>
-
</view>
-
</template>
-
<script>
-
const xprinter = uni.requireNativePlugin('zzzili-xprinter');
-
export default {
-
data() {
-
return {
-
msglist: [],
-
usbport:'/dev/bus/usb/002/008'
-
}
-
},
-
onLoad() {
-
this.msglist.push('start test print')
-
},
-
methods: {
-
getversion: function() {
-
this.msglist.push(xprinter.getVersion());
-
},
-
serviceConnection: function() {
-
-
var that = this;
-
var result = xprinter.serviceConnection(function(res){
-
console.log(res);
-
that.msglist.push(res);
-
});
-
this.msglist.push(JSON.stringify(result));
-
},
-
getUsbPathNames: function() {
-
var that = this;
-
var result = xprinter.getUsbPathNames();
-
this.msglist.push(JSON.stringify(result));
-
},
-
connectUsbPort: function() {
-
var that = this;
-
var result = xprinter.connectUsbPort(this.usbport,function(res){
-
console.log(res);
-
that.msglist.push(res);
-
});
-
this.msglist.push(result);
-
},
-
printText: function() {
-
var that = this;
-
var str = "123456789zbc";
-
var result = xprinter.write(str.getBytes(),function(res){
-
console.log(res);
-
that.msglist.push(res);
-
});
-
this.msglist.push(result);
-
},
-
testWrite: function() {
-
var that = this;
-
var result = xprinter.testWrite(function(res){
-
console.log(res);
-
that.msglist.push(res);
-
});
-
this.msglist.push(result);
-
},
-
}
-
}
-
</script>
-
<style>
-
</style>
3.安卓项目AndroidManifest.xml文件中增加配置
<service android:name="net.posprinter.service.PosprinterService" />
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/118105543
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)