GithubHelp home page GithubHelp logo

aeatho / android-lite-bluetoothle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from litesuits/android-lite-bluetoothle

0.0 2.0 0.0 216 KB

BLE:Android Bluetooth 4.0,低功耗蓝牙便捷操作框架

Home Page: http://litesuits.com/

Java 100.00%

android-lite-bluetoothle's Introduction

LiteBle: Android Bluetooth Framework

Extremely simple! Based on callback.

Communication with BluetoothLE(BLE) device as easy as HTTP communication.

##Usage

###1. scan device

private static int TIME_OUT_SCAN = 10000;
liteBluetooth.startLeScan(new PeriodScanCallback(TIME_OUT_SCAN) {
    @Override
    public void onScanTimeout() {
        dialogShow(TIME_OUT_SCAN + " Millis Scan Timeout! ");
    }

    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        BleLog.i(TAG, "device: " + device.getName() + "  mac: " + device.getAddress()
                      + "  rssi: " + rssi + "  scanRecord: " + Arrays.toString(scanRecord));
    }
});

###2. scan and connect

private static String MAC = "00:00:00:AA:AA:AA";
liteBluetooth.scanAndConnect(MAC, false, new BleGattCallback() {

    @Override
    public void onConnectSuccess(BluetoothGatt gatt, int status) {
        // discover services !
        gatt.discoverServices();
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        BluetoothUtil.printServices(gatt);
        dialogShow(MAC + " Services Discovered SUCCESS !");
    }

    @Override
    public void onConnectFailure(BleException exception) {
        bleExceptionHandler.handleException(exception);
        dialogShow(MAC + " Services Discovered FAILURE !");
    }
});

###3. write data to characteritic

LiteBleConnector connector = liteBluetooth.newBleConnector();
connector.withUUIDString(UUID_SERVICE, UUID_CHAR_WRITE, null)
         .writeCharacteristic(new byte[]{1, 2, 3}, new BleCharactCallback() {
             @Override
             public void onSuccess(BluetoothGattCharacteristic characteristic) {
                 BleLog.i(TAG, "Write Success, DATA: " + Arrays.toString(characteristic.getValue()));
             }

             @Override
             public void onFailure(BleException exception) {
                 BleLog.i(TAG, "Write failure: " + exception);
                 bleExceptionHandler.handleException(exception);
             }
         });

###4. write data to descriptor

LiteBleConnector connector = liteBluetooth.newBleConnector();
connector.withUUIDString(UUID_SERVICE, UUID_CHAR_WRITE, UUID_DESCRIPTOR_WRITE)
         .writeDescriptor(new byte[]{1, 2, 3}, new BleDescriptorCallback() {
             @Override
             public void onSuccess(BluetoothGattDescriptor descriptor) {
                 BleLog.i(TAG, "Write Success, DATA: " + Arrays.toString(descriptor.getValue()));
             }

             @Override
             public void onFailure(BleException exception) {
                 BleLog.i(TAG, "Write failure: " + exception);
                 bleExceptionHandler.handleException(exception);
             }
         });

###5. read data from characteritic

LiteBleConnector connector = liteBluetooth.newBleConnector();
connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, null)
         .readCharacteristic(new BleCharactCallback() {
             @Override
             public void onSuccess(BluetoothGattCharacteristic characteristic) {
                 BleLog.i(TAG, "Read Success, DATA: " + Arrays.toString(characteristic.getValue()));
             }

             @Override
             public void onFailure(BleException exception) {
                 BleLog.i(TAG, "Read failure: " + exception);
                 bleExceptionHandler.handleException(exception);
             }
         });

###6. enable notification of characteristic

LiteBleConnector connector = liteBluetooth.newBleConnector();
connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, null)
         .enableCharacteristicNotification(new BleCharactCallback() {
             @Override
             public void onSuccess(BluetoothGattCharacteristic characteristic) {
                 BleLog.i(TAG, "Notification characteristic Success, DATA: " + Arrays
                         .toString(characteristic.getValue()));
             }

             @Override
             public void onFailure(BleException exception) {
                 BleLog.i(TAG, "Notification characteristic failure: " + exception);
                 bleExceptionHandler.handleException(exception);
             }
         });

###7. enable notification of descriptor

LiteBleConnector connector = liteBluetooth.newBleConnector();
connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, UUID_DESCRIPTOR_READ)
         .enableDescriptorNotification(new BleDescriptorCallback() {
             @Override
             public void onSuccess(BluetoothGattDescriptor descriptor) {
                 BleLog.i(TAG,
                         "Notification descriptor Success, DATA: " + Arrays.toString(descriptor.getValue()));
             }

             @Override
             public void onFailure(BleException exception) {
                 BleLog.i(TAG, "Notification descriptor failure : " + exception);
                 bleExceptionHandler.handleException(exception);
             }
         });

###8. read RSSI of device

liteBluetooth.newBleConnector()
             .readRemoteRssi(new BleRssiCallback() {
                 @Override
                 public void onSuccess(int rssi) {
                     BleLog.i(TAG, "Read Success, rssi: " + rssi);
                 }

                 @Override
                 public void onFailure(BleException exception) {
                     BleLog.i(TAG, "Read failure : " + exception);
                     bleExceptionHandler.handleException(exception);
                 }
             });

##More Detail


Website : http://litesuits.com

Email : [email protected]

QQgroup : 47357508 , 42960650

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.