diff --git a/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothManager.java b/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothManager.java index bdf4835..a866379 100644 --- a/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothManager.java +++ b/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothManager.java @@ -9,6 +9,7 @@ import java.io.InputStream; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -54,6 +55,17 @@ public List getPairedDevicesList() { * a BluetoothSerialDevice or a BluetoothConnectException */ public Single openSerialDevice(String mac) { + return openSerialDevice(mac, Charset.defaultCharset()); + } + + /** + * @param mac The MAC address of the device + * you are trying to connect to + * @param charset The Charset to use for input/output streams + * @return An RxJava Single, that will either emit + * a BluetoothSerialDevice or a BluetoothConnectException + */ + public Single openSerialDevice(String mac, Charset charset) { if (devices.containsKey(mac)) { return Single.just(devices.get(mac)); } else { @@ -63,7 +75,7 @@ public Single openSerialDevice(String mac) { BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); adapter.cancelDiscovery(); socket.connect(); - BluetoothSerialDevice serialDevice = BluetoothSerialDevice.getInstance(mac, socket); + BluetoothSerialDevice serialDevice = BluetoothSerialDevice.getInstance(mac, socket, charset); devices.put(mac, serialDevice); return serialDevice; } catch (Exception e) { diff --git a/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothSerialDevice.java b/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothSerialDevice.java index 272146a..d93ee84 100644 --- a/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothSerialDevice.java +++ b/androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothSerialDevice.java @@ -9,6 +9,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.nio.charset.Charset; import io.reactivex.BackpressureStrategy; import io.reactivex.Completable; @@ -22,19 +23,21 @@ public class BluetoothSerialDevice { private final BluetoothSocket socket; private final OutputStream outputStream; private final InputStream inputStream; + private final Charset charset; @Nullable private SimpleBluetoothDeviceInterface owner; - private BluetoothSerialDevice(String mac, BluetoothSocket socket, OutputStream outputStream, InputStream inputStream) { + private BluetoothSerialDevice(String mac, BluetoothSocket socket, OutputStream outputStream, InputStream inputStream, Charset charset) { this.mac = mac; this.socket = socket; this.outputStream = outputStream; this.inputStream = inputStream; + this.charset = charset; } - static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket) throws IOException { - return new BluetoothSerialDevice(mac, socket, socket.getOutputStream(), socket.getInputStream()); + static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket, Charset charset) throws IOException { + return new BluetoothSerialDevice(mac, socket, socket.getOutputStream(), socket.getInputStream(), charset); } /** @@ -44,7 +47,7 @@ static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket) thr */ public Completable send(String message) { requireNotClosed(); - return Completable.fromAction(() -> { if (!closed) outputStream.write(message.getBytes()); }); + return Completable.fromAction(() -> { if (!closed) outputStream.write(message.getBytes(charset)); }); } /** @@ -54,7 +57,7 @@ public Completable send(String message) { public Flowable openMessageStream() { requireNotClosed(); return Flowable.create(emitter -> { - BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); + BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, charset)); while (!emitter.isCancelled() && !closed) { synchronized (this) { try {