Coder Social home page Coder Social logo

aqua30 / android-bluetooth-low-energy-wrapper-ble Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 12.0 158 KB

This project is created on top of Google's BLE project but made it more simpler in use. A module or wrapper which lets you not worry about any handling of BLE related work. Set up callbacks and get started.

Java 100.00%
android bluetooth ble bluetooth-low-energy bluetooth-arduino bluetooth-controller bluetooth-library communication

android-bluetooth-low-energy-wrapper-ble's Introduction

Android-Bluetooth-Low-Energy-Wrapper-BLE

This sample project demostrate how an Android app communicate with a Bluetooth Low Energy Device.

Purpose of BLE Wrapper

While integrating the BLE device with Android app, the overall documentation was clustering the code. So, it was felt that it would be better to create a callback mechanism wherein we just set up the BLE device with a single object. Because its a wrapper, it mainly follows the google documentation and has some added scenarios over it.

What it simplifies

BluetoothManager is the object that the app deals with. It helps in handling the following

  • handles the permission for Android device running both above and below Marshmallow
  • detects the device bluetooth state (ON/OFF)
  • automatically saves the last connected device and we can retrieve it anytime as
Device lastDevice = new Device(context);
Log.d("last connected device", String.format("name: %s, "address: %s", lastDevice.getDeviceName(), lastDevice.getDeviceAddress()));
  • a clean pop for scan devices and connect to the selected device.
  • creates a session for a connected device and destroy it when not in use or not communicating.
  • returns a list of paired devices
  • returns if a device is connected or not.

Easy set up

Create an instance of BluetoothManager object and get started as

bluetoothManager = new BluetoothController(this);
bluetoothManager.setGoogleApiClient(mGoogleApiClient);

/* must call onActivityResult for bluetoothManager object to set the required results */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   bluetoothManager.onActivityResult(requestCode, resultCode, data);
}

Callbacks provided

The wrapper provides callbacks for managing the various actions to get running with a BLE device.

Permission Callbacks: Set this to observe any state related to bluetooth permission, location permission(for device > M), device bluetooth state and do we need to ask for permission, set the ConnectionCallback as

bluetoothManager.setConnectionCallbacks(this);

@Override
public void askLocationPermission() {}

@Override
public void gpsState(int connectionState) {}

@Override
public void bleConnectionState(int connectionState) {}

@Override
public void permissionState(int permissionState) {}

@Override
public void bleDeviceConnectionState(int deviceConnectionState) {}

Communication Callback: For to receive data in your parent Activity/Fragment, set

bluetoothManager.setDataCallbacks(this);

@Override
public void onDataReceived(String data) {
  log("Data received: "+ data);
}

Discovery Callback: Mainly used in ScanDeviceFragment, can be used to find BLE devices and connect to them.

bluetoothManager.setDiscoveryCallbacks(this);

@Override
public void onDeviceDiscovered(BluetoothDevice device) {}

@Override
public void onDeviceDiscoveryStopped() {}

ConnectedDevice Callbacks: Set this callback to know if device is connected or disconnected during runtime

bluetoothManager.setConnectedDeviceStateCallbacks(this);

@Override
public void connectedDeviceState(int connectedDeviceState) {}

Optional feature

Bluetooth State Detection: Set this to know bluetooth on device is turned ON or OFF at runtime. This is a broadcast receiver with defined codes and must be unregistered in onPause().

@Override
protected void onResume() {
  super.onResume();
  bluetoothManager.registerStateDetection();
}

Scan Device Fragment A fragment which is just required to be called and it'll handle the scan itself. It mainly scans for 10 seconds and then it stops scanning but we can rescan.

ScanDeviceFragment scanDeviceFragment = new ScanDeviceFragment();
scanDeviceFragment.setScanDeviceListener(new ScanDeviceFragment.ScanDeviceListener() {
    @Override
    public void onDeviceSelected(BluetoothDevice device) {
        /* once a device is connected, we can connect to it and stop scanning */
    });
scanDeviceFragment.show(getFragmentManager(), "ScanDeviceFragment");

android-bluetooth-low-energy-wrapper-ble's People

Contributors

aqua30 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

android-bluetooth-low-energy-wrapper-ble's Issues

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.