Giter Site home page Giter Site logo

rxbluetooth's Introduction

RxBluetooth

Build Status

Android reactive bluetooth library. Basically, RxBluetooth is just wrapper around android BluetoothAdapter, so first of all the Bluetooth developer guide should be read.

RxBluetooth is in early-stage. There is a lot of missing stuff. Feel free to contribute.

Full documentation

Usage

  1. Declare permissions:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  2. Create RxBluetooth instance.

  3. Check it:

    // check if bluetooth is supported on your hardware
    if  (!rxBluetooth.isBluetoothAvailable()) {
       // handle the lack of bluetooth support
    } else {
       // check if bluetooth is currently enabled and ready for use
       if (!rxBluetooth.isBluetoothEnabled()) { 
          // to enable blutooth via startActivityForResult()
          rxBluetooth.enableBluetooth(this, REQUEST_ENABLE_BT);
       } else {
          // you are ready
       }
    }
  4. Have fun.

  5. Make sure you are unsubscribing and stopping discovery OnDestroy():

    if (rxBluetooth != null) {
          rxBluetooth.cancelDiscovery();
        }
    unsubscribe(rxBluetoothSubscription);
Observing devices
rxBluetooth.observeDevices()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<BluetoothDevice>() {
        @Override public void call(BluetoothDevice bluetoothDevice) {
          //
        }
      });
Create connection to device
// 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");

rxBluetooth.observeConnectDevice(bluetoothDevice, uuid)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.io())
      .subscribe(new Action1<BluetoothSocket>() {
        @Override public void call(BluetoothSocket socket) {
          // Connected to the device, do anything with the socket 
        }
      }, new Action1<Throwable>() {
       @Override public void call(Throwable throwable) {
         // Error occured
       }
     });
Observing discovery state

To observe just ACTION_DISCOVERY_STARTED:

rxBluetooth.observeDiscovery()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
      .subscribe(new Action1<String>() {
        @Override public void call(String action) {
          //
        }
      });

To observe both ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED:

rxBluetooth.observeDiscovery()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_STARTED, BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
      .subscribe(new Action1<String>() {
        @Override public void call(String action) {
          //
        }
      });
Observing bluetooth state
rxBluetooth.observeBluetoothState()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.STATE_ON))
      .subscribe(new Action1<Integer>() {
        @Override public void call(Integer integer) {
          //
        }
      });

You can observe single or multiple states:

BluetoothAdapter.STATE_OFF
BluetoothAdapter.STATE_TURNING_ON
BluetoothAdapter.STATE_ON
BluetoothAdapter.STATE_TURNING_OFF
Observing scan mode
rxBluetooth.observeScanMode()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE))
      .subscribe(new Action1<Integer>() {
        @Override public void call(Integer integer) {
          //
        }
      });

You can observe single or multiple scan modes:

BluetoothAdapter.SCAN_MODE_NONE
BluetoothAdapter.SCAN_MODE_CONNECTABLE
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE
Getting the profile proxy object
rxBluetooth.observeBluetoothProfile(myProfile)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<ServiceEvent>() {
        @Override public void call(ServiceEvent serviceEvent) {
          switch (serviceEvent.getState()) {
           case CONNECTED:
                BluetoothProfile bluetoothProfile = serviceEvent.getBluetoothProfile();
                List<BluetoothDevice> devices = bluetoothProfile.getConnectedDevices();                        
                for ( final BluetoothDevice dev : devices ) {
                  //..
                }
                break;
           case DISCONNECTED:
                //serviceEvent.getBluetoothProfile() returns null
                break;
            }
          }
        });

myProfile can be one of BluetoothProfile.HEALTH, BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.GATT or BluetoothProfile.GATT_SERVER

Clients should close profile proxy when they are no longer using the proxy obtained from observeBluetoothProfile:

rxBluetooth.closeProfileProxy(int profile, BluetoothProfile proxy);
Observing device state

To observe the current device state, you can receive the ConnectionStateEvent which provides the state, previous state, and BluetoothDevice.

rxBluetooth.observeConnectionState()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<ConnectionStateEvent>() {
        @Override public void call(ConnectionStateEvent event) {
          switch (event.getState()) {
            case BluetoothAdapter.STATE_DISCONNECTED:
                // device disconnected
                break;
            case BluetoothAdapter.STATE_CONNECTING:
                // device connecting
                break;
            case BluetoothAdapter.STATE_CONNECTED:
                // device connected
                break;
            case BluetoothAdapter.STATE_DISCONNECTING:
                // device disconnecting
                break;
          }
        }
      });

Possible states are:

BluetoothAdapter.STATE_DISCONNECTED
BluetoothAdapter.STATE_CONNECTING
BluetoothAdapter.STATE_CONNECTED
BluetoothAdapter.STATE_DISCONNECTING
Observing device bond state

To observe the bond state of devices, you can receive the BondStateEvent which provides the state, previous state, and BluetoothDevice.

rxBluetooth.observeBondState()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<BondStateEvent>() {
        @Override public void call(BondStateEvent event) {
          switch (event.getState()) {
            case BluetoothDevice.BOND_NONE:
                // device unbonded
                break;
            case BluetoothDevice.BOND_BONDING:
                // device bonding
                break;
            case BluetoothDevice.BOND_BONDED:
                // device bonded
                break;
          }
        }
      });

Possible states are:

BluetoothDevice.BOND_NONE
BluetoothDevice.BOND_BONDING
BluetoothDevice.BOND_BONDED

Read and Write with BluetoothSocket

After creating a connection to the device, you can use BluetoothConnection class to read and write with its socket.

Read:
BluetoothConnection bluetoothConnection = new BluetoothConnection(bluetoothSocket);

// Observe every byte received
bluetoothConnection.observeByteStream()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Action1<Byte>() {
      @Override public void call(Byte aByte) {
        // This will be called every single byte received
      }
    }, new Action1<Throwable>() {
      @Override public void call(Throwable throwable) {
        // Error occured
      }
    });

// Or just observe string
bluetoothConnection.observeStringStream()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Action1<String>() {
      @Override public void call(String string) {
        // This will be called every string received
      }
    }, new Action1<Throwable>() {
      @Override public void call(Throwable throwable) {
        // Error occured
      }
    });
Write:
bluetoothConnection.send("Hello"); // String
bluetoothConnection.send("There".getBytes()); // Array of bytes

Download

compile 'com.github.ivbaranov:rxbluetooth:0.1.5'

Snapshots of the development version are available in Sonatype's snapshots repository.

Contributing

Make sure you use SquareAndroid code style. (https://github.com/square/java-code-styles)

Create a branch for each feature.

Developed By

Ivan Baranov

License

Copyright 2015 Ivan Baranov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

rxbluetooth's People

Contributors

ivbaranov avatar qwildz avatar robholmes avatar

Watchers

James Cloos avatar Dharma Kshetri avatar

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.