Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Commit

Permalink
Add proper error handling for open() and close()
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Siong Chan committed Sep 2, 2019
1 parent 4383bd0 commit a58c650
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
Binary file modified android/libs/escposjava-1.0-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ public BluetoothPrinter(String address) {
this.address = address;
}

public void open() {
try {
BluetoothDevice device = adapter.getRemoteDevice(address);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
socket.connect();
printer = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
public void open() throws IOException {
BluetoothDevice device = adapter.getRemoteDevice(address);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
socket.connect();
printer = socket.getOutputStream();
}

public void write(byte[] command) {
Expand All @@ -42,11 +38,7 @@ public void write(byte[] command) {
}
}

public void close() {
try {
printer.close();
} catch (IOException e) {
e.printStackTrace();
}
public void close() throws IOException {
printer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,32 +201,44 @@ public void setConfig(ReadableMap config) {

@ReactMethod
public void connectBluetoothPrinter(String address, Promise promise) {
if (!"bluetooth".equals(config.getString("type"))) {
promise.reject("config.type is not a bluetooth type");
try {
if (!"bluetooth".equals(config.getString("type"))) {
promise.reject("config.type is not a bluetooth type");
}
Printer printer = new BluetoothPrinter(address);
printerService = new PrinterService(printer);
promise.resolve(true);
} catch (IOException e) {
promise.reject(e);
}
Printer printer = new BluetoothPrinter(address);
printerService = new PrinterService(printer);
promise.resolve(true);
}

@ReactMethod
public void connectNetworkPrinter(String address, int port, Promise promise) {
if (!"network".equals(config.getString("type"))) {
promise.reject("config.type is not a network type");
try {
if (!"network".equals(config.getString("type"))) {
promise.reject("config.type is not a network type");
}
Printer printer = new NetworkPrinter(address, port);
printerService = new PrinterService(printer);
promise.resolve(true);
} catch (IOException e) {
promise.reject(e);
}
Printer printer = new NetworkPrinter(address, port);
printerService = new PrinterService(printer);
promise.resolve(true);
}

@ReactMethod
public void disconnect(Promise promise) {
printerService.close();
promise.resolve(true);
try {
printerService.close();
promise.resolve(true);
} catch (IOException e) {
promise.reject(e);
}
}

@ReactMethod
public void scanDevice() {
public void scanDevices() {
scanManager.registerCallback(new ScanManager.OnBluetoothScanListener() {
@Override
public void deviceFound(BluetoothDevice bluetoothDevice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import leesiongchan.reactnativeescpos.helpers.EscPosHelper;
import leesiongchan.reactnativeescpos.utils.BitMatrixUtils;
Expand Down Expand Up @@ -139,11 +139,11 @@ public void beep() {
basePrinterService.beep();
}

public void open() {
public void open() throws IOException {
basePrinterService.open();
}

public void close() {
public void close() throws IOException {
basePrinterService.close();
}

Expand Down

0 comments on commit a58c650

Please sign in to comment.