与Android打印 [英] Printing with android

查看:207
本文介绍了与Android打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从通过蓝牙或WiFi在我的Andr​​oid应用程序8打印文本文件。请给我建议的解决方案。

I want to print the text file from my application in android 8 through Bluetooth or WiFi. Please suggest me the solution.

推荐答案

这是一个示例

package com.example.untitled2;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

public class MyActivity extends Activity {
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
                    Toast.makeText(MyActivity.this, "no device", Toast.LENGTH_LONG).show();
                }
                if (!mBluetoothAdapter.isEnabled()) {
                    mBluetoothAdapter.enable();
                }
                Set<BluetoothDevice> bluetoothDevices = mBluetoothAdapter.getBondedDevices();
                if (bluetoothDevices.size() == 0)
                    return;
                OutputStream mmOutStream;
                BluetoothDevice device = bluetoothDevices.iterator().next();
                try {
                    BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID);
                    socket.connect();
                    mmOutStream = socket.getOutputStream();
                    /*String textPrint = ""+(char) 27 + (char)116 + (char) 27;*/
                    String textPrint = "this is example text"+(char)10;
                    mmOutStream.flush();
                    mmOutStream.write(textPrint.getBytes());
                    mmOutStream.flush();
                    for (int i = 0; i < 10; i++) {
                        mmOutStream.write(textPrint.getBytes());
                    }
                    mmOutStream.flush();
                    //mmOutStream.wait();
                    mmOutStream.close();
                    socket.close();
                } catch (IOException e) {
                    Toast.makeText(MyActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}

这篇关于与Android打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆