【问题标题】:Android Bluetooth Printing安卓蓝牙打印
【发布时间】:2012-12-16 04:31:48
【问题描述】:

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

任何人都可以解决它吗???或者有没有其他方法可以摆脱这种打印??

【问题讨论】:

  • 我想将简单的文本从我的 android 设备发送到支持蓝牙的热敏打印机。但我对如何将数据发送到蓝牙打印机有点困惑,
  • @NiravBhandari :我遇到了类似的问题。我的应用程序几乎做同样的事情。如果我能与您取得联系以寻求帮助,您会非常高兴。谢谢。 :)

标签: android printing bluetooth


【解决方案1】:

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

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

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

【讨论】:

  • 太好了,请不要将我的回答标记为已接受。标记你的答案。因为你自己解决了它
  • 你在 AndroidManifest.xml 中使用了哪些权限?
【解决方案2】:

您可以为任何打印机使用 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)

【讨论】:

    【解决方案3】:

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

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

    只需调用此方法并传递其中的字符串即可打印。

    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;
    }
    

    【讨论】:

    • 感谢您的回答...实际上我无法连接到打印机。我已经按照提到的 android 开发者网站尝试过...你能帮我如何将我的 android 设备连接到打印机吗?
    • 请回答我在这里提出的问题,以便我可以清楚地帮助您。您是否搜索过使用蓝牙的设备?你得到蓝牙设备的列表吗?你有没有和这两个蓝牙设备配对??
    • 我已经用搜索蓝牙的代码编辑了问题,并将数据发送到蓝牙,如你所说...请查看它
    • 我得到了解决方案......我已经回答了......感谢您的支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    相关资源
    最近更新 更多