- BLE operation methods (i.e.
writeCharacteristic(...), etc.) return theRequestclass now, instead of boolean. onLinklossOccurcallback has been renamed toonLinkLossOccurred.- GATT callbacks (for example:
onCharacteristicRead,onCharacteristicNotified, etc.) insideBleManagerGattCallbackhas been deprecated. UseRequestcallbacks instead. - Build-in Battery Level support has been deprecated. Request Battery Level as any other value.
- A new callbacks method:
onBondingFailedhas been added toBleManagerCallbacks. shouldAutoConnect()has ben deprecated, useuseAutoConnect(boolean)inConnectRequestinstead.- Timeout is supported for connect, disconnect and wait for notification/indication.
Most BLE operations do not support setting timeout, as receiving the
BluetoothGattCallbackis required in order to perform the next operation. - Atomic
RequestQueueandReliableWriteRequestare supported. - BLE Library 2.0 uses Java 8. There's no good reason for this except to push the ecosystem to having this be a default. As of AGP 3.2 there is no reason not to do this (via butterknife).
- Replace
initGatt(BluetoothGatt)withinitialize():
Old code:
@Override
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
final LinkedList<Request> requests = new LinkedList<>();
requests.add(Request.newEnableNotificationsRequest(characteristic));
return requests;
}New code:
@Override
protected void initialize() {
setNotificationCallback(characteristic)
.with(new DataReceivedCallback() {
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
...
}
});
enableNotifications(characteristic)
.enqueue();
}See changes in Android nRF Toolbox and Android nRF Blinky for more examples.
Remember to call .enqueue() method for initialization requests!
Connect's completion callback is called after the initialization is done (without or with errors).
- Move your callback implementation from
BleManagerGattCallbackto request callbacks. - To split logic from parsing, we recommend to extend
DataReceivedCallbackinterface in a class where your parse your data, and return higher-level values. For a sample, check out nRF Toolbox and Android BLE Common Library. If you are depending on a SIG adopted profile, like Heart Rate Monitor, Proximity, etc., feel free to include the BLE Common Library in your project. It has all the parsers implemented. If your profile isn't there, we are happy to accept PRs. connect()anddisconnect()methods also require calling.enqueue()in asynchronous use.- Replace the
shouldAutoConnect()method in the manager withconnect(device).useAutConnect(true).enqueue()/await().