蓝牙打印机设置文本格式 [英] Format text in bluetooth printer
问题描述
其实,我要实现一个模块它连接到BT termical打印机和打印它。我有一个简单但功能上的例子,它的工作原理与打印机。现在的问题是,这些文字刻印以纯文本的,我必须给它的格式,大胆,更改字体大小和其他人......我该怎么办呢?。我不知道我怎么...使用这个类:
/ *
*版权所有(C)2009年的Android开源项目
*
* Apache许可证下授权,版本2.0(以下简称许可证);
*您可能不能使用这个文件除了在遵守许可。
*您可以在获得许可证的副本
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*许可证下发布分布在原样的基础上,
*无担保或任何形式的条件,无论是EX preSS或暗示的保证。
*请参阅许可的特定语言的管理权限和
*许可下限制。
* /
包com.edec.aptr;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.util.UUID中;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.bluetooth.BluetoothServerSocket;
进口android.bluetooth.BluetoothSocket;
进口android.content.Context;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.Message;
进口android.util.Log;
/ **
*该类完成所有的设置和管理蓝牙工作
*与其他设备连接。它有一个线程监听传入
*连接,一个线程用于与一个装置连接,和一个线程
*当连接执行数据传输。
* /
公共类BluetoothService {
// 调试
私有静态最后字符串变量=BluetoothService;
私有静态最终布尔D =真;
//创建服务器套接字时的SDP记录名称
私有静态最后字符串名称=BTPrinter;
//唯一的UUID这个应用程序
私有静态最后UUID MY_UUID = UUID
.fromString(00001101-0000-1000-8000-00805F9B34FB); //通过改变
//重庆
//金瓯
//会员领域
私人最终BluetoothAdapter mAdapter;
私人最终处理程序mHandler;
私人AcceptThread mAcceptThread;
私人ConnectThread mConnectThread;
私人ConnectedThread mConnectedThread;
私人诠释mState;
//常数,用于指示当前连接状态
公共静态最终诠释STATE_NONE = 0; //我们做什么
公共静态最终诠释STATE_LISTEN = 1; //现在监听传入
//连接
公共静态最终诠释STATE_CONNECTING = 2; //现在开始传出
// 连接
公共静态最终诠释STATE_CONNECTED = 3; //现在连接到远程
// 设备
/ **
*构造函数。 prepares新BTPrinter会话。
*
* @参数方面
*用户界面活动上下文
* @参数处理程序
*一个处理程序回邮件发送到用户界面活动
* /
公共BluetoothService(上下文的背景下,处理程序处理){
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler =处理程序;
}
/ **
*设置连接的当前状态
*
* @参数状态
*定义当前连接状态的整数
* /
私人同步无效的setState(INT状态){
如果(D)的
Log.d(TAG的setState()+ mState + - >中+的状态);
mState =状态;
//给这个新状态的处理程序,以便用户界面的活动可以更新
mHandler.obtainMessage(amarre.MESSAGE_STATE_CHANGE,州,-1)
.sendToTarget();
}
/ **
*返回当前连接状态。
* /
公共同步INT的getState(){
返回mState;
}
/ **
*启动该服务。具体开始AcceptThread在开始一个会话
*听(服务器)模式。通过活动onResume()调用
* /
市民同步无效的start(){
如果(D)的
Log.d(TAG,开始);
//取消任何线程尝试建立连接
如果(mConnectThread!= NULL){
mConnectThread.cancel();
mConnectThread = NULL;
}
//取消任何线程当前正在运行的连接
如果(mConnectedThread!= NULL){
mConnectedThread.cancel();
mConnectedThread = NULL;
}
//开始,以一个BluetoothServerSocket听线程
如果(mAcceptThread == NULL){
mAcceptThread =新AcceptThread();
mAcceptThread.start();
}
的setState(STATE_LISTEN);
}
/ **
*启动ConnectThread发起到远程设备的连接。
*
* @参数设备
*本BluetoothDevice类连接
* /
市民同步无效连接(BluetoothDevice类设备){
如果(D)的
Log.d(TAG,连接到+设备);
//取消任何线程尝试建立连接
如果(mState == STATE_CONNECTING){
如果(mConnectThread!= NULL){
mConnectThread.cancel();
mConnectThread = NULL;
}
}
//取消任何线程当前正在运行的连接
如果(mConnectedThread!= NULL){
mConnectedThread.cancel();
mConnectedThread = NULL;
}
//启动线程与给定的设备连接
mConnectThread =新ConnectThread(设备);
mConnectThread.start();
的setState(STATE_CONNECTING);
}
/ **
*启动ConnectedThread开始管理蓝牙连接
*
* @参数插座
*在其上连接制成的的BluetoothSocket
* @参数设备
*已连接的BluetoothDevice类
* /
市民同步无效连接(的BluetoothSocket插座,
BluetoothDevice类设备){
如果(D)的
Log.d(TAG,连接);
//取消已完成的连接线
如果(mConnectThread!= NULL){
mConnectThread.cancel();
mConnectThread = NULL;
}
//取消任何线程当前正在运行的连接
如果(mConnectedThread!= NULL){
mConnectedThread.cancel();
mConnectedThread = NULL;
}
//取消接受线程,因为我们只需要连接到一个
// 设备
如果(mAcceptThread!= NULL){
mAcceptThread.cancel();
mAcceptThread = NULL;
}
//启动线程来管理连接并进行传输
mConnectedThread =新ConnectedThread(插座);
mConnectedThread.start();
//发送连接设备的名称回UI活动
消息味精= mHandler.obtainMessage(amarre.MESSAGE_DEVICE_NAME);
束束=新包();
bundle.putString(amarre.DEVICE_NAME,device.getName());
msg.setData(包);
mHandler.sendMessage(MSG);
的setState(STATE_CONNECTED);
}
/ **
*停止所有线程
* /
市民同步无效停止(){
如果(D)的
Log.d(TAG,停止);
的setState(STATE_NONE);
如果(mConnectThread!= NULL){
mConnectThread.cancel();
mConnectThread = NULL;
}
如果(mConnectedThread!= NULL){
mConnectedThread.cancel();
mConnectedThread = NULL;
}
如果(mAcceptThread!= NULL){
mAcceptThread.cancel();
mAcceptThread = NULL;
}
}
/ **
*写的ConnectedThread在非同步的方式
*
* @Param出
*将要写入的字节
* @see ConnectedThread#写入(字节[])
* /
公共无效的write(byte []的输出){
//创建临时对象
ConnectedThreadř;
//该ConnectedThread副本同步
同步(本){
如果(mState!= STATE_CONNECTED)
返回;
R = mConnectedThread;
}
//执行写操作不同步
r.write(出);
}
/ **
*表明连接尝试失败,并通知用户界面活动。
* /
私人无效为connectionFailed(){
的setState(STATE_LISTEN);
//发送失败的消息回活动
消息味精= mHandler.obtainMessage(amarre.MESSAGE_TOAST);
束束=新包();
bundle.putString(
amarre.TOAST,
没有本质puede conectar人dispostivo,verifique阙ESTE SE encuentra encendidoŸcercano一拉片);
msg.setData(包);
mHandler.sendMessage(MSG);
}
/ **
*表明连接丢失,并通知用户界面活动。
* /
私人无效connectionLost(){
//的setState(STATE_LISTEN);
//发送失败的消息回活动
消息味精= mHandler.obtainMessage(amarre.MESSAGE_TOAST);
束束=新包();
bundle.putString(amarre.TOAST,
香格里拉体conexion CON EL dispositivo本身公顷尔迪);
msg.setData(包);
mHandler.sendMessage(MSG);
}
/ **
*此线程同时侦听传入连接运行。它的行为
*像一个服务器端客户机。它运行,直到连接被接受(或
*直到取消)。
* /
私有类AcceptThread继承Thread {
//本地服务器套接字
私人最终BluetoothServerSocket mmServerSocket;
公共AcceptThread(){
BluetoothServerSocket TMP = NULL;
//创建一个新的监听服务器套接字
尝试 {
TMP = mAdapter
.listenUsingRfcommWithServiceRecord(姓名,MY_UUID);
}赶上(IOException异常E){
Log.e(TAG,听()失败,E);
}
mmServerSocket = TMP;
}
公共无效的run(){
如果(D)的
Log.d(TAG,BEGIN mAcceptThread+本);
的setName(AcceptThread);
的BluetoothSocket插座= NULL;
//监听到服务器套接字,如果我们没有连接
而(mState!= STATE_CONNECTED){
尝试 {
//这是一个阻塞调用,只会返回一个
//连接成功或异常
插座= mmServerSocket.accept();
}赶上(IOException异常E){
Log.e(TAG,接受()失败,E);
打破;
}
//如果连接被接受
如果(插座!= NULL){
同步(BluetoothService.this){
开关(mState){
案例STATE_LISTEN:
案例STATE_CONNECTING:
//情况正常。启动连接线。
连接(插座,socket.getRemoteDevice());
打破;
案例STATE_NONE:
案例STATE_CONNECTED:
//要么没有准备或已连接。终止
//新的插座。
尝试 {
socket.close();
}赶上(IOException异常E){
Log.e(TAG,无法关闭不必要的插座,E);
}
打破;
}
}
}
}
如果(D)的
Log.i(TAG,END mAcceptThread);
}
公共无效取消(){
如果(D)的
Log.d(TAG,取消+本);
尝试 {
mmServerSocket.close();
}赶上(IOException异常E){
Log.e(TAG,关闭()服务器失败,E);
}
}
}
/ **
*此线程同时尝试与一个传出连接运行
* 设备。它运行直通;连接成功或
*失败。
* /
私有类ConnectThread继承Thread {
私人最终的BluetoothSocket mmSocket;
私人最终BluetoothDevice类mmDevice;
公共ConnectThread(BluetoothDevice类设备){
mmDevice =设备;
的BluetoothSocket TMP = NULL;
//获取的BluetoothSocket用于与连接
//给BluetoothDevice类
尝试 {
TMP = device.createRfcommSocketToServiceRecord(MY_UUID);
}赶上(IOException异常E){
Log.e(TAG,创建()失败,E);
}
mmSocket = TMP;
}
公共无效的run(){
Log.i(TAG,BEGIN mConnectThread);
的setName(ConnectThread);
//随时取消的发现,因为它会减慢连接
mAdapter.cancelDiscovery();
//使到的BluetoothSocket连接
尝试 {
//这是一个阻塞调用,只会返回一个
//连接成功或异常
mmSocket.connect();
}赶上(IOException异常E){
连接失败();
//关闭套接字
尝试 {
mmSocket.close();
}赶上(IOException异常E2){
Log.e(TAG,
无法关闭()连接失败时插座,
E2);
}
//启动服务在重新启动监听模式
BluetoothService.this.start();
返回;
}
//重置ConnectThread,因为我们做的
同步(BluetoothService.this){
mConnectThread = NULL;
}
//启动连接线
连接(mmSocket,mmDevice);
}
公共无效取消(){
尝试 {
mmSocket.close();
}赶上(IOException异常E){
Log.e(TAG,关闭()连接套接字失败的,E);
}
}
}
/ **
*此螺纹与远程设备的连接过程中运行。它处理所有
*传入和传出传输。
* /
私有类ConnectedThread继承Thread {
私人最终的BluetoothSocket mmSocket;
私人最终的InputStream mmInStream;
私人最终的OutputStream mmOutStream;
公共ConnectedThread(的BluetoothSocket插座){
Log.d(TAG,创造ConnectedThread);
mmSocket =插座;
InputStream的tmpIn = NULL;
OutputStream的tmpOut = NULL;
//获取的BluetoothSocket输入和输出流
尝试 {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
}赶上(IOException异常E){
Log.e(TAG,而不是创建临时套接字,E);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
公共无效的run(){
Log.i(TAG,BEGIN mConnectedThread);
诠释字节;
//继续听InputStream的同时连接
而(真){
尝试 {
byte []的缓冲区=新的字节[256];
//读取从InputStream
字节= mmInStream.read(缓冲区);
如果(字节大于0){
//发送所获得的字节到UI活动
mHandler.obtainMessage(amarre.MESSAGE_READ,字节,-1,
缓冲液).sendToTarget();
} 其他 {
Log.e(TAG,断开连接);
失去连接();
//添加重庆金瓯
如果(mState!= STATE_NONE){
Log.e(TAG,断开连接);
//启动服务在重新启动监听模式
BluetoothService.this.start();
}
打破;
}
}赶上(IOException异常E){
Log.e(TAG,断开,E);
失去连接();
//添加重庆金瓯
如果(mState!= STATE_NONE){
//启动服务在重新启动监听模式
BluetoothService.this.start();
}
打破;
}
}
}
/ **
*写入到所连接的OutStream。
*
* @参数缓冲区
*将要写入的字节
* /
公共无效的write(byte []的缓冲区){
尝试 {
mmOutStream.write(缓冲液);
//分享发送消息给UI活动
mHandler.obtainMessage(amarre.MESSAGE_WRITE,-1,-1,缓冲液)
.sendToTarget();
}赶上(IOException异常E){
Log.e(TAG,在写例外,E);
}
}
公共无效取消(){
尝试 {
mmSocket.close();
}赶上(IOException异常E){
Log.e(TAG,关闭()连接套接字失败的,E);
}
}
}
}
我从另一个类发送数据,因此:
byte []的发送;
尝试 {
发送= message.getBytes(GB2312);
}赶上(UnsupportedEncodingException E){
发送= message.getBytes();
}
mService.write(发送);
消息为字符串变种。然后,我怎么可以格式化文本??,或者至少,改变字体大小...
现在我知道如何做到这一点,我不得不运用反向ingeneering,并从市场反编译的.apk文件,使用dex2jar在Linux和明年开放罐的Java反编译器......请尝试......当你写这个命令:的
out.write(str.getBytes(),0,str.getBytes()的长度。);
U的发送到方法的byte []数组,U可以修改的格式发送另一byte []数组之前发出真正的byte []数组...
默认格式byte []数组是这样的:
字节[] arrayOfByte1 = {27,33,0};
,这样你们可以试试这个:
字节[]格式= {27,33,0};
out.write(格式);
out.write(str.getBytes(),0,str.getBytes()的长度。);
这些行我会打印ü默认格式的文本,但如果ü做到这一点:
字节[]格式= {27,33,0};
格式[2] =((字节)(0x8中| arrayOfByte1 [2]));
out.write(格式);
out.write(str.getBytes(),0,str.getBytes()的长度。);
这将在大胆的风格打印文本...... U可以试试这个oter格式数组:
//粗体
格式[2] =((字节)(0x8中| arrayOfByte1 [2]));
// 高度
格式[2] =((字节)(为0x10 | arrayOfByte1 [2]));
// 宽度
格式[2] =((字节)(0x20的| arrayOfByte1 [2]));
// 强调
格式[2] =((字节)(0x80的| arrayOfByte1 [2]));
// 小
格式[2] =((字节)(为0x1 | arrayOfByte1 [2]));
太ü可以结合起来,那么如果u像小和大胆的文字,取消这些阵列asignements,例如:
字节[]格式= {27,33,0};
//粗体
格式[2] =((字节)(0x8中| arrayOfByte1 [2]));
// 高度
格式[2] =((字节)(为0x10 | arrayOfByte1 [2]));
// 宽度
格式[2] =((字节)(0x20的| arrayOfByte1 [2]));
// 强调
//格式[2] =((字节)(0x80的| arrayOfByte1 [2]));
// 小
//格式[2] =((字节)(为0x1 | arrayOfByte1 [2]));
out.write(格式);
out.write(str.getBytes(),0,str.getBytes()的长度。);
这最后code打印阙最大的文本大小......我希望这能帮助ü...
actually, I have to implement a module the which connect to a BT termical printer and print in it. I have a simple but functionally example, it works with the printer. The problem is that these text are printed in plain text, and i have to give it format, bold, change the font size and others... How can i do it??. I don't know how... I use this Class:
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.edec.aptr;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
* This class does all the work for setting up and managing Bluetooth
* connections with other devices. It has a thread that listens for incoming
* connections, a thread for connecting with a device, and a thread for
* performing data transmissions when connected.
*/
public class BluetoothService {
// Debugging
private static final String TAG = "BluetoothService";
private static final boolean D = true;
// Name for the SDP record when creating server socket
private static final String NAME = "BTPrinter";
// Unique UUID for this application
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"); // change by
// chongqing
// jinou
// Member fields
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private int mState;
// Constants that indicate the current connection state
public static final int STATE_NONE = 0; // we're doing nothing
public static final int STATE_LISTEN = 1; // now listening for incoming
// connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing
// connection
public static final int STATE_CONNECTED = 3; // now connected to a remote
// device
/**
* Constructor. Prepares a new BTPrinter session.
*
* @param context
* The UI Activity Context
* @param handler
* A Handler to send messages back to the UI Activity
*/
public BluetoothService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
/**
* Set the current state of the connection
*
* @param state
* An integer defining the current connection state
*/
private synchronized void setState(int state) {
if (D)
Log.d(TAG, "setState() " + mState + " -> " + state);
mState = state;
// Give the new state to the Handler so the UI Activity can update
mHandler.obtainMessage(amarre.MESSAGE_STATE_CHANGE, state, -1)
.sendToTarget();
}
/**
* Return the current connection state.
*/
public synchronized int getState() {
return mState;
}
/**
* Start the service. Specifically start AcceptThread to begin a session in
* listening (server) mode. Called by the Activity onResume()
*/
public synchronized void start() {
if (D)
Log.d(TAG, "start");
// Cancel any thread attempting to make a connection
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to listen on a BluetoothServerSocket
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
setState(STATE_LISTEN);
}
/**
* Start the ConnectThread to initiate a connection to a remote device.
*
* @param device
* The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
if (D)
Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection
*
* @param socket
* The BluetoothSocket on which the connection was made
* @param device
* The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket,
BluetoothDevice device) {
if (D)
Log.d(TAG, "connected");
// Cancel the thread that completed the connection
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Cancel the accept thread because we only want to connect to one
// device
if (mAcceptThread != null) {
mAcceptThread.cancel();
mAcceptThread = null;
}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(amarre.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(amarre.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
/**
* Stop all threads
*/
public synchronized void stop() {
if (D)
Log.d(TAG, "stop");
setState(STATE_NONE);
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mAcceptThread != null) {
mAcceptThread.cancel();
mAcceptThread = null;
}
}
/**
* Write to the ConnectedThread in an unsynchronized manner
*
* @param out
* The bytes to write
* @see ConnectedThread#write(byte[])
*/
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED)
return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
/**
* Indicate that the connection attempt failed and notify the UI Activity.
*/
private void connectionFailed() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(amarre.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(
amarre.TOAST,
"No se puede conectar al dispostivo, verifique que éste se encuentra encendido y cercano a la tablet");
msg.setData(bundle);
mHandler.sendMessage(msg);
}
/**
* Indicate that the connection was lost and notify the UI Activity.
*/
private void connectionLost() {
// setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(amarre.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(amarre.TOAST,
"La conexión con el dispositivo se ha perdido");
msg.setData(bundle);
mHandler.sendMessage(msg);
}
/**
* This thread runs while listening for incoming connections. It behaves
* like a server-side client. It runs until a connection is accepted (or
* until cancelled).
*/
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter
.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D)
Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate
// new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D)
Log.i(TAG, "END mAcceptThread");
}
public void cancel() {
if (D)
Log.d(TAG, "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}
}
/**
* This thread runs while attempting to make an outgoing connection with a
* device. It runs straight through; the connection either succeeds or
* fails.
*/
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG,
"unable to close() socket during connection failure",
e2);
}
// Start the service over to restart listening mode
BluetoothService.this.start();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
/**
* This thread runs during a connection with a remote device. It handles all
* incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
byte[] buffer = new byte[256];
// Read from the InputStream
bytes = mmInStream.read(buffer);
if (bytes > 0) {
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(amarre.MESSAGE_READ, bytes, -1,
buffer).sendToTarget();
} else {
Log.e(TAG, "disconnected");
connectionLost();
// add by chongqing jinou
if (mState != STATE_NONE) {
Log.e(TAG, "disconnected");
// Start the service over to restart listening mode
BluetoothService.this.start();
}
break;
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// add by chongqing jinou
if (mState != STATE_NONE) {
// Start the service over to restart listening mode
BluetoothService.this.start();
}
break;
}
}
}
/**
* Write to the connected OutStream.
*
* @param buffer
* The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(amarre.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
}
I send the data from another class, so:
byte[] send;
try {
send = message.getBytes("GB2312");
} catch (UnsupportedEncodingException e) {
send = message.getBytes();
}
mService.write(send);
message is a String var.. Then, how can I format the text??, or at least, change font size...
Now I know how to do it, i had to apply reverse ingeneering and decompile an .apk from the market, using dex2jar on linux and next opening jar with java decompiler... Try this... when u write this command:
out.write(str.getBytes(),0,str.getBytes().length);
U are sending to the method an byte[] array, u can modify format sending another byte[] array before send the real byte[] array...
The default format byte[] array is this:
byte[] arrayOfByte1 = { 27, 33, 0 };
So u can try this:
byte[] format = { 27, 33, 0 };
out.write(format);
out.write(str.getBytes(),0,str.getBytes().length);
These lines i'll print u the default format text, but if u do it:
byte[] format = { 27, 33, 0 };
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
out.write(format);
out.write(str.getBytes(),0,str.getBytes().length);
It will print text in bold style... U can try this oter format arrays:
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
// Height
format[2] = ((byte)(0x10 | arrayOfByte1[2]));
// Width
format[2] = ((byte) (0x20 | arrayOfByte1[2]));
// Underline
format[2] = ((byte)(0x80 | arrayOfByte1[2]));
// Small
format[2] = ((byte)(0x1 | arrayOfByte1[2]));
Too u can combine it, then if u like little and bold text, uncomment these array asignements, for example:
byte[] format = { 27, 33, 0 };
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
// Height
format[2] = ((byte)(0x10 | arrayOfByte1[2]));
// Width
format[2] = ((byte) (0x20 | arrayOfByte1[2]));
// Underline
// format[2] = ((byte)(0x80 | arrayOfByte1[2]));
// Small
// format[2] = ((byte)(0x1 | arrayOfByte1[2]));
out.write(format);
out.write(str.getBytes(),0,str.getBytes().length);
This last code prints que biggest text size... I hope this can helps u...
这篇关于蓝牙打印机设置文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!