Android蓝牙打印

时间:2013-01-01 14:39:06

标签: android printing bluetooth

我正在编写一个将数据发送到蓝牙打印机的应用程序。谁能帮我 ?如何使用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(),那么数据只打印一次..在我重新启动手机蓝牙之前,我无法再次打印数据。

任何人都有解决方案吗???还是有其他办法摆脱这种印刷?

3 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:0)

您可以对任何打印机使用printooth库,printooth简单且文档齐全, https://github.com/mazenrashed/Printooth

var printables = ArrayList<Printable>()
var printable = Printable.PrintableBuilder()  
    .setText("Hello World") //The text you want to print
    .setAlignment(DefaultPrinter.ALLIGMENT_CENTER)
    .setEmphasizedMode(DefaultPrinter.EMPHASISED_MODE_BOLD) //Bold or normal  
    .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
    .setUnderlined(DefaultPrinter.UNDELINED_MODE_ON) // Underline on/off
    .setCharacterCode(DefaultPrinter.CHARACTER_CODE_USA_CP437) // Character code to support languages
    .setLineSpacing(DefaultPrinter.LINE_SPACING_60)
    .setNewLinesAfter(1) // To provide n lines after sentence
    .build()
printables.add(printable)
BluetoothPrinter.printer().print(printables)

答案 2 :(得分:-1)

如果您已连接设备并将其配对。

因此,对于打印,打印机需要字节。所以我创造了一个方法。

只需调用此方法并在其中传递String即可打印。

<强> String str = new String("This is the text sending to the printer");

private void printData() {
    // TODO Auto-generated method stub

    String newline = "\n";
    try {
        out.write(str.getBytes(),0,str.getBytes().length);
        Log.i("Log", "One line printed");
    } catch (IOException e) {
        Toast.makeText(BluetoothDemo.this, "catch 1", Toast.LENGTH_LONG).show();
        e.printStackTrace();
        Log.i("Log", "unable to write ");
        flagCheck = false;
    }
    try {
        out.write(newline.getBytes(),0,newline.getBytes().length);
    } catch (IOException e) {        
        Log.i("Log", "Unable to write the new line::");
        e.printStackTrace();
        flagCheck = false;
    }
    flagCheck = true;
}