diff --git a/README.md b/README.md index f5b7022..0bbd5d0 100644 --- a/README.md +++ b/README.md @@ -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() { - @Override public void accept(BluetoothSocket socket) throws Exception { - // Connected to the device, do anything with the socket +rxBluetooth.connectAsServer("servername", uuid).subscribe( + new Consumer() { + @Override public void accept(BluetoothSocket bluetoothSocket) throws Exception { + // Client connected, do anything with the socket } }, new Consumer() { @Override public void accept(Throwable throwable) throws Exception { - // Error occured + // On error + } + }); + +rxBluetooth.connectAsClient(bluetoothDevice, uuid).subscribe( + new Consumer() { + @Override public void accept(BluetoothSocket bluetoothSocket) throws Exception { + // Connected to bluetooth device, do anything with the socket + } + }, new Consumer() { + @Override public void accept(Throwable throwable) throws Exception { + // On error } }); ``` diff --git a/app/src/main/java/com/github/ivbaranov/rxbluetooth/example/MainActivity.java b/app/src/main/java/com/github/ivbaranov/rxbluetooth/example/MainActivity.java index 67370dd..e730db8 100644 --- a/app/src/main/java/com/github/ivbaranov/rxbluetooth/example/MainActivity.java +++ b/app/src/main/java/com/github/ivbaranov/rxbluetooth/example/MainActivity.java @@ -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; @@ -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; @@ -192,6 +194,28 @@ private void initEventListeners() { rxBluetooth.cancelDiscovery(); } }); + + rxBluetooth.connectAsServer("servername", UUID.randomUUID()).subscribe( + new Consumer() { + @Override public void accept(BluetoothSocket bluetoothSocket) throws Exception { + + } + }, new Consumer() { + @Override public void accept(Throwable throwable) throws Exception { + + } + }); + + rxBluetooth.connectAsClient(bluetoothDevice, UUID.randomUUID()).subscribe( + new Consumer() { + @Override public void accept(BluetoothSocket bluetoothSocket) throws Exception { + + } + }, new Consumer() { + @Override public void accept(Throwable throwable) throws Exception { + + } + }); } private void addDevice(BluetoothDevice device) {