Skip to content

Commit fe63734

Browse files
authored
Merge pull request #4 from augonis/master
Ability to specify charset for the serial device
2 parents 213b4c5 + b4a0c39 commit fe63734

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.InputStream;
1010
import java.io.StringWriter;
1111
import java.io.Writer;
12+
import java.nio.charset.Charset;
1213
import java.util.ArrayList;
1314
import java.util.Iterator;
1415
import java.util.List;
@@ -54,6 +55,17 @@ public List<BluetoothDevice> getPairedDevicesList() {
5455
* a BluetoothSerialDevice or a BluetoothConnectException
5556
*/
5657
public Single<BluetoothSerialDevice> openSerialDevice(String mac) {
58+
return openSerialDevice(mac, Charset.defaultCharset());
59+
}
60+
61+
/**
62+
* @param mac The MAC address of the device
63+
* you are trying to connect to
64+
* @param charset The Charset to use for input/output streams
65+
* @return An RxJava Single, that will either emit
66+
* a BluetoothSerialDevice or a BluetoothConnectException
67+
*/
68+
public Single<BluetoothSerialDevice> openSerialDevice(String mac, Charset charset) {
5769
if (devices.containsKey(mac)) {
5870
return Single.just(devices.get(mac));
5971
} else {
@@ -63,7 +75,7 @@ public Single<BluetoothSerialDevice> openSerialDevice(String mac) {
6375
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
6476
adapter.cancelDiscovery();
6577
socket.connect();
66-
BluetoothSerialDevice serialDevice = BluetoothSerialDevice.getInstance(mac, socket);
78+
BluetoothSerialDevice serialDevice = BluetoothSerialDevice.getInstance(mac, socket, charset);
6779
devices.put(mac, serialDevice);
6880
return serialDevice;
6981
} catch (Exception e) {

androidBluetoothSerial/src/main/java/com/harrysoft/androidbluetoothserial/BluetoothSerialDevice.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.InputStream;
1010
import java.io.InputStreamReader;
1111
import java.io.OutputStream;
12+
import java.nio.charset.Charset;
1213

1314
import io.reactivex.BackpressureStrategy;
1415
import io.reactivex.Completable;
@@ -22,19 +23,21 @@ public class BluetoothSerialDevice {
2223
private final BluetoothSocket socket;
2324
private final OutputStream outputStream;
2425
private final InputStream inputStream;
26+
private final Charset charset;
2527

2628
@Nullable
2729
private SimpleBluetoothDeviceInterface owner;
2830

29-
private BluetoothSerialDevice(String mac, BluetoothSocket socket, OutputStream outputStream, InputStream inputStream) {
31+
private BluetoothSerialDevice(String mac, BluetoothSocket socket, OutputStream outputStream, InputStream inputStream, Charset charset) {
3032
this.mac = mac;
3133
this.socket = socket;
3234
this.outputStream = outputStream;
3335
this.inputStream = inputStream;
36+
this.charset = charset;
3437
}
3538

36-
static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket) throws IOException {
37-
return new BluetoothSerialDevice(mac, socket, socket.getOutputStream(), socket.getInputStream());
39+
static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket, Charset charset) throws IOException {
40+
return new BluetoothSerialDevice(mac, socket, socket.getOutputStream(), socket.getInputStream(), charset);
3841
}
3942

4043
/**
@@ -44,7 +47,7 @@ static BluetoothSerialDevice getInstance(String mac, BluetoothSocket socket) thr
4447
*/
4548
public Completable send(String message) {
4649
requireNotClosed();
47-
return Completable.fromAction(() -> { if (!closed) outputStream.write(message.getBytes()); });
50+
return Completable.fromAction(() -> { if (!closed) outputStream.write(message.getBytes(charset)); });
4851
}
4952

5053
/**
@@ -54,7 +57,7 @@ public Completable send(String message) {
5457
public Flowable<String> openMessageStream() {
5558
requireNotClosed();
5659
return Flowable.create(emitter -> {
57-
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
60+
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, charset));
5861
while (!emitter.isCancelled() && !closed) {
5962
synchronized (this) {
6063
try {

0 commit comments

Comments
 (0)