Skip to content

Conversation

@philips77
Copy link
Collaborator

DO NOT MERGE

@philips77 philips77 changed the title Migration to BLE Library v2 [NO MERGE YET] Migration to BLE Library v2 Apr 30, 2018
@philips77 philips77 requested a review from bgiori April 30, 2018 20:39
@philips77
Copy link
Collaborator Author

This is an initial PR with some BLE related changes. I will continue to work on it, but wanted you to have a look. Changes:

  1. BLE Library from external project (this is temporary, until the lib is in jcenter)
  2. BLE Library must be cloned into Android-BLE-Library folder, and switched to 'develop' branch.
  3. The v2 of the lib is not finished, but I decided to switch now, as feedback from this project is very important. Let's see what do we need to be in the lib and it can be implemented before going out from alpha..
  4. There was a bug in your previous code related to packet defragmentation. The alg correctly identified that a packet does not contain a full response, but also tried to read header from the second packet (which was 3 bytes long) and after casting it to 8-byte headed the expected length was 65000+ or some other random number, so it went into if with 'requiresDefragmentation'. Accidentally.
  5. Also, I removed external callbacks. With per-request-callbacks this is not required.

Problems I see:

  1. Calling a synchronous request will not work when called from UI thread, as the manager now tries to send data from UI thread, which is blocked.
    2.. There is some cleaning that needs to be done. Some classes are not required anymore.

@philips77
Copy link
Collaborator Author

Nor now the BLE Lib requires Java 1.8. I think I'll follow your example and revert lambdas and default interface methods, as after all it's a library and not all use Android Studio 3, unfortunately.

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2018, Nordic Semiconductor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like all the files in the library to use the same Apache 2.0 license. I noticed that the files in the ble package still have the nordic license.

@bgiori
Copy link
Contributor

bgiori commented Jun 21, 2018

Looks sooo good! Thanks again for all your work Aleksander (Filip?).

I am ready to merge the library, but first I want to test the app with mynewt devices (I assume you have not done this).

I've encountered a crash when collecting stats:

06-21 14:39:17.411 24185-24185/io.runtime.mcumgr D/BluetoothLeScanner: Stop Scan
06-21 14:39:17.421 24185-24185/io.runtime.mcumgr D/ViewRootImpl@47c9e1a[ScannerActivity]: Relayout returned: oldFrame=[0,0][1080,1920] newFrame=[0,0][1080,1920] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=false
06-21 14:39:18.128 24185-24185/io.runtime.mcumgr D/ViewRootImpl@26035db[MainActivity]: ViewPostImeInputStage processPointer 0
06-21 14:39:18.170 24185-24185/io.runtime.mcumgr D/ViewRootImpl@26035db[MainActivity]: ViewPostImeInputStage processPointer 1
06-21 14:39:18.203 24185-24185/io.runtime.mcumgr D/SEM_CLIP_SemClipboardManager: isCocktailBarDisplayed : false
06-21 14:39:18.245 24185-24190/io.runtime.mcumgr I/art: Do full code cache collection, code=123KB, data=124KB
06-21 14:39:18.246 24185-24190/io.runtime.mcumgr I/art: After code cache collection, code=114KB, data=101KB
06-21 14:39:18.260 24185-24190/io.runtime.mcumgr I/art: Do partial code cache collection, code=122KB, data=108KB
    After code cache collection, code=122KB, data=108KB
    Increasing code cache capacity to 512KB
06-21 14:39:20.121 24185-24185/io.runtime.mcumgr D/ViewRootImpl@26035db[MainActivity]: ViewPostImeInputStage processPointer 0
06-21 14:39:20.207 24185-24185/io.runtime.mcumgr D/ViewRootImpl@26035db[MainActivity]: ViewPostImeInputStage processPointer 1
06-21 14:39:20.288 24185-24185/io.runtime.mcumgr W/Java7Support: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
06-21 14:39:20.373 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Connecting...
    gatt = device.connectGatt(autoConnect = false)
06-21 14:39:20.375 24185-24185/io.runtime.mcumgr D/BluetoothGatt: connect() - device: E1:86:3C:FD:53:BB, auto: false
06-21 14:39:20.376 24185-24185/io.runtime.mcumgr D/BluetoothAdapter: isSecureModeEnabled
06-21 14:39:20.376 24185-24185/io.runtime.mcumgr D/BluetoothGatt: registerApp()
    registerApp() - UUID=88c4a501-a239-4c6b-9fff-c9ae53114a97
06-21 14:39:20.479 24185-24197/io.runtime.mcumgr D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
06-21 14:39:20.855 24185-24218/io.runtime.mcumgr D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=E1:86:3C:FD:53:BB
06-21 14:39:20.877 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: [Callback] Connection state changed with status: 0 and new state: 2 (CONNECTED)
    Connected to E1:86:3C:FD:53:BB
06-21 14:39:20.897 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Discovering Services...
    gatt.discoverServices()
06-21 14:39:20.897 24185-24185/io.runtime.mcumgr D/BluetoothGatt: discoverServices() - device: E1:86:3C:FD:53:BB
06-21 14:39:21.387 24185-24197/io.runtime.mcumgr D/BluetoothGatt: onClientConnParamsChanged() - Device=E1:86:3C:FD:53:BB interval=6 status=0
06-21 14:39:21.705 24185-24197/io.runtime.mcumgr D/BluetoothGatt: onSearchComplete() = Device=E1:86:3C:FD:53:BB Status=0
06-21 14:39:21.712 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Services Discovered
06-21 14:39:21.713 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Primary service found
06-21 14:39:21.720 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Requesting new MTU...
    gatt.requestMtu(515)
06-21 14:39:21.720 24185-24185/io.runtime.mcumgr D/BluetoothGatt: configureMTU() - device: E1:86:3C:FD:53:BB mtu: 515
06-21 14:39:21.738 24185-24197/io.runtime.mcumgr D/BluetoothGatt: onConfigureMTU() - Device=E1:86:3C:FD:53:BB mtu=512 status=0
06-21 14:39:21.746 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: MTU changed to: 512
06-21 14:39:21.747 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.setCharacteristicNotification(da2e7828-fbce-4e01-ae9e-261174997c48, true)
06-21 14:39:21.748 24185-24185/io.runtime.mcumgr D/BluetoothGatt: setCharacteristicNotification() - uuid: da2e7828-fbce-4e01-ae9e-261174997c48 enable: true
06-21 14:39:21.751 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Enabling notifications for da2e7828-fbce-4e01-ae9e-261174997c48
    gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)
06-21 14:39:21.800 24185-24197/io.runtime.mcumgr D/BluetoothGatt: onClientConnParamsChanged() - Device=E1:86:3C:FD:53:BB interval=39 status=0
06-21 14:39:21.816 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to descr. 00002902-0000-1000-8000-00805f9b34fb, value: (0x) 01-00
06-21 14:39:21.817 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Notifications enabled
06-21 14:39:21.826 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
06-21 14:39:21.827 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:21.856 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 00-00-00-02-00-02-00-01-BF-FF
06-21 14:39:22.148 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Notification received from da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 01-00-00-67-00-02-00-01-BF-62-72-63-00-69-73-74-61-74-5F-6C-69-73-74-9F-64-73-74-61-74-69-62-6C-65-5F-6C-32-63-61-70-67-62-6C-65-5F-61-74-74-67-62-6C-65-5F-67-61-70-69-62-6C-65-5F-67-61-74-74-63-69-62-6C-65-5F-67-61-74-74-73-66-62-6C-65-5F-68-73-6B-62-6C-65-5F-6C-6C-5F-63-6F-6E-6E-66-62-6C-65-5F-6C-6C-67-62-6C-65-5F-70-68-79-FF-FF
06-21 14:39:22.311 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:22.328 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 00-00-00-0C-00-02-00-00-BF-64-6E-61-6D-65-64-73-74-61-74-FF
06-21 14:39:22.451 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Notification received from da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 01-00-00-27-00-02-00-00-BF-62-72-63-00-64-6E-61-6D-65-64-73-74-61-74-65-67-72-6F-75-70-63-73-79-73-66-66-69-65-6C-64-73-BF-62-73-30-0A-FF-FF
06-21 14:39:22.525 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
06-21 14:39:22.526 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:22.534 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 00-00-00-11-00-02-00-00-BF-64-6E-61-6D-65-69-62-6C-65-5F-6C-32-63-61-70-FF
06-21 14:39:22.835 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Notification received from da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 01-00-00-50-00-02-00-00-BF-62-72-63-00-64-6E-61-6D-65-69-62-6C-65-5F-6C-32-63-61-70-65-67-72-6F-75-70-63-73-79-73-66-66-69-65-6C-64-73-BF-62-73-30-06-62-73-31-03-62-73-32-00-62-73-33-00-62-73-34-00-62-73-35-00-62-73-36-00-62-73-37-00-62-73-38-00-62-73-39-00-FF-FF
06-21 14:39:22.880 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:22.891 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 00-00-00-0F-00-02-00-00-BF-64-6E-61-6D-65-67-62-6C-65-5F-61-74-74-FF
06-21 14:39:23.316 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.320 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
06-21 14:39:23.321 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.325 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
06-21 14:39:23.326 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.329 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
06-21 14:39:23.333 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.337 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.341 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.344 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48)
06-21 14:39:23.349 24185-24185/io.runtime.mcumgr D/McuMgrBleTransport: Data written to da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 00-00-00-0F-00-02-00-00-BF-64-6E-61-6D-65-67-62-6C-65-5F-70-68-79-FF
06-21 14:39:23.350 24185-24185/io.runtime.mcumgr D/AndroidRuntime: Shutting down VM
06-21 14:39:23.351 24185-24185/io.runtime.mcumgr E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.runtime.mcumgr, PID: 24185
    java.lang.NullPointerException: Attempt to invoke virtual method 'void no.nordicsemi.android.ble.WriteRequest.notifyPacketSent(android.bluetooth.BluetoothDevice, byte[])' on a null object reference
        at no.nordicsemi.android.ble.BleManager$BleManagerGattCallback.onCharacteristicWriteSafe(BleManager.java:2218)
        at no.nordicsemi.android.ble.MainThreadBluetoothGattCallback.lambda$onCharacteristicWrite$3$MainThreadBluetoothGattCallback(MainThreadBluetoothGattCallback.java:111)
        at no.nordicsemi.android.ble.MainThreadBluetoothGattCallback$$Lambda$3.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

@philips77
Copy link
Collaborator Author

Do you have it running on nrf52 dk? You could then send me a text file.
Yesterday I was testing extensively the BLE Library with various phones and devices and have fixed some issues. Today I have a day off, going hiking, but i could look into the issue on Monday. I wonder why the data at the end are being sent without waiting for a notification.

@bgiori bgiori merged commit 3459223 into master Jun 29, 2018
@bgiori bgiori deleted the ble_lib_v2 branch August 21, 2018 18:33
philips77 added a commit that referenced this pull request Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants