下载相关 资源文件 ,在项目中新建如下层级的文件夹,将源文件中的AIDL文件放入其中。
ICallback:打印服务执行结果的回调
ITax:打印服务执行结果的回调
ILcdCallback:顾显反馈结果
根据需求选择aidl文件,IWoyouService是必须的。一般开钱箱使用IWoyouService和ICallback文件即可。
引入AIDL文件
1.找到需要导入工程的aidl文件.通过文本打开,找到文件中的具体包名
package woyou.aidlservice.jiuiv5;
2.依次创建跟之前找到的包名一样的文件夹,我这里是:woyou、aidlservice、jiuiv5。
把对应的aidl文件拷贝到创建的最后一层文件夹里
3.如下文件,依次修改属性。右键属性→生成操作→修改为AndroidInterfaceDescription
修改完后重新生成,生成成功后在obj/Debug文件下找到aidl文件说明引入AIDL文件成功
调用
创建一个公共类(减少重复代码),也可以不创建直接写在需要调用的地方
usingWoyou.Aidlservice.Jiuiv5;public classServiceConnection : Java.Lang.Object, IServiceConnection
{public IWoyouService woyouService { get; private set; }public voidOnServiceConnected(ComponentName name, IBinder service)
{
woyouService=IWoyouServiceStub.AsInterface(service);
}public voidOnServiceDisconnected(ComponentName name)
{
woyouService= null;
}//public void connectPrinterService(Context con)//{//Context context = con;//Intent intent = new Intent();//ServiceConnection conn = new ServiceConnection();//intent.SetPackage("woyou.aidlservice.jiuiv5");//intent.SetAction("woyou.aidlservice.jiuiv5.IWoyouService");//context.StartService(intent);//context.BindService(intent, conn, Bind.AutoCreate);//}
}
调用,全局方法
privateServiceConnection conn;//连接服务
public voidconnectPrinterService()
{
Context context= this.ApplicationContext;
Intent intent= newIntent();
conn= newServiceConnection();
intent.SetPackage("woyou.aidlservice.jiuiv5");
intent.SetAction("woyou.aidlservice.jiuiv5.IWoyouService");
context.StartService(intent);
context.BindService(intent, conn, Bind.AutoCreate);
}
private void BtnPosOpenBox_Click(objectsender, EventArgs e)
{//通过ES/POS指令的方式开钱箱
byte[] aa = new byte[5];
aa[0] = 0x10;
aa[1] = 0x14;
aa[2] = 0x00;
aa[3] = 0x00;
aa[4] = 0x00;try{if (conn.woyouService == null)
{
connectPrinterService();
}if (conn.woyouService != null)
{//打开钱箱,也可以直接使用conn.woyouService.OpenDrawer(null)方法开钱箱
conn.woyouService.SendRAWData(aa, null);return;
}
Message.Show(MessageText.MSG_OPEN_ERROR_PLEASE_RETRY,this);
}catch(RemoteException e1)
{
e1.PrintStackTrace();
}
}
开钱箱
注意LCD屏幕和第二屏是不一样的,是两种不同的东西
private void Btn_Click(objectsender, EventArgs e)
{try{if (woyouService == null)
{
connectPrinterService();
}if (woyouService != null)
{//1 初始化 2 唤醒LCD 3休眠LCD 4清屏
woyouService.SendLCDCommand(1);//设置状态
woyouService.SendLCDCommand(2);
woyouService.SendLCDString("小杨!", null);//设置显示内容//conn.woyouService.SendLCDDoubleString("金额:", "800", null) ;
return;
}
Message.Show("打开失败,请重试!", this);
}catch(RemoteException e1)
{
e1.PrintStackTrace();
}
}
显示LCD屏幕