Android手机通过蓝牙连接佳博打印机的实例代码

 更新时间:2016年11月07日 17:22:23   作者:yun1  
这篇文章主要介绍了Android手机通过蓝牙连接佳博打印机的实例代码,非常不错具有参考借鉴价值,需要的朋友可以参考下

GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
如果你想靠AI翻身,你先需要一个靠谱的工具!

打印机驱动安装助手是一款绿色、小巧、便携的系统辅助工具,帮助您有效解决打印机驱动安装的问题!!点击下载使用

所使用的打印机为佳博打印机,支持蓝牙、wifi、usb我所使用的是通过蓝牙来连接。

在网上找到一个佳博官方针对安卓开发的App源码,但是各种的跳转,没有看太懂,所以又去问度娘,找到了一个不错的文章

Android对于蓝牙开发从2.0版本的sdk才开始支持,而且模拟器不支持,测试至少需要两部手机,所以制约了很多技术人员的开发。

1. 首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限

1
2
3
4
// 管理蓝牙设备的权限
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
// 使用蓝牙设备的权限
<uses-permissionandroid:name="android.permission.BLUETOOTH" />

2.打开蓝牙

获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 检查设备是否支持蓝牙
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null)
{
// 设备不支持蓝牙
}
// 打开蓝牙
if (!adapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// 设置蓝牙可见性,最多300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
context.startActivity(intent);
}

3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice)

首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。

1
2
3
4
5
6
7
8
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for(int i=0; i<devices.size(); i++)
{
BluetoothDevice device =
BluetoothDevice)devices.iterator().next();
System.out.println(device.getName());
}

4.搜索周围的蓝牙设备

适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 设置广播信息过滤
IntentFilter intentFilter = new IntentFilter();
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();
自定义广播类
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
}
}
}

5.蓝牙设备的配对和状态监视

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 获取查找到的蓝牙设备
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
// 如果查找到的设备符合要连接的设备,处理
if (device.getName().equalsIgnoreCase(name)) {
// 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
adapter.cancelDiscovery();
// 获取蓝牙设备的连接状态
connectState = device.getBondState();
switch (connectState) {
// 未配对
case BluetoothDevice.BOND_NONE:
// 配对
try {
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
break;
// 已配对
case BluetoothDevice.BOND_BONDED:
try {
// 连接
connect(device);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
// 状态改变的广播
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getName().equalsIgnoreCase(name)) {
connectState = device.getBondState();
switch (connectState) {
case BluetoothDevice.BOND_NONE:
break;
case BluetoothDevice.BOND_BONDING:
break;
case BluetoothDevice.BOND_BONDED:
try {
// 连接
connect(device);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
}
}

6.蓝牙设备的连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void connect(BluetoothDevice device) throws IOException {
// 固定的UUID
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
}
private void connect(BluetoothDevice device) throws IOException {
// 固定的UUID
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
}

1.BluetoothAdapter 顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它

BluetoothAdapter里的方法很多,常用的有以下几个:

cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索

disable()关闭蓝牙

enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:

Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()获取本地蓝牙地址

getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter

getName()获取本地蓝牙名称

getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备

getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)

isDiscovering()判断当前是否正在查找设备,是返回true

isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,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两个包导入,之后的连接方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
*连接
*/
private voidconnect() {
intrel =0;
try{//使用端口1,4代表模式为蓝牙模式,蓝牙地址,最后默认为0
rel = mGpService.openPort(1,4,adressData.get(loction),0);
}catch(RemoteException e) {
e.printStackTrace();
}
GpCom.ERROR_CODE r = 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进行绑定;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buletooth);
connection();
}
private voidconnection() {
conn=newPrinterServiceConnection();
Intent intent =newIntent(this, GpPrintService.class);
this.bindService(intent,conn, Context.BIND_AUTO_CREATE);// bindService
}
classPrinterServiceConnectionimplementsServiceConnection {
@Override
public void onServiceDisconnected(ComponentName name) {
mGpService=null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mGpService= GpService.Stub.asInterface(service);
}
}

还要在清单文件中去配置服务

1
2
3
4
5
6
7
8
9
10
11
<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>

做完这些之后就大功告成,可以连接到打印机了,不过还是不能打印,还要进行一些打印的配置,由于我的项目的原因,连接和打印并没有在同一个界面,所以继续去研究佳博源码,发现如果前面链接已经成功的话,在另一个界面只需就行一个配置就可一直接打印打印也代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
public classPrintActivityextendsBaseActivityimplementsView.OnClickListener{
@Bind(R.id.print_print)
Buttonprint;
privateGpServicemGpService=null;
privatePrinterServiceConnectionconn=null;
private static final int MAIN_QUERY_PRINTER_STATUS=0xfe;
private static final int REQUEST_PRINT_LABEL=0xfd;
private static final int REQUEST_PRINT_RECEIPT=0xfc;
private int mTotalCopies=0;
@Override
protected void initHandler() {
handler=newHandler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
}
privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = 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);
String str;
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();
}else if(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,"query printer status error", Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
protected void initTitle() {
mTitleTextMiddle.setText("打印");
}
private void connection() {
conn=newPrinterServiceConnection();
Intent intent =newIntent(this, GpPrintService.class);
this.bindService(intent,conn, Context.BIND_AUTO_CREATE);// bindService
}
classPrinterServiceConnectionimplementsServiceConnection {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("ServiceConnection","onServiceDisconnected() called");
mGpService=null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mGpService= GpService.Stub.asInterface(service);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
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_MODE mode)
*,在打印完成后会接收到,action为GpCom.ACTION_LABEL_RESPONSE的广播,特别用于连续打印,
*可参照该sample中的sendLabelWithResponse方法与广播中的处理
**/
registerReceiver(mBroadcastReceiver,newIntentFilter(GpCom.ACTION_LABEL_RESPONSE));
}
@Override
protected void initView() {
print.setOnClickListener(this);
}
@Override
public void onClick(View v) {
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,"Printer is not receipt mode", Toast.LENGTH_SHORT).show();
}
}catch(RemoteException e1) {
e1.printStackTrace();
}
}
break;
}
}
private void sendReceipt() {
EscCommand esc =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("Welcome to use SMARNET printer!\n"); //打印文字
// /*打印繁体中文需要打印机支持繁体字库 */
// String message = "佳博智匯票據打印機\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("Print bitmap!\n"); //打印文字
// Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.gprinter);
// esc.addRastBitImage(b, 384, 0); //打印图片
// /*打印一维条码 */
// esc.addText("Print code128\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);
String sss = Base64.encodeToString(bytes, Base64.DEFAULT);
intrs;
try{
rs =mGpService.sendEscCommand(1, sss);
GpCom.ERROR_CODE r = GpCom.ERROR_CODE.values()[rs];
if(r != GpCom.ERROR_CODE.SUCCESS) {
Toast.makeText(getApplicationContext(), GpCom.getErrorText(r), Toast.LENGTH_SHORT).show();
}
}catch(RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}

至此大功告成,打印出东西之后感觉好爽

以上所述是小编给大家介绍的Android手机通过蓝牙连接佳博打印机的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

打印机驱动安装助手是一款绿色、小巧、便携的系统辅助工具,帮助您有效解决打印机驱动安装的问题!!点击下载使用

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

原文链接:http://blog.csdn.net/u011685953/article/details/53067325

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!

相关文章

最新评论