Code Monkey home page Code Monkey logo

Comments (3)

zzt93 avatar zzt93 commented on July 30, 2024
  • What the best practice to unsubscribe connection if I want to connect to many devices and communicate ? in Activity#onDestory? or like the following code
Observable<RxBleConnection> connectionSrc = rxBleScanResult
        .getBleDevice()
        .establishConnection(activity, false);

// 6/8/16  when found the device, stop scanning
subscribe.unsubscribe();

// TODO: 6/8/16 when un-subscribe this
Subscription conSubscription = connectionSrc
        .flatMap(new Func1<RxBleConnection, Observable<byte[]>>() {
            @Override
            public Observable<byte[]> call(final RxBleConnection rxBleConnection) {
                return rxBleConnection.readCharacteristic(BLE.BLE_CHAT_UUID);
            }
        })
        .subscribe(new Action1<byte[]>() {
            @Override
            public void call(byte[] bytes) {
                // read bytes              
                **conSubscription.unsubscribe();**
            }
        });

from rxandroidble.

zzt93 avatar zzt93 commented on July 30, 2024
  • read write combined vs simple read then simple write
    The following code is blog's code for read/write combined.
device.establishConnection(context, false)
    .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid)
        .doOnNext(bytes -> {
            // Process read data.
        })
        .flatMap(bytes -> rxBleConnection.writeCharacteristic(characteristicUuid, bytesToWrite))
     ).subscribe(writeBytes -> {
        // Written data.
    });

What is the difference between the following code:

Observable<RxBleConnection> connectionSrc = device.establishConnection(activity, false);             
connectionSrc
    .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUUID))
    .subscribe(characteristicValue -> {
        // Read characteristic value.
    });
connectionSrc
    .flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
    .subscribe(characteristicValue -> {
        // Characteristic value confirmed.
    });

from rxandroidble.

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

The answer is to unsubscribe from the scanning as soon as you're no longer interested in it. For instance if you're interested in connecting to a fixed amount of devices that are meeting your requirements:

CompositeSubscription compositeSubscription = new CompositeSubscription

Subscription scanSubscription = rxBleClient.scanBleDevices()
  .filter(scanResult -> /* filter the devices you're interested in */)
  .take(/* number of devices you want to scan */) // this operator will automatically unsubscribe from scanSubscription once the number of scanResults will be taken
  .map(scanResult -> scanResult.getBleDevice())
  .subscribe(bleDevice -> connectToDevice(bleDevice))
compositeSubscription.add(scanSubscription)

where:

void connectToDevice(RxBleDevice device) {
  Subscription connectFlowSubscription = device.establishConnection(context, false)
    .flatMap(connection -> /* do what you need to do */)
    .subscribe()
  compositeSubscription.add(connectFlowSubscription)
}

and you should clear the compositeSubscription when you're no longer using the app. For instance onPause of the Activity.

from rxandroidble.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.