安卓蓝牙USB连接热敏打印机一
2015-12-30 14:00
519 查看
安卓蓝牙连接打印机
首先,你的热敏打印机必须支持蓝牙,我选择的是芯华xp-58iih这款。此款支持蓝牙连接和USB连接。
由于我不想每次打印的时候去搜索蓝牙设备,所以我提前使用平板配备上了这款蓝牙打印机。
这样,我就可以在程序中直接找到这个蓝牙设备,并打印数据。
第一,实例化一个蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
第二,找到蓝牙设备地址
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice bluetoothDevice : bondedDevices) { deviceAddress = bluetoothDevice.getAddress(); }
第三,获取设备,连接,获取流
device = bluetoothAdapter.getRemoteDevice(deviceAddress);
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
第四,传入数据,进行打印
byte[] printData = data.getBytes("gbk");
outputStream.write(printData, 0, printData.length);
outputStream.flush();
完毕后,记得关闭
bluetoothSocket.close();
outputStream.close();
测试结果如下:
当然,我们还需要在AndroidManifest.xml中分配权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
废话不多说,详细的代码请下载:http://download.csdn.net/detail/konkon2012/9383696
首先,你的热敏打印机必须支持蓝牙,我选择的是芯华xp-58iih这款。此款支持蓝牙连接和USB连接。
由于我不想每次打印的时候去搜索蓝牙设备,所以我提前使用平板配备上了这款蓝牙打印机。
这样,我就可以在程序中直接找到这个蓝牙设备,并打印数据。
第一,实例化一个蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
第二,找到蓝牙设备地址
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice bluetoothDevice : bondedDevices) { deviceAddress = bluetoothDevice.getAddress(); }
第三,获取设备,连接,获取流
device = bluetoothAdapter.getRemoteDevice(deviceAddress);
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
第四,传入数据,进行打印
byte[] printData = data.getBytes("gbk");
outputStream.write(printData, 0, printData.length);
outputStream.flush();
完毕后,记得关闭
bluetoothSocket.close();
outputStream.close();
测试结果如下:
当然,我们还需要在AndroidManifest.xml中分配权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
废话不多说,详细的代码请下载:http://download.csdn.net/detail/konkon2012/9383696
相关文章推荐
- Java笔记12:Java对象排序
- android-Managing Audio Playback
- addImageAsync异步加载资源和进度条ProgressTimer的实现
- Raid3.0数据丢失怎么办
- 【Java】断言assert及Eclipse、Tomcat 开启断言方式
- FIELD SYMBOL和TYPE REF TO的用法和比较
- R语言中paste()中粘贴向量
- C语言struct内存占用问题
- java.lang.IllegalArgumentException: MALFORMED jar解析中文报错问题
- Grails 3.0连接Mysql数据库
- Java的接口与抽象类
- CMake 包含了头文件 找到了so动态库 链接时仍然出现undefined reference to 错误
- [LeetCode]题解(python):086-Partition List
- Git CMD - clone: Clone a repository into a new directory
- iOS UITableView 便捷编写
- docker 简介
- JavaScript阻止回车提交表单的方法
- iOS视图控制对象生命周期的区别
- 【Android 开发】Android App 内存泄露之Handler
- 在一个apk中调用另外一个apk中的activity