Skip to content

Commit

Permalink
Merge pull request #4 from augonis/master
Browse files Browse the repository at this point in the history
Ability to specify charset for the serial device
  • Loading branch information
harryjph authored May 13, 2019
2 parents 213b4c5 + b4a0c39 commit fe63734
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,17 @@ public List<BluetoothDevice> getPairedDevicesList() {
* a BluetoothSerialDevice or a BluetoothConnectException
*/
public Single<BluetoothSerialDevice> 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<BluetoothSerialDevice> openSerialDevice(String mac, Charset charset) {
if (devices.containsKey(mac)) {
return Single.just(devices.get(mac));
} else {
Expand All @@ -63,7 +75,7 @@ public Single<BluetoothSerialDevice> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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)); });
}

/**
Expand All @@ -54,7 +57,7 @@ public Completable send(String message) {
public Flowable<String> 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 {
Expand Down

0 comments on commit fe63734

Please sign in to comment.