@@ -16,7 +16,7 @@ allprojects {
1616- ** App level ` build.gradle ` **
1717``` gradle
1818dependencies {
19- implementation 'com.github.harry1453:android-bluetooth-serial:v1.0 '
19+ implementation 'com.github.harry1453:android-bluetooth-serial:v1.1 '
2020
2121 // RxJava is also required.
2222 implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
@@ -65,54 +65,39 @@ import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice;
6565```
6666
6767``` JAVA
68- private BluetoothSerialDevice device ;
68+ private SimpleBluetoothDeviceInterface deviceInterface ;
6969
7070private void connectDevice(String mac) {
7171 bluetoothManager. openSerialDevice(mac)
7272 .subscribeOn(Schedulers . io())
7373 .observeOn(AndroidSchedulers . mainThread())
74- .subscribe(this :: onConnected, this :: onConnectError);
75- }
76-
77- private void sendMessage(String message) {
78- if (device != null ) { // Check we are connected
79- serialDevice. send(message)
80- .subscribeOn(Schedulers . io())
81- .observeOn(AndroidSchedulers . mainThread())
82- .subscribe(this :: onSentMessage, this :: onSendMessageError);
83- }
74+ .subscribe(this :: onConnected, this :: onError);
8475}
8576
8677private void onConnected(BluetoothSerialDevice connectedDevice) {
8778 // You are now connected to this device!
8879 // Here you may want to retain an instance to your device:
89- this . device = connectedDevice;
80+ deviceInterface = connectedDevice. toSimpleDeviceInterface() ;
9081
91- // Open a message stream:
92- serialDevice. openMessageStream()
93- .subscribeOn(Schedulers . io())
94- .observeOn(AndroidSchedulers . mainThread())
95- .subscribe(this :: onMessageReceived, this :: onReceiveMessageError));
82+ // Listen to bluetooth events
83+ deviceInterface. setListeners(this :: onMessageReceived, this :: onMessageSent, this :: onError);
9684
97- // Let's send a message using our send function:
98- sendMessage(" Hello world!" );
85+ // Let's send a message:
86+ deviceInterface. sendMessage(" Hello world!" );
87+ }
88+
89+ private void onMessageSent(String message) {
90+ // We sent a message! Handle it here.
91+ Toast . makeText(context, " Sent a message! Message was: " + message, Toast . LENGTH_LONG ). show(); // Replace context with your context instance.
9992}
10093
10194private void onMessageReceived(String message) {
10295 // We received a message! Handle it here.
10396 Toast . makeText(context, " Received a message! Message was: " + message, Toast . LENGTH_LONG ). show(); // Replace context with your context instance.
10497}
10598
106- private void onConnectError(Throwable error) {
107- // Handle the connection error
108- }
109-
110- private void onReceiveMessageError(Throwable error) {
111- // Handle the receiving error
112- }
113-
114- private void onSendMessageError(Throwable error) {
115- // Handle the sending error
99+ private void onError(Throwable error) {
100+ // Handle the error
116101}
117102```
118103
@@ -124,6 +109,8 @@ private void onSendMessageError(Throwable error) {
124109bluetoothManager. closeDevice(macAddress); // Close by mac
125110// OR
126111bluetoothManager. closeDevice(connectedDevice); // Close by device instance
112+ // OR
113+ bluetoothManager. closeDevice(deviceInterface); // Close by interface instance
127114
128115// Disconnect all devices
129116bluetoothManager. close();
0 commit comments