Advertisement

Android 实现蓝牙打印的功能

阅读量:

必须先创建一个蓝牙打印工具类

import com.github.promeg.pinyinhelper.Pinyin;

import java.io.IOException;
import java.io OutputStream;
import java.io OutputStreamWriter;
import java.util.Calendar;

/**

蓝牙打印工具类
*/
public class PrintUtil {

私有输出流写入器 mWriter 等于 null;
私有输出流 mOutputStream 等于 null;

public final static int WIDTH_PIXEL = 384;
public final static int IMAGE_SIZE = 320;
private static String myTime;

/** * 初始化输出流写入器
* @param outputStream 流输出
* @param encoding 编码参数
*/
public PrintUtil(OutputStream outputStream, String encoding) throws IOException {
mWriter = new OutputStreamWriter(outputStream, encoding);
mOutputStream = outputStream;
initPrinter();
}

public void print(byte[] bs) throws IOException {
mOutputStream.write(bs);
}

public void printRawBytes(byte[] bytes) throws IOException {
mOutputStream.write(bytes);
mOutputStream.flush();
}

/** * 启动打印机设备

    • @throws I/O 异常 */
      public void initPrinter() throws I/O Exception {
      mWriter issuing a reset command;
      mWriter issuing a print command;
      mWriter flush buffer;
      // 获取当前时间
      getTime();
      }

/** * 输出换行
* @throws IOException * @return 要输出的空行数量 */
public void printLine(int lineNum) throws IOException {
while (lineNum-- > 0) {
mWriter.write('\n');
}
mWriter.flush();
}

/**

  • 打印仅一行
    • @throws IOException
      */
      public void printLine() throws IOException {
      // 调用自身一次
      printLine();
      }

/**

  • 输出空白段落(长度为一个制表符的大约4个汉字)
    • @param length 表示要输出制表符的数量
  • @throws IOException
    */
    public void printTabSpace(int length) throws IOException {
    for(int i=0; i<length; i++){
    mWriter.write("\t");
    }
    mWriter.flush();
    }

/** * 绝对打印位置
* * @return
* @throws IOException
*/
public byte[] setLocation(int offset) throws IOException {
byte[] bs = new byte[4];
bs[0] = 0x1B;
bs[1] = 0x24;
bs[2] = (byte) (offset % 256);
bs[3] = (byte) (offset / 256);
return bs;
}

public byte array getGBK(String text) throws Exception {
byte[] result = text.getBytes("GBK");
// 该操作必须包含在try块中
return result;
}

私有函数 private int getString PixLength(String str) {
整型变量 pixLength 初始化为 0。
char 型变量 c 定义。
for 循环从索引 i 等于 0 开始。
i 小于 str 的长度时执行循环体。
c 等于 str 的第 i 个字符。
如果 Pinyin.isChinese(c) 为真,则 pixLength 加 24。
否则 pixLength 加 12。
循环结束后返回 pixLength 的值。
}

public int computeOffset(String str) {
return WIDTH_PIXEL - getStringPixelLength(str);
}

// 打印给定的文字
public void printStringContent(String text) throws IOException {
// 使用指定的Writer实例将文本内容写入并flush缓冲区。
mWriter.write(text);
mWriter.flush();
}

/** * 指定字符在字符串中的位置方式为: 0 表示左对齐、1 表示中间对齐、2 表示右对齐

public void printLargeText(String text) throws IOException {

复制代码
     mWriter.write(0x1b);

     mWriter.write(0x21);
     mWriter.write(48);
    
     mWriter.write(text);
    
     mWriter.write(0x1b);
     mWriter.write(0x21);
     mWriter.write(0);
    
     mWriter.flush();

}

public void printTwoColumn(String title, String content) throws IOException {
int iNum = 0;
byte[] byteBuffer = new byte[100];
byte[] tmp;

复制代码
     tmp = getGbk(title);

     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = setLocation(getOffset(content));
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = getGbk(content);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
    
     print(byteBuffer);

}

public void printThreeColumns(String left, String middle, String right) throws Exception IO {
long num = 0;
byte[] buffer = new byte[200];
byte[] tmpBuffer = new byte[0];
// 添加注释是为了提高代码可读性
// left: 左侧字符串
// middle: 中间字符串
// right: 右侧字符串

复制代码
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);

     iNum += tmp.length;
    
     tmp = getGbk(left);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     int pixLength = getStringPixLength(left) % WIDTH_PIXEL;
     if (pixLength > WIDTH_PIXEL / 2 || pixLength == 0) {
     middle = "\n\t\t" + middle;
     }
    
     tmp = setLocation(192);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = getGbk(middle);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = setLocation(getOffset(right));
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = getGbk(right);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
    
     print(byteBuffer);

}

public void printDashLine() throws IOException {
printText("--------------------------------");
}

public void printBitmap(Bitmap bitmap) throws IOException {
bitmap经过压缩处理后赋值给变量compressPic;
首先调用draw2PxPoint方法生成点阵图数据;
将点阵图数据转换为字节数组并存储于变量bmpByteArray;
最后调用printRawBytes方法打印未压缩的点阵图数据;
}

/************************************************************************* *

设想一张分辨率设定为360×360像素的图片,在y轴方向上均匀分布着多个采样点。假设将该图片按照一定的算法进行处理后,在x轴方向上分为15行进行打印输出。每一行都生成一个大小为360×24像素的小矩阵,在y轴方向上有24个采样点,并且每个采样点占用三个字节的空间来存储数据信息。

每个byte单元负责存储八个像素信息。

byte[] result = new byte[k];
System.arraycopy(tmp, 0, result, 0, k);
return result;
}

/** * 图片二值化,黑色是1,白色是0
* * @param x 横坐标
* @param y 纵坐标
* @param bit 位图
* @return
*/
private byte px2Byte(int x, int y, Bitmap bit) {
if (x < bit.getWidth() && y < bit.getHeight()) {
byte b;
int pixel = bit.getPixel(x, y);
int red = (pixel & 0x00ff0000) >> 16; // 取高两位
int green = (pixel & 0x0000ff00) >> 8; // 取中两位
int blue = pixel & 0x000000ff; // 取低两位
int gray = RGB2Gray(red, green, blue);
if (gray < 128) {
b = 1;
} else {
b = 0;
}
return b;
}
return 0;
}

/**

  • 将RGB颜色值转换为灰度值
    */
    private double GrayscaleConversion(int red, int green, int blue) {
    double gray = (double)(red * 30 + green * 58 + blue * 14) / 100;
    return gray;
    }

/** * 对图片进行压缩(去除透明度)
* * @param bitmapOrg
*/
private Bitmap compressPic(Bitmap bitmapOrg) {
// 获取这个图片的宽和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
// 定义预转换成的图片的宽度和高度
int newWidth = IMAGE_SIZE;
int newHeight = IMAGE_SIZE;
Bitmap targetBmp = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas targetCanvas = new Canvas(targetBmp);
targetCanvas.drawColor(0xffffffff);
targetCanvas.drawBitmap(bitmapOrg, new Rect(0, 0, width, height), new Rect(0, 0, newWidth, newHeight), null);
return targetBmp;
}

public static method outputResult(Bluetooth Socket实例 bluetooth, BitMap对象 bitmap) {

复制代码
     try {

     PrintUtil pUtil = new PrintUtil(bluetoothSocket.getOutputStream(), "GBK");
     // 店铺名 居中 放大
     pUtil.printAlignment(1);
     pUtil.printLargeText("广州德胜");
     pUtil.printLine();
     pUtil.printAlignment(0);
     pUtil.printLine();
    
     pUtil.printTwoColumn("时间:", myTime);
     pUtil.printLine();
    
     pUtil.printTwoColumn("订单号:", System.currentTimeMillis() + "");
     pUtil.printLine();
    
     pUtil.printTwoColumn("付款人:", "VitaminChen");
     pUtil.printLine();
    
     // 分隔线
     pUtil.printDashLine();
     pUtil.printLine();
    
     //打印商品列表
     pUtil.printText("商品");
     pUtil.printTabSpace(2);
     pUtil.printText("数量");
     pUtil.printTabSpace(1);
     pUtil.printText("    单价");
     pUtil.printLine();
    
     pUtil.printThreeColumn("iphone6", "1", "4999.00");
     pUtil.printThreeColumn("测试测试", "1", "4999.00");
    
     pUtil.printDashLine();
     pUtil.printLine();
    
     pUtil.printTwoColumn("订单金额:", "9998.00");
     pUtil.printLine();
    
     pUtil.printTwoColumn("实收金额:", "10000.00");
     pUtil.printLine();
    
     pUtil.printTwoColumn("找零:", "2.00");
     pUtil.printLine();
    
     pUtil.printDashLine();
     //打印图片
    // pUtil.printBitmap(bitmap);
    
     pUtil.printLine(4);
    
     } catch (IOException e) {
    
     }

}

private void getTime() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
if (month > 9) {
myTime = year + “-” + month + “-” + day;
} else {
if (day > 9) {
myTime = year + “-” + “0” + month + “-” + day;
} else {
myTime = year + “-” + “0” + month + “-” + “0” + day;
}

复制代码
     }

}
}

第二步 :还有一个检测蓝牙是否连接的工具类

import android.app.AndroidActivity;
import android.bluetooth.AndroidBluetoothAdapter;
import android.bluetooth.AndroidBluetoothClass;
import android.bluetooth.AndroidBluetoothDevice;
import android.bluetooth.AndroidBluetoothSocket;
import android.content.Intent;

导入IOException对象。
导入ArrayList集合类型。
导入List集合类型。
导入Set集合类型。
导入UUID类。

public class BluetoothUtil {

复制代码
    /** * 蓝牙是否打开
     */
    public static boolean isBluetoothOn() {
    	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    	if (mBluetoothAdapter != null)
    		// 蓝牙已打开
    		if (mBluetoothAdapter.isEnabled())
    			return true;
    
    	return false;
    }
    
    /** * 获取所有已配对的设备
     */
    public static List<BluetoothDevice> getPairedDevices() {
    	List deviceList = new ArrayList<>();
    	Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    	if (pairedDevices.size() > 0) {
    		for (BluetoothDevice device : pairedDevices) {
    			deviceList.add(device);
    		}
    	}
    	return deviceList;
    }
    
    /** * 获取所有已配对的打印类设备
     */
    public static List<BluetoothDevice> getPairedPrinterDevices() {
    	return getSpecificDevice(BluetoothClass.Device.Major.IMAGING);
    }
    
    /** * 从已配对设配中,删选出某一特定类型的设备展示
     * @param deviceClass
     * @return
     */
    public static List<BluetoothDevice> getSpecificDevice(int deviceClass){
    	List<BluetoothDevice> devices = BluetoothUtil.getPairedDevices();
    	List<BluetoothDevice> printerDevices = new ArrayList<>();
    
    	for (BluetoothDevice device : devices) {
    		BluetoothClass klass = device.getBluetoothClass();
    		// 关于蓝牙设备分类参考 http://stackoverflow.com/q/23273355/4242112
    		if (klass.getMajorDeviceClass() == deviceClass)
    			printerDevices.add(device);
    	}
    
    	return printerDevices;
    }
    
    /** * 弹出系统对话框,请求打开蓝牙
     */
    public static void openBluetooth(Activity activity) {
    	Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	activity.startActivityForResult(enableBtIntent, 666);
    }
    
    public static BluetoothSocket connectDevice(BluetoothDevice device) {
    	BluetoothSocket socket = null;
    	try {
    		socket = device.createRfcommSocketToServiceRecord(
    				UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
    		socket.connect();
    	} catch (IOException e) {
    		try {
    			socket.close();
    		} catch (IOException closeException) {
    			return null;
    		}
    		return null;
    	}
    	return socket;
    }

}

在本步骤中,我们计划创建一个基础类,并引入以下所需的相关内容:android.app_PROGRESS_DIALOG用于显示进度条;android.bluetooth_BLUETOOTH_ADAPTER负责蓝牙适配器的操作;android.bluetooth_BLUETOOTH_DEVICE管理蓝牙设备;android.bluetooth_BLUETOOTH_SOCKET处理蓝牙连接;android.content.Broadcast_receiver用于广播接收操作;android.content.CONTEXT提供应用程序上下文信息;android.content.Intent用于处理意图请求;android.content.Intent_filter用于过滤无关意图;android.os.PROCESS用于执行后台进程任务;android.os.Bundle作为应用的整体管理单元;同时引入了Android支持的版本7的应用体态(Androidx)并将其标记为活动应用,默认实现所有接口以供后续开发使用。

import com.will.bluetoothprinterdemo.utils.BluetoothUtil;

import java.io.IOException;

public abstract class BasePrintActivity extends AppCompatActivity {

复制代码
    String tag = getClass().getSimpleName();
    private BluetoothSocket mSocket;
    private BluetoothStateReceiver mBluetoothStateReceiver;
    private AsyncTask mConnectTask;
    private ProgressDialog mProgressDialog;
    
    /** * 蓝牙连接成功后回调,该方法在子线程执行,可执行耗时操作
     */
    public abstract void onConnected(BluetoothSocket socket, int taskType);
    
    
    /** * 蓝牙状态发生变化时回调
     */
    public void onBluetoothStateChanged(Intent intent) {
    
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initReceiver();
    }
    
    @Override
    protected void onStop() {
    cancelConnectTask();
    closeSocket();
    super.onStop();
    }
    
    protected void closeSocket() {
    if (mSocket != null) {
        try {
            mSocket.close();
        } catch (IOException e) {
            mSocket = null;
            e.printStackTrace();
        }
    }
    }
    
    protected void cancelConnectTask() {
    if (mConnectTask != null) {
        mConnectTask.cancel(true);
        mConnectTask = null;
    }
    }
    
    @Override
    protected void onDestroy() {
    unregisterReceiver(mBluetoothStateReceiver);
    super.onDestroy();
    }
    
    private void initReceiver() {
    mBluetoothStateReceiver = new BluetoothStateReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mBluetoothStateReceiver, filter);
    }
    
    /** * 检查蓝牙状态,如果已打开,则查找已绑定设备
     * * @return
     */
    public boolean checkBluetoothState() {
    if (BluetoothUtil.isBluetoothOn()) {
        return true;
    } else {
        BluetoothUtil.openBluetooth(this);
        return false;
    }
    }
    
    public void connectDevice(BluetoothDevice device, int taskType) {
    if (checkBluetoothState() && device != null) {
        mConnectTask = new ConnectBluetoothTask(taskType).execute(device);
    }
    }
    
    
    class ConnectBluetoothTask extends AsyncTask<BluetoothDevice, Integer, BluetoothSocket> {
    
    int mTaskType;
    
    public ConnectBluetoothTask(int taskType) {
        this.mTaskType = taskType;
    }
    
    @Override
    protected void onPreExecute() {
        showProgressDialog("请稍候...");
        super.onPreExecute();
    }
    
    @Override
    protected BluetoothSocket doInBackground(BluetoothDevice... params) {
        if(mSocket != null){
            try {
                mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mSocket = BluetoothUtil.connectDevice(params[0]);;
        onConnected(mSocket, mTaskType);
        return mSocket;
    }
    
    @Override
    protected void onPostExecute(BluetoothSocket socket) {
        mProgressDialog.dismiss();
        if (socket == null || !socket.isConnected()) {
            toast("连接打印机失败");
        } else {
            toast("成功!");
        }
    
        super.onPostExecute(socket);
    }
    }
    
    
    protected void showProgressDialog(String message) {
    if (mProgressDialog == null) {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
    }
    mProgressDialog.setMessage(message);
    if (!mProgressDialog.isShowing()) {
        mProgressDialog.show();
    }
    }
    
    protected void toast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
    
    /** * 监听蓝牙状态变化的系统广播
     */
    class BluetoothStateReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
        switch (state) {
            case BluetoothAdapter.STATE_TURNING_ON:
                toast("蓝牙已开启");
                break;
    
            case BluetoothAdapter.STATE_TURNING_OFF:
                toast("蓝牙已关闭");
                break;
        }
        onBluetoothStateChanged(intent);
    }
    }

}

第四步:主界面代码

package com.will.bluetoothprinterdemo.ui;

导入android.bluetooth中的BluetoothDevice类。
导入android.bluetooth中的BluetoothSocket类。
导入android.content中的Context类。
导入android.content中的Intent类。
导入android.graphics中的Bitmap类。
使用android.graphics.BitmapFactory创建Bitmap。
导入android.os.Bundle。
获取 Settings 资源。
创建)VیzorIntendedView.
初始化Android视图库。
创建Android view.
创建Android view view.
创建Android view view view.
创建Android widget LinearLayout.
添加Light组件到布局中。
在布局中添加Button组件。
在布局中添加CheckBox组件。
在布局中添加TextView组件。
在布局中添加 Toast组件用于通知消息提示信息的显示操作功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础功能实现完整流程图创建基础上扩展基本组件构建复合图形界面应用开发核心逻辑设计完成并进行测试验证

import com.will.bluetoothprinterdemo.R;
import com.will.bluetoothprinterdemo.utils.BluetoothUtil;
import com.will.bluetoothprinterdemo.utils.PrintUtil;

import java.util.List;

public class PrintingConfiguration extends BasePrintActivity implements UIView_ENUMERATORMode {

复制代码
    ListView mLvPairedDevices;
    Button mBtnSetting;
    Button mBtnTest;
    Button mBtnPrint;
    
    DeviceListAdapter mAdapter;
    int mSelectedPosition = -1;
    
    final static int TASK_TYPE_CONNECT = 1;
    final static int TASK_TYPE_PRINT = 2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_printer_setting);
    initViews();
    }
    
    @Override
    protected void onResume() {
    super.onResume();
    fillAdapter();
    }
    
    private void initViews() {
    mLvPairedDevices = (ListView) findViewById(R.id.lv_paired_devices);
    mBtnSetting = (Button) findViewById(R.id.btn_goto_setting);
    mBtnTest = (Button) findViewById(R.id.btn_test_conntect);
    mBtnPrint = (Button) findViewById(R.id.btn_print);
    
    mLvPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mSelectedPosition = position;
            mAdapter.notifyDataSetChanged();
        }
    });
    
    mBtnSetting.setOnClickListener(this);
    mBtnTest.setOnClickListener(this);
    mBtnPrint.setOnClickListener(this);
    
    mAdapter = new DeviceListAdapter(this);
    mLvPairedDevices.setAdapter(mAdapter);
    }
    
    /** * 从所有已配对设备中找出打印设备并显示
     */
    private void fillAdapter() {
    //推荐使用 BluetoothUtil.getPairedPrinterDevices()
    List<BluetoothDevice> printerDevices = BluetoothUtil.getPairedDevices();
    mAdapter.clear();
    mAdapter.addAll(printerDevices);
    refreshButtonText(printerDevices);
    }
    
    private void refreshButtonText(List<BluetoothDevice> printerDevices) {
    if (printerDevices.size() > 0) {
        mBtnSetting.setText("配对更多设备");
    } else {
        mBtnSetting.setText("还未配对打印机,去设置");
    }
    }
    
    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_goto_setting:
            startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
            break;
    
        case R.id.btn_test_conntect:
            connectDevice(TASK_TYPE_CONNECT);
            break;
    
        case R.id.btn_print:
            connectDevice(TASK_TYPE_PRINT);
            break;
    }
    }
    
    private void connectDevice(int taskType){
    if(mSelectedPosition >= 0){
        BluetoothDevice device = mAdapter.getItem(mSelectedPosition);
        if(device!= null)
            super.connectDevice(device, taskType);
    }else{
        Toast.makeText(this, "还未选择打印设备", Toast.LENGTH_SHORT).show();
    }
    }
    
    @Override
    public void onConnected(BluetoothSocket socket, int taskType) {
    switch (taskType){
        case TASK_TYPE_PRINT:
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
            PrintUtil.printTest(socket, bitmap);
            break;
    }
    }
    
    
    class DeviceListAdapter extends ArrayAdapter<BluetoothDevice> {
    
    public DeviceListAdapter(Context context) {
        super(context, 0);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        BluetoothDevice device = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_bluetooth_device, parent, false);
        }
    
        TextView tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
        CheckBox cbDevice = (CheckBox) convertView.findViewById(R.id.cb_device);
    
        tvDeviceName.setText(device.getName());
    
        cbDevice.setChecked(position == mSelectedPosition);
    
        return convertView;
    }
    }

}

第五步:主界面布局
activity_printer_setting.xml

复制代码
    <TextView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/colorGrayTertiary"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:text="选择一个已配对的打印设备"
    android:textColor="@color/textColorSecondary"
    android:textSize="14sp" />
    
    <ListView
    android:id="@+id/lv_paired_devices"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorWhite"
    android:choiceMode="singleChoice"
    android:divider="@android:drawable/divider_horizontal_bright" />
    
    <Button
    android:id="@+id/btn_goto_setting"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/common_margin"
    android:layout_marginRight="@dimen/common_margin"
    android:layout_marginTop="@dimen/common_margin"
    android:text="还未配对打印机,去设置" />
    
    <Button
    android:id="@+id/btn_test_conntect"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/common_margin"
    android:layout_marginRight="@dimen/common_margin"
    android:layout_marginTop="@dimen/common_margin"
    android:text="测试打印机连接" />
    
    <Button
    android:id="@+id/btn_print"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/common_margin"
    android:layout_marginRight="@dimen/common_margin"
    android:layout_marginTop="@dimen/common_margin"
    android:text="打印"
    android:layout_marginBottom="@dimen/common_margin"/>

布局二:

复制代码
    <TextView
    android:id="@+id/tv_device_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="蓝牙打印机"
    android:textColor="@color/textColorPrimary"
    android:textSize="14sp" />
    
    <android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cb_device"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textSize="14sp" />

全部评论 (0)

还没有任何评论哟~