Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Promisified BLE Client

Promisified BLE Client

Introduce Bletia: Promisified BLE Client for Android
potatotips #21 http://connpass.com/event/18732/
Bletia https://github.com/izumin5210/Bletia

Masayuki Izumi

September 16, 2015
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1. Promisified BLE Client
    Masayuki IZUMI - @izumin5210
    potatotips #21
    https://www.flickr.com/photos/dewittkower/4641701143/

    View Slide

  2.  Rekimoto Lab. at the University of Tokyo
    (2008-2015: Akashi-NCT)
     Enginner at Wantedly, Inc.
    (2014.9-2015.2: Dmetlabel, Inc.)
    2

    View Slide

  3. 2
    Ruby
    JavaScript
    Android
    Design
    pry(main) > izumin.skill_ratio

    View Slide

  4. http://konashi.ux-xu.com/
    https://github.com/YUKAI/konashi-android-sdk/
    https://www.flickr.com/photos/[email protected]/8524872002

    View Slide

  5. \ 前回のラブライブ! /
    kyobashi.dex

    View Slide

  6. https://speakerdeck.com/izumin5210/tsurai-ble-on-android

    View Slide

  7. Bluetooth GATT
    GATT Server
    Service
    Service

    View Slide

  8. Bluetooth GATT
    GATT Server
    Service
    Service
    Characteristic

    View Slide

  9. BLE on Android
    Central
    wants data
    Peripheral
    has data

    View Slide

  10. BLE on Android
    Central
    wants data
    Peripheral
    has data
    write value
    read value

    View Slide

  11. Write characteristic value
    Central
    wants data
    Peripheral
    has data
    write value

    View Slide

  12. Write characteristic value
    Central
    wants data
    Peripheral
    has data
    write value
    BluetoothGatt#writeCharacteristic()

    View Slide

  13. Write characteristic value
    Central
    wants data
    Peripheral
    has data
    write value
    BluetoothGatt#writeCharacteristic()
    (async)

    View Slide

  14. Write characteristic value
    Central
    wants data
    Peripheral
    has data
    write value
    BluetoothGatt#writeCharacteristic()
    BluetoothGattCallback#onCharacteristicWrite()
    (async)

    View Slide

  15. When is the callback registered ?
    public BluetoothGatt connectGatt (
    Context context,
    boolean autoConnect,
    BluetoothGattCallback callback
    )

    View Slide

  16. When is the callback registered ?
    public BluetoothGatt connectGatt (
    Context context,
    boolean autoConnect,
    BluetoothGattCallback callback
    )
    We can register only 1 instance...

    View Slide

  17. Handle callback
    public void onCharacteristicWrite (
    BluetoothGatt gatt,
    BluetoothGattCharacteristic characteristic,
    int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
    // check characteristic instance, handling...
    } else {
    // error handling...
    }
    }

    View Slide

  18. Handle callback
    public void onCharacteristicWrite (
    BluetoothGatt gatt,
    BluetoothGattCharacteristic characteristic,
    int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
    // check characteristic instance, handling...
    } else {
    // error handling...
    }
    }

    View Slide

  19. if BG has promise like api ...
    gatt.writeCharacteristic(characteristic)
    .then(
    // call when the operation successfully
    )
    .fail(
    // call when the operation failure
    )

    View Slide

  20. Bletia
    https://github.com/izumin5210/Bletia

    View Slide

  21. Download
    dependencies {
    compile 'info.izumin.android:bletia:1.2.2'
    }

    View Slide

  22. Instantiate / Connect
    // Pass application context to constructor.
    Bletia bletia = new Bletia(context);
    // BluetoothDevice device
    bletia.connect(device);

    View Slide

  23. Instantiate / Connect
    bletia.writeCharacteristic(characteristic);

    View Slide

  24. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .then(new DoneCallback() {
    @Override
    public void onDone(BluetoothGattCharacteristic result) {
    // Call when the request was successfully.
    }
    });

    View Slide

  25. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .then(new DoneCallback() {
    @Override
    public void onDone(BluetoothGattCharacteristic result) {
    // Call when the request was successfully.
    }
    });

    View Slide

  26. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .then(new DoneCallback() {
    @Override
    public void onDone(BluetoothGattCharacteristic result) {
    // Call when the request was successfully.
    }
    });

    View Slide

  27. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .fail(new FailCallback() {
    @OVerride
    public void onFail(BletiaException result) {
    // Call when the request was failure.
    }
    });

    View Slide

  28. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .fail(new FailCallback() {
    @OVerride
    public void onFail(BletiaException result) {
    // Call when the request was failure.
    }
    });

    View Slide

  29. Write characteristic
    bletia.writeCharacteristic(characteristic)
    .fail(new FailCallback() {
    @OVerride
    public void onFail(BletiaException result) {
    // Call when the request was failure.
    }
    });

    View Slide

  30. Support promisified operations (v1.2.2)
    * readCharacteristic() / writeCharacteristic()
    * readDescriptor() / writeDescriptor()
    * enableNotification()
    * readRemoteRssi()

    View Slide

  31. Conclusion
    Saiko no Experiense
    with
    Promise

    View Slide