Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用爱普生蓝牙打印机打印粗体字母_Android_Printing_Bluetooth_Epson - Fatal编程技术网

在Android中使用爱普生蓝牙打印机打印粗体字母

在Android中使用爱普生蓝牙打印机打印粗体字母,android,printing,bluetooth,epson,Android,Printing,Bluetooth,Epson,这是我的全部工作代码 public class NewActivity extends Activity{ // will show the statuses like bluetooth open, close or data sent TextView myLabel; // will enable user to enter any text to be printed EditText myTextbox; // android built in classes for bluet

这是我的全部工作代码

public class NewActivity extends Activity{

// will show the statuses like bluetooth open, close or data sent
TextView myLabel;

// will enable user to enter any text to be printed
EditText myTextbox;

// android built in classes for bluetooth operations
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;

// needed for communication to bluetooth device / network
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;

byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_main);

    try {
        // more codes will be here
    }catch(Exception e) {
        e.printStackTrace();
    }
 // we are going to have three buttons for specific functions
    Button openButton = (Button) findViewById(R.id.open);
    Button sendButton = (Button) findViewById(R.id.send);
    Button closeButton = (Button) findViewById(R.id.close);

    // text label and input box
    myLabel = (TextView) findViewById(R.id.label);
    myTextbox = (EditText) findViewById(R.id.entry);

 // open bluetooth connection
    openButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                findBT();
                openBT();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

 // send data typed by the user to be printed
    sendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                sendData();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

 // close bluetooth connection
    closeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                closeBT();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
}

// close the connection to bluetooth printer.
void closeBT() throws IOException {
    try {
        stopWorker = true;
        mmOutputStream.close();
        mmInputStream.close();
        mmSocket.close();
        myLabel.setText("Bluetooth Closed");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
// this will send text data to be printed by the bluetooth printer
void sendData() throws IOException {
    try {

        Spanned  msg = Html.fromHtml("<h1>My First Example</h1> <p>My first Print.</p> <p><b>This is a paragraph.</b></p>");
        myTextbox.setText(msg, TextView.BufferType.SPANNABLE);

        byte[] arrayOfByte1 = { 27, 33, 0 };
        byte[] format = { 27, 33, 0 };

     // Bold
     format[2] = ((byte)(0x8 | arrayOfByte1[2]));

        mmOutputStream.write(format);
        mmOutputStream.write(msg.toString().getBytes(),0,msg.toString().getBytes().length);

        // tell the user data were sent
        myLabel.setText("Data sent.");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

// this will find a bluetooth printer device
void findBT() {

    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if(mBluetoothAdapter == null) {
            myLabel.setText("No bluetooth adapter available");
        }

        if(!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        if(pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {

                // RPP300 is the name of the bluetooth printer device
                // we got this name from the list of paired devices
                if (device.getName().equals("TM-P20_002154")) {
                    mmDevice = device;
                    break;
                }
            }
        }

        myLabel.setText("Bluetooth device found.");

    }catch(Exception e){
        e.printStackTrace();
    }
}

// tries to open a connection to the bluetooth printer device
void openBT() throws IOException {
    try {

        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
         // UUID uuid = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");            
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        mmOutputStream = mmSocket.getOutputStream();
        mmInputStream = mmSocket.getInputStream();

        beginListenForData();

        myLabel.setText("Bluetooth Opened");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

/*
 * after opening a connection to bluetooth printer device,
 * we have to listen and check if a data were sent to be printed.
 */
void beginListenForData() {
    try {
        final Handler handler = new Handler();

        // this is the ASCII code for a newline character
        final byte delimiter = 10;

        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];

        workerThread = new Thread(new Runnable() {
            public void run() {

                while (!Thread.currentThread().isInterrupted() && !stopWorker) {

                    try {

                        int bytesAvailable = mmInputStream.available();

                        if (bytesAvailable > 0) {

                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);

                            for (int i = 0; i < bytesAvailable; i++) {

                                byte b = packetBytes[i];
                                if (b == delimiter) {

                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(
                                        readBuffer, 0,
                                        encodedBytes, 0,
                                        encodedBytes.length
                                    );

                                    // specify US-ASCII encoding
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;

                                    // tell the user data were sent to bluetooth printer device
                                    handler.post(new Runnable() {
                                        public void run() {
                                            myLabel.setText(data);
                                        }
                                    });

                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }

                    } catch (IOException ex) {
                        stopWorker = true;
                    }

                }
            }
        });

        workerThread.start();

    } catch (Exception e) {
        e.printStackTrace();
     }
    }
  }
公共类NewActivity扩展活动{
//将显示蓝牙打开、关闭或数据发送等状态
文本视图myLabel;
//将允许用户输入要打印的任何文本
编辑文本myTextbox;
//android内置蓝牙操作类
蓝牙适配器mBluetoothAdapter;
蓝牙插座;
蓝牙设备;
//需要与蓝牙设备/网络进行通信
输出流mmOutputStream;
输入流mmInputStream;
螺纹加工螺纹;
字节[]读取缓冲区;
int-readBufferPosition;
易变布尔工;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_main);
试一试{
//更多的代码将在这里
}捕获(例外e){
e、 printStackTrace();
}
//我们将有三个按钮用于特定功能
按钮打开按钮=(按钮)findViewById(R.id.open);
按钮sendButton=(按钮)findViewById(R.id.send);
按钮关闭按钮=(按钮)findViewById(R.id.close);
//文本标签和输入框
myLabel=(TextView)findViewById(R.id.label);
myTextbox=(EditText)findViewById(R.id.entry);
//打开蓝牙连接
openButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
findBT();
openBT();
}捕获(IOEX异常){
例如printStackTrace();
}
}
});
//发送用户键入的要打印的数据
sendButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
sendData();
}捕获(IOEX异常){
例如printStackTrace();
}
}
});
//关闭蓝牙连接
closeButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
closeBT();
}捕获(IOEX异常){
例如printStackTrace();
}
}
});
}
//关闭与蓝牙打印机的连接。
void closeBT()引发IOException{
试一试{
stopWorker=true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
myLabel.setText(“蓝牙关闭”);
}捕获(例外e){
e、 printStackTrace();
}
}
//这将发送要由蓝牙打印机打印的文本数据
void sendData()引发IOException{
试一试{
span msg=Html.fromHtml(“我的第一个示例我的第一次打印。

这是一个段落。

”; myTextbox.setText(msg,TextView.BufferType.SPANNABLE); 字节[]arrayOfByte1={27,33,0}; 字节[]格式={27,33,0}; //大胆的 格式[2]=(字节)(0x8 | arrayOfByte1[2]); mmOutputStream.write(格式); mmOutputStream.write(msg.toString().getBytes(),0,msg.toString().getBytes().length); //告诉用户数据已发送 setText(“已发送的数据”); }捕获(例外e){ e、 printStackTrace(); } } //这将找到蓝牙打印机设备 void findBT(){ 试一试{ mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter==null){ myLabel.setText(“没有可用的蓝牙适配器”); } 如果(!mBluetoothAdapter.isEnabled()){ 意图启用Bluetooth=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE); startActivityForResult(启用蓝牙,0); } 设置pairedDevices=mBluetoothAdapter.getBondedDevices(); 如果(pairedDevices.size()>0){ 用于(蓝牙设备:pairedDevices){ //RPP300是蓝牙打印机设备的名称 //我们从配对设备列表中获取此名称 if(device.getName()等于(“TM-P20_002154”)){ mmDevice=设备; 打破 } } } setText(“找到蓝牙设备”); }捕获(例外e){ e、 printStackTrace(); } } //尝试打开与蓝牙打印机设备的连接 void openBT()引发IOException{ 试一试{ //标准SerialPortService ID UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805f9b34fb”); //UUID UUID=UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66”); mmSocket=mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream=mmSocket.getOutputStream(); mmInputStream=mmSocket.getInputStream(); beginListenForData(); myLabel.setText(“蓝牙打开”); }捕获(例外e){ e、 printStackTrace(); } } /* *打开与蓝牙打印机设备的连接后, *我们必须倾听并检查是否发送了数据进行打印。 */ void beginListenForData(){ 试一试{ 最终处理程序=新处理程序(); //这是换行符的ASCII码 最后一个字节分隔符=10; stopWorker=false; readBufferPosition=0; readBuffer=新字节[1024]; workerThread=新线程(new Runnable()){ 公开募捐{ 而(!Thread.currentThread().isInterrupted()&&!stopWorker){ 试一试{ int bytesavable=mmInputStream.available(); 如果(字节可用>0){ byte[]packetBytes=新字节[bytesAvailable]; mmInputStream.read(packetBytes); for(int i=0;i<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="10dp" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/label" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/entry" > <Button android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:text="Open" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> <Button android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Close" /> </LinearLayout>