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

举报
清雨小竹 发表于 2022/09/14 10:37:50 2022/09/14
【摘要】 因为xprinter的安卓sdk需要在安卓项目里配置service,但是uniapp里云打包无法配置service,所以只能用原生插件离线打包的形式开发。https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/androidUniPlugin-Hello-AS安卓项目引入 PosPrinterSDK.jar1.andr...

因为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" />
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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