权限private void checkPermission() {for (String permission : permissions) {if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, permission)) {per.add(permission);}}}private void requestPermission() {if (per.size() > 0) {String[] p = new String[per.size()];ActivityCompat.requestPermissions(this, per.toArray(p), REQUEST_CODE);}}public void btnConnect(View view) {startActivityForResult(new Intent(MainActivity.this, BluetoothListActivity.class), BLUETOOTH_REQUEST_CODE);}
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {//蓝牙连接if (requestCode == BLUETOOTH_REQUEST_CODE) {closePort();//获取蓝牙mac地址String macAddress = data.getStringExtra(BluetoothListActivity.EXTRA_DEVICE_ADDRESS);//初始化DeviceConnFactoryManager 并设置信息new DeviceConnFactoryManager.Build()//设置标识符.setId(id)//设置连接方式.setConnMethod(DeviceConnFactoryManager.CONN_METHOD.BLUETOOTH)//设置连接的蓝牙mac地址.setMacAddress(macAddress).build();//配置完信息,就可以打开端口连接了Log.i("TAG", "onActivityResult: 连接蓝牙" + id);threadPool = ThreadPool.getInstantiation();threadPool.addTask(new Runnable() {@Overridepublic void run() {DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].openPort();}});}}}/*** 重新连接回收上次连接的对象,避免内存泄漏*/private void closePort() {if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] != null && DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].mPort != null) {DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].reader.cancel();DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].mPort.closePort();DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].mPort = null;}}创建一Receiver@Overrideprotected void onStart() {super.onStart();/** 注册接收连接状态的广播*/IntentFilter filter = new IntentFilter();filter.addAction(ACTION_QUERY_PRINTER_STATE);filter.addAction(DeviceConnFactoryManager.ACTION_CONN_STATE);registerReceiver(receiver, filter);}@Overrideprotected void onStop() {super.onStop();unregisterReceiver(receiver);}/*** 连接状态的广播*/private BroadcastReceiver receiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (DeviceConnFactoryManager.ACTION_CONN_STATE.equals(action)) {int state = intent.getIntExtra(DeviceConnFactoryManager.STATE, -1);int deviceId = intent.getIntExtra(DeviceConnFactoryManager.DEVICE_ID, -1);switch (state) {case DeviceConnFactoryManager.CONN_STATE_DISCONNECT:if (id == deviceId) mTvState.setText("未连接");break;case DeviceConnFactoryManager.CONN_STATE_CONNECTING:mTvState.setText("连接中");break;case DeviceConnFactoryManager.CONN_STATE_CONNECTED:mTvState.setText("已连接");Toast.makeText(MainActivity.this, "已连接", Toast.LENGTH_SHORT).show();break;case CONN_STATE_FAILED:mTvState.setText("未连接");Toast.makeText(MainActivity.this, "连接失败!重试或重启打印机试试", Toast.LENGTH_SHORT).show();break;}/* Usb连接断开、蓝牙连接断开广播 */} else if (ACTION_USB_DEVICE_DETACHED.equals(action)) {mHandler.obtainMessage(CONN_STATE_DISCONN).sendToTarget();}}};@SuppressLint("HandlerLeak")private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case CONN_STATE_DISCONN:if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] != null || !DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()) {DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].closePort(id);Toast.makeText(MainActivity.this, "成功断开连接", Toast.LENGTH_SHORT).show();}break;case PRINTER_COMMAND_ERROR:Toast.makeText(MainActivity.this, "请选择正确的打印机指令", Toast.LENGTH_SHORT).show();break;case CONN_PRINTER:Toast.makeText(MainActivity.this, "请先连接打印机", Toast.LENGTH_SHORT).show();break;}}};/*** 打印标签*/public void btnPrint(View view) {printLabel();}public void printLabel() {Log.i("TAG", "准备打印");threadPool = ThreadPool.getInstantiation();threadPool.addTask(new Runnable() {@Overridepublic void run() {//先判断打印机是否连接if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null ||!DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()) {mHandler.obtainMessage(CONN_PRINTER).sendToTarget();return;}if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.TSC) {Log.i("TAG", "开始打印");sendLabel();} else {mHandler.obtainMessage(PRINTER_COMMAND_ERROR).sendToTarget();}}});}private void sendLabel() {LabelCommand tsc = new LabelCommand();tsc.addSize(40, 30); // 设置标签尺寸,按照实际尺寸设置tsc.addGap(1); // 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0tsc.addDirection(LabelCommand.DIRECTION.FORWARD, LabelCommand.MIRROR.NORMAL);// 设置打印方向tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);//开启带Response的打印,用于连续打印tsc.addReference(0, 0);// 设置原点坐标tsc.addTear(EscCommand.ENABLE.ON); // 撕纸模式开启tsc.addCls();// 清除打印缓冲区// 绘制简体中文tsc.addText(30, 30, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"这是标题");tsc.addText(200, 30, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"序号:" + "1");tsc.addText(30, 90, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"价格:" + "99.00");tsc.addText(30, 140, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"数量:" + "99");tsc.addText(30, 190, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,"日期:" + "2020-02-02");绘制图片
// Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
// tsc.addBitmap(20, 50, LabelCommand.BITMAP_MODE.OVERWRITE, b.getWidth(), b);//二维码tsc.addQRCode(200, 90, LabelCommand.EEC.LEVEL_L, 4, LabelCommand.ROTATION.ROTATION_0, "www.baidu.com");tsc.addPrint(1, 1); // 打印标签tsc.addSound(2, 100); // 打印标签后 蜂鸣器响/* 发送数据 */Vector<Byte> data = tsc.getCommand();if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null) {Log.i("TAG", "sendLabel: 打印机为空");return;}DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendDataImmediately(data);}
//[ 这是比较详细的,我也是借鉴的](https://github.com/yechaoa/PrinterDemo)