android - 用安卓打印

标签 android printing wifi

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

最佳答案

这是一个例子

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 - 用安卓打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12582101/

相关文章:

java - 单击按钮后单个 ValueEventListener 未获取值

android - SipDemo 还是 Sipdroid?

android - 同时激活 WiFi 和蜂窝网络接口(interface)

javascript - Android 如何检测双击?

android - FragmentManager - IllegalStateException - 无主机

css - wkhtmltopdf 生成的 PDF 页面大小比预期小 25%

ASP.NET 打印 css 没有打印对话框和成功打印的模式确认

Python:从字符串中打印特定字符

ios - iOS 8.3 现在支持 Wifi 调试了吗?

wifi断开后,Android socket仍然可以正常写入