Android手机通过蓝牙连接佳博打印机的实例代码
所使用的打印机为佳博打印机,支持蓝牙、wifi、usb我所使用的是通过蓝牙来连接。
在网上找到一个佳博官方针对安卓开发的App源码,但是各种的跳转,没有看太懂,所以又去问度娘,找到了一个不错的文章
Android对于蓝牙开发从2.0版本的sdk才开始支持,而且模拟器不支持,测试至少需要两部手机,所以制约了很多技术人员的开发。
1.首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限
//管理蓝牙设备的权限 <uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/> //使用蓝牙设备的权限 <uses-permissionandroid:name="android.permission.BLUETOOTH"/>
2.打开蓝牙
获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。
//检查设备是否支持蓝牙
adapter=BluetoothAdapter.getDefaultAdapter();
if(adapter==null)
{
//设备不支持蓝牙
}
//打开蓝牙
if(!adapter.isEnabled())
{
Intentintent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//设置蓝牙可见性,最多300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
context.startActivity(intent);
}
3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice)
首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。
BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice>devices=adapter.getBondedDevices();
for(inti=0;i<devices.size();i++)
{
BluetoothDevicedevice=
BluetoothDevice)devices.iterator().next();
System.out.println(device.getName());
}
4.搜索周围的蓝牙设备
适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。
//设置广播信息过滤
IntentFilterintentFilter=newIntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//注册广播接收器,接收并处理搜索结果
context.registerReceiver(receiver,intentFilter);
//寻找蓝牙设备,android会将查找到的设备以广播形式发出去
adapter.startDiscovery();
自定义广播类
privateBroadcastReceiverreceiver=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
}
}
}
5.蓝牙设备的配对和状态监视
privateBroadcastReceiverreceiver=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//获取查找到的蓝牙设备
BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
//如果查找到的设备符合要连接的设备,处理
if(device.getName().equalsIgnoreCase(name)){
//搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
adapter.cancelDiscovery();
//获取蓝牙设备的连接状态
connectState=device.getBondState();
switch(connectState){
//未配对
caseBluetoothDevice.BOND_NONE:
//配对
try{
MethodcreateBondMethod=BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
}catch(Exceptione){
e.printStackTrace();
}
break;
//已配对
caseBluetoothDevice.BOND_BONDED:
try{
//连接
connect(device);
}catch(IOExceptione){
e.printStackTrace();
}
break;
}
}
}elseif(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
//状态改变的广播
BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getName().equalsIgnoreCase(name)){
connectState=device.getBondState();
switch(connectState){
caseBluetoothDevice.BOND_NONE:
break;
caseBluetoothDevice.BOND_BONDING:
break;
caseBluetoothDevice.BOND_BONDED:
try{
//连接
connect(device);
}catch(IOExceptione){
e.printStackTrace();
}
break;
}
}
}
}
}
6.蓝牙设备的连接
privatevoidconnect(BluetoothDevicedevice)throwsIOException{
//固定的UUID
finalStringSPP_UUID="00001101-0000-1000-8000-00805F9B34FB";
UUIDuuid=UUID.fromString(SPP_UUID);
BluetoothSocketsocket=device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
}
privatevoidconnect(BluetoothDevicedevice)throwsIOException{
//固定的UUID
finalStringSPP_UUID="00001101-0000-1000-8000-00805F9B34FB";
UUIDuuid=UUID.fromString(SPP_UUID);
BluetoothSocketsocket=device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
}
1.BluetoothAdapter顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它
BluetoothAdapter里的方法很多,常用的有以下几个:
cancelDiscovery()根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索
disable()关闭蓝牙
enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:
Intemtenabler=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);
getAddress()获取本地蓝牙地址
getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter
getName()获取本地蓝牙名称
getRemoteDevice(Stringaddress)根据蓝牙地址获取远程蓝牙设备
getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)
isDiscovering()判断当前是否正在查找设备,是返回true
isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false
listenUsingRfcommWithServiceRecord(Stringname,UUIDuuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步
startDiscovery()开始搜索,这是搜索的第一步
2.BluetoothDevice看名字就知道,这个类描述了一个蓝牙设备
createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket
这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket
这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter
3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,
这个类一种只有三个方法
两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!
还有一点需要注意的是,这两个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接
close()这个就不用说了吧,翻译一下——关闭!
4.BluetoothSocket,跟BluetoothServerSocket相对,是客户端
一共5个方法,不出意外,都会用到
close(),关闭
connect()连接
getInptuStream()获取输入流
getOutputStream()获取输出流
getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备
根据这篇文章只是更改了最后的蓝牙连接部分(因为佳博打印机有自己的SDK,SDK中有自己的连接方法)将下载的DEMO中的gprintersdkv22.jar和xUtils-2.6.14.jar两个包导入,之后的连接方法如下:
/**
*连接
*/
privatevoidconnect(){
intrel=0;
try{//使用端口1,4代表模式为蓝牙模式,蓝牙地址,最后默认为0
rel=mGpService.openPort(1,4,adressData.get(loction),0);
}catch(RemoteExceptione){
e.printStackTrace();
}
GpCom.ERROR_CODEr=GpCom.ERROR_CODE.values()[rel];
if(r!=GpCom.ERROR_CODE.SUCCESS){
if(r==GpCom.ERROR_CODE.DEVICE_ALREADY_OPEN){
//开启成功
}else{
UIUtils.showMessage(GpCom.getErrorText(r));
}
}else{
ProgressDialogUtil.dismiss(BuleToothActivity.this);
UIUtils.showMessage("失败");
}
}
不要忘记的就是mGpService是佳博SDK的一个自己的服务,要先获取,并与Activity进行绑定;
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buletooth);
connection();
}
privatevoidconnection(){
conn=newPrinterServiceConnection();
Intentintent=newIntent(this,GpPrintService.class);
this.bindService(intent,conn,Context.BIND_AUTO_CREATE);//bindService
}
classPrinterServiceConnectionimplementsServiceConnection{
@Override
publicvoidonServiceDisconnected(ComponentNamename){
mGpService=null;
}
@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
mGpService=GpService.Stub.asInterface(service);
}
}
还要在清单文件中去配置服务
<service android:name="com.gprinter.service.GpPrintService" android:enabled="true" android:exported="true" android:label="GpPrintService"> <intent-filter> <actionandroid:name="com.gprinter.aidl.GpPrintService"/> </intent-filter> </service> <serviceandroid:name="com.gprinter.service.AllService"> </service>
做完这些之后就大功告成,可以连接到打印机了,不过还是不能打印,还要进行一些打印的配置,由于我的项目的原因,连接和打印并没有在同一个界面,所以继续去研究佳博源码,发现如果前面链接已经成功的话,在另一个界面只需就行一个配置就可一直接打印打印也代码如下:
publicclassPrintActivityextendsBaseActivityimplementsView.OnClickListener{
@Bind(R.id.print_print)
Buttonprint;
privateGpServicemGpService=null;
privatePrinterServiceConnectionconn=null;
privatestaticfinalintMAIN_QUERY_PRINTER_STATUS=0xfe;
privatestaticfinalintREQUEST_PRINT_LABEL=0xfd;
privatestaticfinalintREQUEST_PRINT_RECEIPT=0xfc;
privateintmTotalCopies=0;
@Override
protectedvoidinitHandler(){
handler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
}
};
}
privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
Log.d("TAG",action);
//GpCom.ACTION_DEVICE_REAL_STATUS为广播的IntentFilter
if(action.equals(GpCom.ACTION_DEVICE_REAL_STATUS)){
//业务逻辑的请求码,对应哪里查询做什么操作
intrequestCode=intent.getIntExtra(GpCom.EXTRA_PRINTER_REQUEST_CODE,-1);
//判断请求码,是则进行业务操作
if(requestCode==MAIN_QUERY_PRINTER_STATUS){
intstatus=intent.getIntExtra(GpCom.EXTRA_PRINTER_REAL_STATUS,16);
Stringstr;
if(status==GpCom.STATE_NO_ERR){
str="打印机正常";
}else{
str="打印机";
if((byte)(status&GpCom.STATE_OFFLINE)>0){
str+="脱机";
}
if((byte)(status&GpCom.STATE_PAPER_ERR)>0){
str+="缺纸";
}
if((byte)(status&GpCom.STATE_COVER_OPEN)>0){
str+="打印机开盖";
}
if((byte)(status&GpCom.STATE_ERR_OCCURS)>0){
str+="打印机出错";
}
if((byte)(status&GpCom.STATE_TIMES_OUT)>0){
str+="查询超时";
}
}
Toast.makeText(getApplicationContext(),"打印机:"+1+"状态:"+str,Toast.LENGTH_SHORT)
.show();
}elseif(requestCode==REQUEST_PRINT_RECEIPT){
intstatus=intent.getIntExtra(GpCom.EXTRA_PRINTER_REAL_STATUS,16);
if(status==GpCom.STATE_NO_ERR){
sendReceipt();
}else{
Toast.makeText(PrintActivity.this,"queryprinterstatuserror",Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
protectedvoidinitTitle(){
mTitleTextMiddle.setText("打印");
}
privatevoidconnection(){
conn=newPrinterServiceConnection();
Intentintent=newIntent(this,GpPrintService.class);
this.bindService(intent,conn,Context.BIND_AUTO_CREATE);//bindService
}
classPrinterServiceConnectionimplementsServiceConnection{
@Override
publicvoidonServiceDisconnected(ComponentNamename){
Log.i("ServiceConnection","onServiceDisconnected()called");
mGpService=null;
}
@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
mGpService=GpService.Stub.asInterface(service);
}
}
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print);
connection();
//注册实时状态查询广播
registerReceiver(mBroadcastReceiver,newIntentFilter(GpCom.ACTION_DEVICE_REAL_STATUS));
/**
*票据模式下,可注册该广播,在需要打印内容的最后加入addQueryPrinterStatus(),在打印完成后会接收到
*action为GpCom.ACTION_DEVICE_STATUS的广播,特别用于连续打印,
*可参照该sample中的sendReceiptWithResponse方法与广播中的处理
**/
registerReceiver(mBroadcastReceiver,newIntentFilter(GpCom.ACTION_RECEIPT_RESPONSE));
/**
*标签模式下,可注册该广播,在需要打印内容的最后加入addQueryPrinterStatus(RESPONSE_MODEmode)
*,在打印完成后会接收到,action为GpCom.ACTION_LABEL_RESPONSE的广播,特别用于连续打印,
*可参照该sample中的sendLabelWithResponse方法与广播中的处理
**/
registerReceiver(mBroadcastReceiver,newIntentFilter(GpCom.ACTION_LABEL_RESPONSE));
}
@Override
protectedvoidinitView(){
print.setOnClickListener(this);
}
@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.print_print:
if(mGpService==null){
UIUtils.showMessage("服务正在开启");
}else{
try{
inttype=mGpService.getPrinterCommandType(1);
if(type==GpCom.ESC_COMMAND){
mGpService.queryPrinterStatus(1,1000,REQUEST_PRINT_RECEIPT);
}else{
Toast.makeText(this,"Printerisnotreceiptmode",Toast.LENGTH_SHORT).show();
}
}catch(RemoteExceptione1){
e1.printStackTrace();
}
}
break;
}
}
privatevoidsendReceipt(){
EscCommandesc=newEscCommand();
esc.addInitializePrinter();
esc.addPrintAndFeedLines((byte)3);
esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印居中
esc.addSelectPrintModes(EscCommand.FONT.FONTA,EscCommand.ENABLE.OFF,EscCommand.ENABLE.ON,EscCommand.ENABLE.ON,EscCommand.ENABLE.OFF);//设置为倍高倍宽
esc.addText("asdfkldsjgfsdl\n");//打印文字
esc.addPrintAndLineFeed();
/*打印文字*/
esc.addSelectPrintModes(EscCommand.FONT.FONTA,EscCommand.ENABLE.OFF,EscCommand.ENABLE.OFF,EscCommand.ENABLE.OFF,EscCommand.ENABLE.OFF);//取消倍高倍宽
esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);//设置打印左对齐
esc.addText("dfkdsgklfds\n");//打印文字
//esc.addText("WelcometouseSMARNETprinter!\n");//打印文字
///*打印繁体中文需要打印机支持繁体字库*/
//Stringmessage="佳博智匯票據打印機\n";
////esc.addText(message,"BIG5");
//esc.addText(message,"GB2312");
esc.addPrintAndLineFeed();
/*绝对位置具体详细信息请查看GP58编程手册*/
esc.addText("商品名称");
esc.addSetHorAndVerMotionUnits((byte)7,(byte)0);
esc.addSetAbsolutePrintPosition((short)6);
esc.addText("订单号");
esc.addSetAbsolutePrintPosition((short)10);
esc.addText("状态");
esc.addPrintAndLineFeed();
esc.addText("苹果");
esc.addSetHorAndVerMotionUnits((byte)7,(byte)0);
esc.addSetAbsolutePrintPosition((short)6);
esc.addText("12345");
esc.addSetAbsolutePrintPosition((short)10);
esc.addText("正常");
esc.addPrintAndLineFeed();
esc.addText("果粒橙300ml");
esc.addSetHorAndVerMotionUnits((byte)7,(byte)0);
esc.addSetAbsolutePrintPosition((short)6);
esc.addText("3545456");
esc.addSetAbsolutePrintPosition((short)10);
esc.addText("正常");
esc.addPrintAndLineFeed();
///*打印图片*/
//esc.addText("Printbitmap!\n");//打印文字
//Bitmapb=BitmapFactory.decodeResource(getResources(),R.drawable.gprinter);
//esc.addRastBitImage(b,384,0);//打印图片
///*打印一维条码*/
//esc.addText("Printcode128\n");//打印文字
//esc.addSelectPrintingPositionForHRICharacters(EscCommand.HRI_POSITION.BELOW);//
////设置条码可识别字符位置在条码下方
//esc.addSetBarcodeHeight((byte)60);//设置条码高度为60点
//esc.addSetBarcodeWidth((byte)1);//设置条码单元宽度为1
//esc.addCODE128(esc.genCodeB("SMARNET"));//打印Code128码
//esc.addPrintAndLineFeed();
/*
*QRCode命令打印此命令只在支持QRCode命令打印的机型才能使用。在不支持二维码指令打印的机型上,则需要发送二维条码图片
*/
esc.addText("商家二维码\n");//打印文字
esc.addSelectErrorCorrectionLevelForQRCode((byte)0x31);//设置纠错等级
esc.addSelectSizeOfModuleForQRCode((byte)6);//设置qrcode模块大小
esc.addStoreQRCodeData("dfgdgs");//设置qrcode内容
esc.addPrintQRCode();//打印QRCode
esc.addPrintAndLineFeed();
/*打印文字*/esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印左对齐
esc.addText("Completed!\r\n");//打印结束
//开钱箱
esc.addGeneratePlus(LabelCommand.FOOT.F5,(byte)255,(byte)255);
esc.addPrintAndFeedLines((byte)8);
Vector<Byte>datas=esc.getCommand();//发送数据
byte[]bytes=GpUtils.ByteTo_byte(datas);
Stringsss=Base64.encodeToString(bytes,Base64.DEFAULT);
intrs;
try{
rs=mGpService.sendEscCommand(1,sss);
GpCom.ERROR_CODEr=GpCom.ERROR_CODE.values()[rs];
if(r!=GpCom.ERROR_CODE.SUCCESS){
Toast.makeText(getApplicationContext(),GpCom.getErrorText(r),Toast.LENGTH_SHORT).show();
}
}catch(RemoteExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
至此大功告成,打印出东西之后感觉好爽
以上所述是小编给大家介绍的Android手机通过蓝牙连接佳博打印机的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!