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
privatestaticintTIME_OUT_SCAN = 10000; liteBluetooth.startLeScan(newPeriodScanCallback(TIME_OUT_SCAN){@OverridepublicvoidonScanTimeout(){dialogShow(TIME_OUT_SCAN + " Millis Scan Timeout! ")} @OverridepublicvoidonLeScan(BluetoothDevicedevice, intrssi, byte[] scanRecord){BleLog.i(TAG, "device: " + device.getName() + " mac: " + device.getAddress() + " rssi: " + rssi + " scanRecord: " + Arrays.toString(scanRecord))} });###2. scan and connect
privatestaticStringMAC = "00:00:00:AA:AA:AA"; liteBluetooth.scanAndConnect(MAC, false, newBleGattCallback(){@OverridepublicvoidonConnectSuccess(BluetoothGattgatt, intstatus){// discover services !gatt.discoverServices()} @OverridepublicvoidonServicesDiscovered(BluetoothGattgatt, intstatus){BluetoothUtil.printServices(gatt); dialogShow(MAC + " Services Discovered SUCCESS !")} @OverridepublicvoidonConnectFailure(BleExceptionexception){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} */privatevoidaddNewCallbackToOneConnection(){BluetoothGattCallbackliteCallback = newBluetoothGattCallback(){@OverridepublicvoidonConnectionStateChange(BluetoothGattgatt, intstatus, intnewState){} @OverridepublicvoidonCharacteristicChanged(BluetoothGattgatt, BluetoothGattCharacteristiccharacteristic){} @OverridepublicvoidonCharacteristicWrite(BluetoothGattgatt, BluetoothGattCharacteristiccharacteristic, intstatus){} @OverridepublicvoidonReadRemoteRssi(BluetoothGattgatt, intrssi, intstatus){} }; 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
LiteBleConnectorconnector = liteBluetooth.newBleConnector(); connector.withUUIDString(UUID_SERVICE, UUID_CHAR_WRITE, null) .writeCharacteristic(newbyte[]{1, 2, 3}, newBleCharactCallback(){@OverridepublicvoidonSuccess(BluetoothGattCharacteristiccharacteristic){BleLog.i(TAG, "Write Success, DATA: " + Arrays.toString(characteristic.getValue()))} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Write failure: " + exception); bleExceptionHandler.handleException(exception)} });###8. write data to descriptor
LiteBleConnectorconnector = liteBluetooth.newBleConnector(); connector.withUUIDString(UUID_SERVICE, UUID_CHAR_WRITE, UUID_DESCRIPTOR_WRITE) .writeDescriptor(newbyte[]{1, 2, 3}, newBleDescriptorCallback(){@OverridepublicvoidonSuccess(BluetoothGattDescriptordescriptor){BleLog.i(TAG, "Write Success, DATA: " + Arrays.toString(descriptor.getValue()))} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Write failure: " + exception); bleExceptionHandler.handleException(exception)} });###9. read data from characteritic
LiteBleConnectorconnector = liteBluetooth.newBleConnector(); connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, null) .readCharacteristic(newBleCharactCallback(){@OverridepublicvoidonSuccess(BluetoothGattCharacteristiccharacteristic){BleLog.i(TAG, "Read Success, DATA: " + Arrays.toString(characteristic.getValue()))} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Read failure: " + exception); bleExceptionHandler.handleException(exception)} });###10. enable notification of characteristic
LiteBleConnectorconnector = liteBluetooth.newBleConnector(); connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, null) .enableCharacteristicNotification(newBleCharactCallback(){@OverridepublicvoidonSuccess(BluetoothGattCharacteristiccharacteristic){BleLog.i(TAG, "Notification characteristic Success, DATA: " + Arrays .toString(characteristic.getValue()))} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Notification characteristic failure: " + exception); bleExceptionHandler.handleException(exception)} });###11. enable notification of descriptor
LiteBleConnectorconnector = liteBluetooth.newBleConnector(); connector.withUUIDString(UUID_SERVICE, UUID_CHAR_READ, UUID_DESCRIPTOR_READ) .enableDescriptorNotification(newBleDescriptorCallback(){@OverridepublicvoidonSuccess(BluetoothGattDescriptordescriptor){BleLog.i(TAG, "Notification descriptor Success, DATA: " + Arrays.toString(descriptor.getValue()))} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Notification descriptor failure : " + exception); bleExceptionHandler.handleException(exception)} });###12. read RSSI of device
liteBluetooth.newBleConnector() .readRemoteRssi(newBleRssiCallback(){@OverridepublicvoidonSuccess(intrssi){BleLog.i(TAG, "Read Success, rssi: " + rssi)} @OverridepublicvoidonFailure(BleExceptionexception){BleLog.i(TAG, "Read failure : " + exception); bleExceptionHandler.handleException(exception)} });##More Detail, See The Sample
Website : http://litesuits.com
Email : [email protected]