安卓蓝牙USB连接热敏打印机一


安卓蓝牙连接打印机

       首先,你的热敏打印机必须支持蓝牙,我选择的是芯华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 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咆哮的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值