Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
IvBaranov committed Oct 29, 2018
1 parent a53ebcb commit a77c527
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,31 @@ rxBluetooth.observeDevices()
}));
```

##### Create connection to device
##### Create connection between devices
```java
// Use 00001101-0000-1000-8000-00805F9B34FB for SPP service
// Use 00001101-0000-1000-8000-00805F9B34FB for SPP service
// (ex. Arduino) or use your own generated UUID.
UUID uuid = UUID.fromString("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
// UUID uuid = UUID.fromString("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

rxBluetooth.observeConnectDevice(bluetoothDevice, uuid)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<BluetoothSocket>() {
@Override public void accept(BluetoothSocket socket) throws Exception {
// Connected to the device, do anything with the socket
rxBluetooth.connectAsServer("servername", uuid).subscribe(
new Consumer<BluetoothSocket>() {
@Override public void accept(BluetoothSocket bluetoothSocket) throws Exception {
// Client connected, do anything with the socket
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
// Error occured
// On error
}
});

rxBluetooth.connectAsClient(bluetoothDevice, uuid).subscribe(
new Consumer<BluetoothSocket>() {
@Override public void accept(BluetoothSocket bluetoothSocket) throws Exception {
// Connected to bluetooth device, do anything with the socket
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
// On error
}
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
Expand All @@ -28,6 +29,7 @@
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION_COARSE_LOCATION = 0;
Expand Down Expand Up @@ -192,6 +194,28 @@ private void initEventListeners() {
rxBluetooth.cancelDiscovery();
}
});

rxBluetooth.connectAsServer("servername", UUID.randomUUID()).subscribe(
new Consumer<BluetoothSocket>() {
@Override public void accept(BluetoothSocket bluetoothSocket) throws Exception {

}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {

}
});

rxBluetooth.connectAsClient(bluetoothDevice, UUID.randomUUID()).subscribe(
new Consumer<BluetoothSocket>() {
@Override public void accept(BluetoothSocket bluetoothSocket) throws Exception {

}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {

}
});
}

private void addDevice(BluetoothDevice device) {
Expand Down

0 comments on commit a77c527

Please sign in to comment.