• Stars
    star
    970
  • Rank 47,174 (Top 1.0 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BLE Framework. Based on Bluetooth 4.0. Based on callback. Extremely simple! Communication with BluetoothLE(BLE) device as easy as HTTP communication. Android低功耗蓝牙便捷操作框架,基于回调,完成蓝牙设备交互就像发送网络请求一样简单。

LiteBle: Android Bluetooth Framework

Extremely simple! Based on callback. Communication with BluetoothLE(BLE) device as easy as HTTP communication. One Device, One connection, One LiteBluetooth Instance.

But One connection can has many callback: One LiteBluetooth Instance can add many BluetoothGattCallback.

##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. get state of litebluetooth

BleLog.i(TAG, "liteBluetooth.getConnectionState: " + liteBluetooth.getConnectionState());
BleLog.i(TAG, "liteBluetooth isInScanning: " + liteBluetooth.isInScanning());
BleLog.i(TAG, "liteBluetooth isConnected: " + liteBluetooth.isConnected());
BleLog.i(TAG, "liteBluetooth isServiceDiscoered: " + liteBluetooth.isServiceDiscoered());
if (liteBluetooth.getConnectionState() >= LiteBluetooth.STATE_CONNECTING) {
    BleLog.i(TAG, "lite bluetooth is in connecting or connected");
}
if (liteBluetooth.getConnectionState() == LiteBluetooth.STATE_SERVICES_DISCOVERED) {
    BleLog.i(TAG, "lite bluetooth is in connected, services have been found");
}

###4. add(remove) new callback to an existing connection.

/**
 * add(remove) new callback to an existing connection.
 * One Device, One {@link LiteBluetooth}.
 * But one device( {@link LiteBluetooth}) can add many callback {@link BluetoothGattCallback}
 *
 * {@link LiteBleGattCallback} is a extension of {@link BluetoothGattCallback}
 */
private void addNewCallbackToOneConnection() {
    BluetoothGattCallback liteCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {}

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt,
                                          BluetoothGattCharacteristic characteristic, int status) {
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {}
    };

    if (liteBluetooth.isConnectingOrConnected()) {
        liteBluetooth.addGattCallback(liteCallback);
        liteBluetooth.removeGattCallback(liteCallback);
    }
}

###5. refresh bluetooth device cache

liteBluetooth.refreshDeviceCache();

###6. close connection

if (liteBluetooth.isConnectingOrConnected()) {
    liteBluetooth.closeBluetoothGatt();
}

###7. 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);
             }
         });

###8. 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);
             }
         });

###9. 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);
             }
         });

###10. 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);
             }
         });

###11. 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);
             }
         });

###12. 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, See The Sample


Website : http://litesuits.com

Email : [email protected]

QQgroup : 42960650 , 47357508

More Repositories

1

android-common

Android Common Utils or Helper. Such as Log, Averager, Base64, Check, FlashLight, KeyguardLock, LogReader, Network, SilentInstaller, TimeAverager, TimeCounter, Toastor, WakeLock, ScreenReceiver, SmsReceiver, PhoneReceiver, NotificationService, AndroidUtil, AppUtil, BitmapUtil, ByteUtil, ClassUtil, DialogUtil, FieldUtil, FileUtil, HexUtil, MD5Util, NotificationUtil, NumberUtil, PackageUtil, RandomUtil, ShellUtil, TelephoneUtil, VibrateUtil, IOUtils, FileUtils, AsyncExecutor, etc. 通用性强,纯洁简单,体积不到50K!其中包括bitmap处理,文件操作,加密存储器,shell命令,静默安装,计数器,均值器,吐司,日志,校验,提示,网络监测等基础功能,以及一些Base64、MD5、Hex、Byte、Number、Dialog、Filed、Class、Package、Telephone、Random等工具类。
Java
3,645
star
2

android-lite-orm

LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.
Java
1,491
star
3

android-lite-http

LiteHttp is a simple, intelligent and flexible HTTP framework for Android. With LiteHttp you can make HTTP request with only one line of code! It could convert a java model to the parameter and rander the response JSON as a java model intelligently.
Java
830
star
4

android-lite-async

Crossbow(LiteAsync) is an ameliorative, enhanced AsyncTask for Android. LiteAsync provides SimpleTask, SafeTask, CachedTask, etc, for rapid development. More convenient is, it has a TaskExcutor that excutes ordered, cyclicbarrier, delayed and timer Task.
Java
456
star
5

android-lite-go

LiteGo is a Java-based asynchronous concurrency library. It has a smart executor, which can be freely set the maximum number of concurrent at same time , and the number of threads in waiting queue. It can also set waiting policies and overload strategies.
Java
189
star
6

android-lite-auto

lite your android ! the code is on the way~
33
star
7

useful-tools

Tools and Services for developer, entrepreneurs, tester, product manager, project manager.
31
star
8

android-lite-tidy

Android Annotation Framework.
Java
9
star
9

android-sms-message

一个简单的短信接受、转发器
Java
6
star
10

for-test

this project just used for test and some demo.
Java
4
star
11

find-best

To find a more suitable company. To find a more suitable developer. 寻找到更合适的公司。寻找到更合适的开发者。
4
star
12

android-lite-base

some base/views compnent
3
star
13

litesuits.github.com

hello world
HTML
1
star
14

android-lite-sdk

I hope that LiteSdk is a series of best practices, and can support the rapid development of Android App. 我希望LiteSdk是一套通用的最佳开发实践的集合,同时可以支撑Android App的快速开发。
1
star