Android蓝牙打印

我正在编写一个将数据发送到蓝牙打印机的应用程序.谁能帮我 ?如何使用 Android蓝牙堆栈进行打印?或者是否有任何外部api或sdk使用?

这是我搜索蓝牙的代码…

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));

private final BroadcastReceiver ActionFoundReceiver = 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);
            btArrayAdapter.add(device.getName() + "\n"
                    + device.getAddress());
            btArrayAdapter.notifyDataSetChanged();
        }
    }
};

这是我的数据发送到打印机的代码..

BluetoothDevice mDevice = bluetoothAdapter.getRemoteDevice("00:15:FF:F2:56:A4");
Method m = mDevice.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
mBTsocket = (BluetoothSocket) m.invoke(mDevice,1);
System.out.println("Connecting.....");
mBTsocket.connect();
System.out.println("Connected");
OutputStream os = mBTsocket.getOutputStream();
os.flush();
os.write(Receipt.getBytes());
// mBTsocket.close();

当我写socket.close()时,数据没有打印到打印机,因为套接字连接在打印数据之前关闭..如果我没有写socket.close()那么数据只打印一次..我不会能够第二次打印数据,直到我重新启动手机的蓝牙.

任何人都可以解决它?还是有其他方法可以摆脱这种印刷?

解决方法

我得到了解决问题的方法……

如果我想打印数据超过一次,那么你不需要与设备创建新的套接字连接…而是调用outputstream.write(bytes)方法.

最后如果要断开设备,请调用mBTScoket.close()方法断开设备.

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...