Java

集成

Java SDK 发布至 gitlab maven registry, 可以透过在你的项目中引入 repository 来引用 SDK. 或者直接使用demo里的jar包来集成开发(推荐). Maven

<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/projects/35052067/packages/maven</url>
  </repository>
</repositories>

Grandle

maven {
  url 'https://gitlab.com/api/v4/projects/35052067/packages/maven'
}

Kotlin

maven("https://gitlab.com/api/v4/projects/35052067/packages/maven")

接下来直接在你的项目中添加对应指令的依赖即可.

CPCL指令

implementation 'com.printer.psdk:fat-generic-cpcl-bluetooth-classic:0.1.8-GA'

ESC指令

implementation 'com.printer.psdk:fat-generic-esc-bluetooth-classic:0.1.8-GA'

TSPL指令

implementation 'com.printer.psdk:fat-generic-tspl-bluetooth-classic:0.1.8-GA'

同时需要多种指令

implementation 'com.printer.psdk:fat-generic-tspl-bluetooth-classic:0.1.8-GA'

最新发布的版本可以从此处查看: package-center

使用

蓝牙部分示例代码

//可以直接通过蓝牙扫描获取到BluetoothDevice
BluetoothDevice device = getIntent().getParcelableExtra("device");
//或者通过传mac地址来获取BluetoothDevice
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bluetoothAddress);
//根据BluetoothDevice创建连接对象
Connection connection = ClassicBluetooth.getInstance().createConnection(device, new ConnectListener() {
  @Override
  public void onConnectSuccess(ConnectedDevice connectedDevice) {
    //设备连接成功的回调,根据对应指令生成对应指令的对象 如cpcl:
    GenericCPCL cpcl = CPCL.generic(connectedDevice);
    //tspl:
    GenericTSPL tspl = TSPL.generic(connectedDevice);
    //esc:
    GenericESC esc = ESC.generic(connectedDevice);
    //如果想监听打印机回传的数据可以调用
    DataListener.with(connectedDevice).listen(new ListenAction() {
      @Override
      public void action(byte[] bytes) {
        //打印机回传的数据处理
      }
    });
  }

  @Override
  public void onConnectFail(String errMsg, Throwable e) {
  }

  @Override
  public void onConnectionStateChanged(BluetoothDevice device, int state) {
    String msg;
    switch (state) {
      case Connection.STATE_CONNECTING:
        msg = "连接中";
        break;
      case Connection.STATE_PAIRING:
        msg = "配对中...";
        break;
      case Connection.STATE_PAIRED:
        msg = "配对成功";
        break;
      case Connection.STATE_CONNECTED:
        msg = "连接成功";
        break;
      case Connection.STATE_DISCONNECTED:
        msg = "连接断开";
        break;
      case Connection.STATE_RELEASED:
        msg = "连接已销毁";
        break;
      default:
        msg = "";
    }
  }

});
//连接是个耗时操作可以开个线程
new Thread(new Runnable() {
  @Override
  public void run() {
    //调用连接方法连接设备 连接成功会走onConnectSuccess回调
    connection.connect(null);
  }
}).start();

CPCL指令示例代码

//使用指令对象cpcl来拼接指令
//一个完整的打印指令一定要有page()开头和print()结尾
GenericCPCL _gcpcl = cpcl.page(CPage.builder().width(608).height(1040).copies(sampleNumber).build())
  .box(CBox.builder().topLeftX(0).topLeftY(1).bottomRightX(598).bottomRightY(664).lineWidth(2).build())
  .line(CLine.builder().startX(0).startY(88).endX(598).endY(88).lineWidth(2).build())
  .bar(CBar.builder().x(120).y(88 + 12).lineWidth(1).height(80).content("1234567890").build())
  .text(CText.builder().textX(120 + 12).textY(88 + 20 + 76).font(Font.TSS24).content("1234567890").build())
  .print(CPrint.builder().build());
//最后使用write()方法把指令写入打印机
_gcpcl.write();

TSPL指令示例代码

//使用指令对象tspl来拼接指令
//一个完整的打印指令一定要有page()开头和print()结尾
GenericTSPL _gtspl = tspl.page(TPage.builder().width(100).height(180).build())
  .direction(TDirection.builder().direction(TDirection.Direction.UP_OUT).mirror(TDirection.Mirror.NO_MIRROR).build())
  .gap(true)
  .cut(true)
  .speed(6)
  .density(6)
  .cls()
  .bar(TBar.builder().x(300).y(10).width(4).height(90).build())
  .text(TText.builder().x(400).y(25).font(Font.TSS24).xmulti(3).ymulti(3).content("上海浦东").build())
  .circle(TCircle.builder().x(670).y(1170).width(6).radius(100).build())
  .qrcode(TQRCode.builder().x(620).y(620).correctLevel(CorrectLevel.H).cellWidth(4).content("www.qrprt.com").build())
  .print(1);
//最后使用write()方法把指令写入打印机
_gtspl.write();

ESC指令示例代码

//使用指令对象esc来拼接指令
GenericESC _gesc = esc.enable()
  .wakeup()
  .location(ELocation.builder().location(Location.LEFT).build())
  .image(EImage.builder()
    .image(bitmap2Bytes(bitmap))
    .compress(true)
    .build())
  .lineDot(30)
  .stopJob();
//最后使用write()方法把指令写入打印机
_gesc.write();

此外, 可以参考 demo 或者 参考手册 来查看如何使用 SDK.