99import java .io .InputStream ;
1010import java .io .InputStreamReader ;
1111import java .io .OutputStream ;
12+ import java .nio .charset .Charset ;
1213
1314import io .reactivex .BackpressureStrategy ;
1415import 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