uniapp调用芯烨xprinter打印机安卓sdk

举报
清雨小竹 发表于 2022/09/25 00:50:41 2022/09/25
【摘要】 因为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自定义插件代码


  
  1. package com.yf.xprinter;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.IBinder;
  7. import com.taobao.weex.annotation.JSMethod;
  8. import net.posprinter.posprinterface.IMyBinder;
  9. import net.posprinter.posprinterface.ProcessData;
  10. import net.posprinter.posprinterface.TaskCallback;
  11. import net.posprinter.service.PosprinterService;
  12. import net.posprinter.utils.PosPrinterDev;
  13. import net.posprinter.utils.StringUtils;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import io.dcloud.feature.uniapp.bridge.UniJSCallback;
  17. import io.dcloud.feature.uniapp.common.UniModule;
  18. public class print extends UniModule {
  19. @JSMethod(uiThread = false)
  20. public String getVersion() {
  21. return "V20210622";
  22. }
  23. private static IMyBinder myBinder;
  24. private static ServiceConnection mSerconnection;
  25. @JSMethod(uiThread = false)
  26. public boolean serviceConnection(final UniJSCallback callback) {
  27. mSerconnection= new ServiceConnection() {
  28. @Override
  29. public void onServiceConnected(ComponentName name, IBinder service) {
  30. myBinder= (IMyBinder) service;
  31. callback.invokeAndKeepAlive("connect");
  32. }
  33. @Override
  34. public void onServiceDisconnected(ComponentName name) {
  35. callback.invokeAndKeepAlive("disconnect");
  36. }
  37. };
  38. //bind service,get imyBinder
  39. Intent intent =new Intent(mWXSDKInstance.getContext(), PosprinterService.class);
  40. Activity activity = (Activity )mWXSDKInstance.getContext();
  41. activity.bindService(intent,mSerconnection,Activity.BIND_AUTO_CREATE);
  42. return true;
  43. }
  44. @JSMethod(uiThread = false)
  45. public List<String> getUsbPathNames() {
  46. return PosPrinterDev.GetUsbPathNames(mWXSDKInstance.getContext());
  47. }
  48. @JSMethod(uiThread = false)
  49. public boolean connectUsbPort(String usbAddress,final UniJSCallback callback) {
  50. myBinder.ConnectUsbPort(mWXSDKInstance.getContext(),usbAddress, new TaskCallback() {
  51. @Override
  52. public void OnSucceed() {
  53. callback.invoke("succeed");
  54. }
  55. @Override
  56. public void OnFailed() {
  57. callback.invoke("failed");
  58. }
  59. });
  60. return true;
  61. }
  62. @JSMethod(uiThread = false)
  63. public boolean testWrite(final UniJSCallback callback) {
  64. byte[] bytes= StringUtils.strTobytes("1234567890qwertyuiopakjbdscm nkjdv mcdskjb");
  65. myBinder.Write(bytes, new TaskCallback() {
  66. @Override
  67. public void OnSucceed() {
  68. callback.invoke("succeed");
  69. }
  70. @Override
  71. public void OnFailed() {
  72. callback.invoke("failed");
  73. }
  74. });
  75. return true;
  76. }
  77. @JSMethod(uiThread = false)
  78. public boolean write(byte[] bytes,final UniJSCallback callback) {
  79. myBinder.Write(bytes, new TaskCallback() {
  80. @Override
  81. public void OnSucceed() {
  82. callback.invoke("succeed");
  83. }
  84. @Override
  85. public void OnFailed() {
  86. callback.invoke("failed");
  87. }
  88. });
  89. return true;
  90. }
  91. @JSMethod(uiThread = false)
  92. public boolean writeSendData(final List<byte[]> byteList, final UniJSCallback callback) {
  93. myBinder.WriteSendData(new TaskCallback() {
  94. @Override
  95. public void OnSucceed() {
  96. callback.invoke("succeed");
  97. }
  98. @Override
  99. public void OnFailed() {
  100. callback.invoke("failed");
  101. }
  102. }, new ProcessData() {
  103. @Override
  104. public List<byte[]> processDataBeforeSend() {
  105. return byteList;
  106. }
  107. });
  108. return true;
  109. }
  110. }

2.uniapp页面代码


  
  1. <template>
  2. <view>
  3. <view>
  4. <button @click="getversion()">getversion</button>
  5. <button @click="serviceConnection()">serviceConnection</button>
  6. <input style="width: 100%;" v-model="usbport"/>
  7. <button @click="getUsbPathNames()">getUsbPathNames</button>
  8. <button @click="connectUsbPort()">connectUsbPort</button>
  9. <button @click="testWrite()">testWrite</button>
  10. <button @click="printText()">printText</button>
  11. </view>
  12. <text v-for="(item,index) in msglist">{{item}}\n</text>
  13. </view>
  14. </template>
  15. <script>
  16. const xprinter = uni.requireNativePlugin('zzzili-xprinter');
  17. export default {
  18. data() {
  19. return {
  20. msglist: [],
  21. usbport:'/dev/bus/usb/002/008'
  22. }
  23. },
  24. onLoad() {
  25. this.msglist.push('start test print')
  26. },
  27. methods: {
  28. getversion: function() {
  29. this.msglist.push(xprinter.getVersion());
  30. },
  31. serviceConnection: function() {
  32. var that = this;
  33. var result = xprinter.serviceConnection(function(res){
  34. console.log(res);
  35. that.msglist.push(res);
  36. });
  37. this.msglist.push(JSON.stringify(result));
  38. },
  39. getUsbPathNames: function() {
  40. var that = this;
  41. var result = xprinter.getUsbPathNames();
  42. this.msglist.push(JSON.stringify(result));
  43. },
  44. connectUsbPort: function() {
  45. var that = this;
  46. var result = xprinter.connectUsbPort(this.usbport,function(res){
  47. console.log(res);
  48. that.msglist.push(res);
  49. });
  50. this.msglist.push(result);
  51. },
  52. printText: function() {
  53. var that = this;
  54. var str = "123456789zbc";
  55. var result = xprinter.write(str.getBytes(),function(res){
  56. console.log(res);
  57. that.msglist.push(res);
  58. });
  59. this.msglist.push(result);
  60. },
  61. testWrite: function() {
  62. var that = this;
  63. var result = xprinter.testWrite(function(res){
  64. console.log(res);
  65. that.msglist.push(res);
  66. });
  67. this.msglist.push(result);
  68. },
  69. }
  70. }
  71. </script>
  72. <style>
  73. </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

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。