From 5e23c5ec636c990bcc2013f83f14c346695158d9 Mon Sep 17 00:00:00 2001 From: Gyurme Date: Wed, 30 May 2018 20:23:23 +1200 Subject: [PATCH] Added a method that allows you to fetch the list of supported UUIDs that can be used to connect to a device. Useful for connecting to devices that may not support the standard UUIDs. --- .../ivbaranov/rxbluetooth/RxBluetooth.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java index 4c617a8..aee9f4a 100644 --- a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java +++ b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java @@ -26,6 +26,9 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Build; +import android.os.Parcelable; +import android.os.RemoteException; +import android.support.annotation.RequiresApi; import android.text.TextUtils; import com.github.ivbaranov.rxbluetooth.events.AclEvent; import com.github.ivbaranov.rxbluetooth.events.BondStateEvent; @@ -574,4 +577,46 @@ public Observable observeAclEvent() { } }); } + + /** + * Performs a service discovery and fetches a list of UUIDs that can be used to connect to {@link BluetoothDevice} + * + * @param bluetoothDevice bluetooth device to connect + * @return RxJava Observable with an array of Device UUIDs that can be used to connect to the device + */ + + @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) + public Observable observeFetchDeviceUuids(final BluetoothDevice bluetoothDevice) { + final IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothDevice.ACTION_UUID); + + return Observable.defer(new Callable>() { + @Override public ObservableSource call() { + return Observable.create(new ObservableOnSubscribe() { + @Override + public void subscribe(@NonNull final ObservableEmitter emitter) { + final BroadcastReceiver receiver = new BroadcastReceiver() { + @Override public void onReceive(Context context, Intent intent) { + Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID); + emitter.onNext(uuids); + } + }; + + context.registerReceiver(receiver, filter); + + emitter.setDisposable(new MainThreadDisposable() { + @Override + protected void onDispose() { + context.unregisterReceiver(receiver); + dispose(); + } + }); + + bluetoothDevice.fetchUuidsWithSdp(); + + } + }); + } + }); + } }