Click here to Skip to main content
13,146,742 members (79,819 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.1K views
5 bookmarked
Posted 24 Sep 2016

BluetoothKit---Android Bluetooth Framework

, 24 Sep 2016
Rate this:
Please Sign up or sign in to vote.
A library allows for easy access to communicate with BluetoothLE devices

Introduction

This library allows for easy access to Bluetooth device scan and connection, support customizing scan policy and solved many android bluetooth inherent compatibility and stability problems refer to Android 4.3 Bluetooth Low Energy unstable

Project: https://github.com/dingjikerbo/BluetoothKit

Requirements

  • minSdkVersion should be not less than 18

  • Permission in AndroidManifest.xml

<code><uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-feature

    android:name="android.hardware.bluetooth_le"

    android:required="true" />
</code>

Usage

1、If you are building with Gradle, simply add the following line to the dependencies section of your build.gradle file:

compile 'com.inuker.bluetooth:library:1.0.6'

2、Create a BluetoothClient as below: 

BluetoothClient mClient = new BluetoothClient(context);

Scan Device

This library support both Bluetooth LE device scan and Classic device scan, you could customize the scan policy as below:

SearchRequest request = new SearchRequest.Builder()
        .searchBluetoothLeDevice(3000, 3)   // scan Bluetooth LE device for 3000ms, 3 times
        .searchBluetoothClassicDevice(5000) // then scan Bluetooth Classic device for 5000ms, 1 time
        .searchBluetoothLeDevice(2000)      // at last scan Bluetooth LE device for 2000ms
        .build();

mClient.search(request, new SearchResponse() {
    @Override
    public void onSearchStarted() {

    }

    @Override
    public void onDeviceFounded(SearchResult device) {

    }

    @Override
    public void onSearchStopped() {

    }

    @Override
    public void onSearchCanceled() {

    }
});

You could stop the whole scan by just one line:

mClient.stopSearch();

Bluetooth LE Connection

● Connect

BleGattProfile contains all service and characteristic uuid.

mClient.connect(MAC, new BleConnectResponse() {
    @Override
    public void onResponse(int code, BleGattProfile profile) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Connect Status

<code>mClient.registerConnectStatusListener(MAC, mBleConnectStatusListener);

private final BleConnectStatusListener mBleConnectStatusListener = new BleConnectStatusListener() {

    @Override
    public void onConnectStatusChanged(int status) {
        if (status == STATUS_CONNECTED) {

        } else if (status == STATUS_DISCONNECTED) {

        }
    }
};

mClient.unregisterConnectStatusListener(MAC, mBleConnectStatusListener);
</code>

● Disconnect

mClient.disconnect(MAC);

● Read Characteristic

mClient.read(MAC, serviceUUID, characterUUID, new BleReadResponse() {
    @Override
    public void onResponse(int code, byte[] data) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Write Characteristic

The data to write should be no more than 20 bytes.

mClient.write(MAC, serviceUUID, characterUUID, bytes, new BleWriteResponse() {
    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

// with WRITE_TYPE_NO_RESPONSE
mClient.writeNoRsp(MAC, serviceUUID, characterUUID, bytes, new BleWriteResponse() {
    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Open Notify

mClient.notify(MAC, serviceUUID, characterUUID, new BleNotifyResponse() {
    @Override
    public void onNotify(UUID service, UUID character, byte[] value) {

    }

    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Close Notify

mClient.unnotify(MAC, serviceUUID, characterUUID, new BleUnnotifyResponse() {
    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Read Rssi

mClient.readRssi(MAC, new BleReadRssiResponse() {
    @Override
    public void onResponse(int code, Integer rssi) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});

● Refresh Cache

Refresh cache at the beginning of next connection.

mClient.refreshCache(MAC);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionArticle concerns Pin
DavidCrow29-Sep-16 13:03
memberDavidCrow29-Sep-16 13:03 
QuestionCant understand Pin
M Badhri narayanan Mbn27-Sep-16 6:11
memberM Badhri narayanan Mbn27-Sep-16 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 24 Sep 2016
Article Copyright 2016 by Member 10165288
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid