您的位置:首页 > 其它

安卓蓝牙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 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: