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

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

因为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" />
相关文章
|
开发工具 Android开发
uniapp调用芯烨xprinter打印机安卓sdk
uniapp调用芯烨xprinter打印机安卓sdk
180 0
|
小程序 JavaScript API
支付宝小程序集成mqtt兼容IOS和安卓
支付宝小程序集成mqtt兼容IOS和安卓
211 0
|
API 开发工具 iOS开发
一点就通,社交源码IOS客户端开发集成SDK
所谓SDK,全称是SoftwaredevelopmentKit,翻译成软件开发工具包。SDK用助开发某种软件,今天给大家简单讲解下如何在社交源码IOS客户端上开发集成 SDK。
|
小程序 Android开发 iOS开发
微信小程序连接蓝牙工具类支持IOS和安卓
微信小程序连接蓝牙工具类支持IOS和安卓
238 0
|
开发工具 Android开发
Appium 移动端自动化 - Android SDK的安装与配置,使用安卓SDK连接手机实例演示
Appium 移动端自动化 - Android SDK的安装与配置,使用安卓SDK连接手机实例演示
184 0
|
Web App开发 移动开发 BI
如何使用谷歌浏览器远程调试安卓/ios真机H5应用?
公司今年开始着手移动办公,将部分原来需要在PC端操作的功能逐渐增加到了移动端,开发了很多的H5报表。因为是内部用的功能,在测试的时候也没有可以去关注兼容性和一些app端的专项测试,我们只是负责测试自己部门开发的轻应用,接入到公司的平台中能不能正常使用。
如何使用谷歌浏览器远程调试安卓/ios真机H5应用?
|
开发工具 iOS开发
产品百科 | 如何在 iOS 模拟器上安装阿里云短视频 SDK
短视频 SDK 在 IOS 模拟器上安装和使用的方法
产品百科 | 如何在 iOS 模拟器上安装阿里云短视频 SDK
|
Web App开发 开发工具 Android开发