由于之前很多系统都是使用Unity来进行开发,上头老顶突然说要用商米V1打单。商米V1买回来不能直接使用,需要在跟机器的渠道商而不是商米沟通获取调试权限。(这麻痹的商业模式)。商米官网上提供了两种开发Demo,http://docs.sunmi.com/V1doc.php,有ADLL调用、Android SDK和蓝牙三只模式调用。一开始想试试用蓝牙通信打印的方便兼用其它品牌打印机不需要重新设计打印样式。可惜官方没有提供Demo无法入手,而且论坛上也没有回复只能放弃使用Android SDK。
至于Unity Android插件如何创建可以参考我另外一篇文章。
先上Java代码:
package com.example.androidplugins;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
//震动的包
import android.os.Vibrator;
import android.app.Service;
//发送短信库
import android.telephony.SmsManager;
import com.google.zxing.BarcodeFormat;
import com.sunmi.controller.ICallback;
import com.sunmi.impl.V1Printer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
private V1Printer printer;
private ICallback callback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
printer = new V1Printer(this);
callback = new ICallback(){
@Override
public void onRunResult(boolean isSuccess) {
}
@Override
public void onReturnString(String result) {
}
@Override
public void onRaiseException(int code, String msg) {
}
};
printer.setCallback(callback);
}
/***
*打印byte[]
*/
public void PrintTest1(){
printer.setAlignment(1);
printer.sendRAWData(getErlmoData());
printer.commitTransaction();
}
public void PrintTest2(){
String[] text = new String[4];
int[] width = new int[] { 10, 6, 6, 8 };
int[] align = new int[] { 0, 2, 2, 2 }; // 左齐,右齐,右齐,右齐
text[0] = "名稱";
text[1] = "數量";
text[2] = "單價";
text[3] = "金額";
printer.printColumnsText(text, width, new int[] { 1, 2, 2, 2 });
text[0] = "草莓酸奶A布甸";
text[1] = "4";
text[2] = "12.00";
text[3] = "48.00";
printer.printColumnsText(text, width, align);
text[0] = "酸奶B水果夹心面包";
text[1] = "10";
text[2] = "4.00";
text[3] = "40.00";
printer.printColumnsText(text, width, align);
text[0] = "酸奶水果布甸香橙软桃蛋糕"; // 文字超长,换行
text[1] = "100";
text[2] = "16.00";
text[3] = "1600.00";
printer.printColumnsText(text, width, align);
text[0] = "酸奶水果夹心面包";
text[1] = "10";
text[2] = "4.00";
text[3] = "40.00";
printer.printColumnsText(text, width, align);
printer.setFontSize(30);
printer.setAlignment(0);
printer.commitTransaction();
}
/**
* 开启事物
*/
public void beginTransaction(){
printer.beginTransaction();
}
/**
* 提交事物(开始打印)
*/
public void commitTransaction(){
printer.commitTransaction();
}
/**
* 打印文字
*/
public void printText(String text){
printer.printText(text);
}
/**
* 打印空行
* @param lines
*/
public void lineWrap(int lines){
printer.lineWrap(lines);
}
/***
* 打印一维码
* @param data
*/
public void printBarCode(String data){
printer.printBarCode(data, BarcodeFormat.CODE_128, 400, 100);
}
/***
* 打印二维码
* @param date
* @param size 大小 240
*/
public void printQRCode(String date,int size){
printer.printQRCode(date, size);
}
/***
* 设置字体
* @param size
*/
public void setFontSize(int size){
printer.setFontSize(size);
}
/***
* 对齐方式
* @param alignment 0左对齐
*/
public void setAlignment(int alignment){
printer.setAlignment(alignment);
}
/*** 暂时无效
* 表格打印
* @param text 内容
* @param width 没列宽度 int[] width = new int[] { 10, 6, 6, 8 };
* @param align 对齐方式 int[] align = new int[] { 0, 2, 2, 2 }; // 左齐,右齐,右齐,右齐
*/
public void printColumnsText(String[] text,int[] width,int[] align){
printer.printColumnsText(text, width, align);
}
/***
* 暂时无效
* @param rv
*/
public void sendRAWData(byte[] rv){
printer.sendRAWData(rv);
}
//饿了么小票
private byte[] getErlmoData(){
byte[] rv = new byte[]{
0x1b,0x61,0x00,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x20,0x1d,0x21,0x11,0x23,0x31,0x1d,0x21,0x00,0x00,(byte)0xb6,(byte)0xf6
,(byte)0xc1,(byte)0xcb,(byte)0xc3,(byte)0xb4,(byte)0xcd,(byte)(byte)0xe2,(byte)0xc2,(byte)0xf4,(byte)0xb5,(byte)0xa5,0x20,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,0x0a,0x0a,0x1b,0x61
,0x01,(byte)0xbf,(byte)0xa8,(byte)0xc8,(byte)0xf8,(byte)0xc5,(byte)0xfb,(byte)0xc8,(byte)0xf8,0x0a,0x0a,0x1b,0x61,0x00,0x1b,0x61,0x01,0x1d,0x21,0x11,0x2d,0x2d
,(byte)0xd2,(byte)0xd1,(byte)0xd6,(byte)0xa7,(byte)0xb8,(byte)0xb6,0x2d,0x2d,0x1d,0x21,0x00,0x00,0x0a,0x0a,0x1b,0x61,0x00,0x1b,0x61,0x01,0x1d,0x21
,0x11,(byte)0xd4,(byte)0xa4,(byte)0xbc,(byte)0xc6,0x31,0x39,0x3a,0x30,0x30,(byte)0xcb,(byte)0xcd,(byte)0xb4,(byte)(byte)0xef,0x1d,0x21,0x00,0x00,0x0a,0x0a,0x1b,0x61
,0x00,0x5b,(byte)0xcf,(byte)0xc2,(byte)0xb5,(byte)0xa5,(byte)0xca,(byte)0xb1,(byte)0xbc,(byte)(byte)0xe4,0x5d,0x32,0x30,0x31,0x34,0x2d,0x31,0x32,0x2d,0x30,0x33,0x20
,0x31,0x36,0x3a,0x32,0x31,0x0a,0x5b,(byte)0xb1,(byte)0xb8,(byte)0xd7,(byte)0xa2,0x5d,0x1d,0x21,0x01,(byte)0xb2,(byte)0xbb,(byte)0xb3,(byte)0xd4,(byte)0xc0,(byte)0xb1,0x20
,(byte)0xc0,(byte)0xb1,(byte)0xd2,(byte)0xbb,(byte)0xb5,(byte)(byte)0xe3,0x20,(byte)0xb6,(byte)(byte)0xe0,(byte)0xbc,(byte)0xd3,(byte)0xc3,(byte)0xd7,0x20,(byte)0xc3,(byte)0xbb,(byte)0xc1,(byte)(byte)0xe3,(byte)0xc7,(byte)0xae,0x1d,0x21
,0x00,0x0a,0x5b,(byte)0xb7,(byte)0xa2,(byte)0xc6,(byte)0xb1,0x5d,(byte)0xd5,(byte)(byte)0xe2,(byte)0xca,(byte)0xc7,(byte)0xd2,(byte)0xbb,(byte)0xb8,(byte)0xf6,(byte)0xb7,(byte)0xa2,(byte)0xc6,(byte)0xb1,(byte)0xcc,(byte)0xa7
,(byte)0xcd,(byte)0xb7,0x0a,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d
,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,(byte)0xb2,(byte)0xcb,(byte)0xc3,(byte)0xfb,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,(byte)0xca,(byte)0xfd,(byte)0xc1,(byte)0xbf,0x20,0x20,(byte)0xd0,(byte)0xa1
,(byte)0xbc,(byte)0xc6,0x0a,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,0x31,(byte)0xba,(byte)0xc5,(byte)0xc0,(byte)0xba,(byte)0xd7,(byte)0xd3
,0x20,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,(byte)0xc3,(byte)0xc0
,(byte)0xca,(byte)0xb3,(byte)0xd2,(byte)0xbb,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21
,0x01,0x20,0x78,0x34,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x34
,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,(byte)0xc3,(byte)0xc0,(byte)0xca,(byte)0xb3,(byte)0xb6,(byte)0xfe,0x1d,0x21,0x00,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x36,0x1d,0x21,0x00,0x1d,0x21
,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x36,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2
,(byte)0xca,(byte)0xd4,(byte)0xc3,(byte)0xc0,(byte)0xca,(byte)0xb3,(byte)0xc8,(byte)0xfd,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x32,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20
,0x1d,0x21,0x01,0x32,0x1d,0x21,0x00,0x0a,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,0x32,(byte)0xba
,(byte)0xc5,(byte)0xc0,(byte)0xba,(byte)0xd7,(byte)0xd3,0x20,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x1d,0x21,0x01,(byte)0xb2
,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,0x31,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x31,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20
,0x20,0x1d,0x21,0x01,0x31,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,0x32,0x1d,0x21,0x00,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x31
,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x31,0x1d,0x21,0x00,0x0a
,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,0x33,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x31,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00
,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x32,0x33,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,0x28,0x2b,0x29,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca
,(byte)0xd4,(byte)0xd1,(byte)0xf3,(byte)0xc6,(byte)0xf8,(byte)0xa4,(byte)0xce,(byte)0xce,(byte)0xf7,(byte)0xca,(byte)0xbd,(byte)0xcc,(byte)0xf0,(byte)0xb5,(byte)(byte)0xe3,0x1d,0x21,0x00,0x20,0x20,0x1d,0x21
,0x01,0x20,0x78,0x31,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x31
,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,0x28,0x2b,0x29,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,(byte)0xcb,(byte)(byte)0xe1,(byte)0xc0,(byte)0xb1,(byte)0xc4,(byte)0xbe,(byte)0xb6,(byte)0xfa
,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x31,0x1d,0x21,0x00,0x1d,0x21
,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x38,0x1d,0x21,0x00,0x0a,0x2d,0x2d,0x2d,0x2d,0x2d
,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,0x33,(byte)0xba,(byte)0xc5,(byte)0xc0,(byte)0xba,(byte)0xd7,(byte)0xd3,0x20,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d
,0x2d,0x2d,0x2d,0x2d,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,(byte)0xb2,(byte)0xcb,(byte)0xc6,(byte)0xb7,(byte)0xc3,(byte)0xfb,(byte)0xd7,(byte)0xd6,(byte)0xba,(byte)0xdc
,(byte)0xb3,(byte)0xa4,(byte)0xba,(byte)0xdc,(byte)0xb3,(byte)0xa4,(byte)0xba,(byte)0xdc,(byte)0xb3,(byte)0xa4,(byte)0xba,(byte)0xdc,(byte)0xb3,(byte)0xa4,(byte)0xba,(byte)0xdc,(byte)0xb3,(byte)0xa4,0x1d,0x21,0x00,0x0a
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x1d,0x21,0x01,0x20,0x78,0x31,0x1d,0x21,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x1d,0x21,0x01
,0x33,0x30,0x30,0x1d,0x21,0x00,0x0a,0x1d,0x21,0x01,(byte)0xb2,(byte)(byte)0xe2,(byte)0xca,(byte)0xd4,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x20,0x78,0x31,0x1d,0x21
,0x00,0x1d,0x21,0x01,0x1d,0x21,0x00,0x20,0x20,0x20,0x20,0x20,0x1d,0x21,0x01,0x31,0x1d,0x21,0x00,0x0a,0x2d,0x2d
,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,(byte)0xc6,(byte)(byte)0xe4,(byte)0xcb,(byte)0xfb,(byte)0xb7,(byte)0xd1,(byte)0xd3,(byte)0xc3,0x20,0x2d,0x2d,0x2d
,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x1d,0x21,0x01,(byte)0xc5,(byte)(byte)0xe4,(byte)0xcb,(byte)0xcd,(byte)0xb7,(byte)0xd1,0x1d,0x21,0x00,0x20
,0x20,0x20,0x20,0x20
};
return rv;
}
}
生成Jar包放入Unity中。
Unity 调用案例:
using UnityEngine;
using System.Collections;
public class Script : MonoBehaviour {
private string FontSize = "";
private string PrintBarCode = "";
private string QrCode = "";
private string PrintText = "";
private AndroidJavaObject ajc;
public PrintStypeTick _printStypeTick;
private int alignment = 0;
void Start ()
{
AndroidJavaClass jc=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
ajc = jc.GetStatic<AndroidJavaObject>("currentActivity");
}
void OnGUI()
{
//字体大小
GUI.Label(new Rect(40, 100, 100, 50), "FontSize");
FontSize = GUI.TextField (new Rect (140, 100, 160, 50), FontSize);
if (GUI.Button(new Rect(400, 100, 100, 50), "字体大小"))
{
ajc.Call("beginTransaction");
ajc.Call("setFontSize", int.Parse(FontSize));
ajc.Call("printText", "吃屎啦");
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
GUI.Label(new Rect(40, 200, 100, 50), "一维码");
PrintBarCode = GUI.TextArea (new Rect (140, 200, 160, 50), PrintBarCode);
if (GUI.Button(new Rect(400, 200, 100, 50), "一维码"))
{
ajc.Call("beginTransaction");
ajc.Call("printBarCode", PrintBarCode);
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
GUI.Label(new Rect(40, 300, 100, 50), "二维码");
QrCode = GUI.TextArea(new Rect(140, 300, 160, 50), QrCode);
if (GUI.Button(new Rect(400, 300, 100, 50), "二维码"))
{
ajc.Call("beginTransaction");
ajc.Call("printQRCode", QrCode, 300);
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
GUI.Label(new Rect(40, 400, 100, 50), "打印文字");
PrintText = GUI.TextArea(new Rect(140, 400, 160, 80), PrintText);
if (GUI.Button(new Rect(400, 400, 100, 50), "打印文字"))
{
ajc.Call("beginTransaction");
ajc.Call("printText", "吃屎啦");
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
if (GUI.Button(new Rect(400, 500, 100, 50), "切换对齐方式"))
{
ajc.Call("beginTransaction");
ajc.Call("setAlignment", alignment);
ajc.Call("printText", "吃屎啦");
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
alignment++;
if (alignment == 3)
alignment = 0;
}
if (GUI.Button(new Rect(400, 600, 100, 50), "表格打印"))
{
/*
ajc.Call("beginTransaction");
string[] text = new string[4];
text[0] = "名称";
text[1] = "数量";
text[2] = "单价";
text[4] = "总价";
int[] width = new int[] { 10, 6, 6, 8 };
int[] align = new int[] { 0, 2, 2, 2 }; // 左齐,右齐,右齐,右齐
ajc.Call("printColumnsText", text, width, align);
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
*/
ajc.Call("PrintTest2");
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
if (GUI.Button(new Rect(400, 700, 100, 50), "byte[]"))
{
/*
ajc.Call("beginTransaction");
ajc.Call("sendRAWData", _printStypeTick.GetByte());
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
*/
ajc.Call("PrintTest1");
ajc.Call("lineWrap", 5);
ajc.Call("commitTransaction");
}
}
}