Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to specify charset for the serial device #4

Merged
merged 4 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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