This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nordic nRF Toolbox code

Hi all,

I am beginner in Java, I have experince just with assembler and C. I did only one app before in java, with Laird libraries, because we have Laird boards using Nordic chips. The Laird libraries are not so great, so I decided to use Nordic libraries for BLE connection. The connection works perfect. I would like to use some parts of nRF Toolbox code, mainly from UART activity. I am using easy communication Rx Tx with commands to our device (Motor Regulator). I do not actually understand how work callbacks in UART activity, what I am trying to do is to have all "UARTManagerCallbacks" in UARTActivity, not only in UARTService. Because I need to know in UARTActivity that for example the Ble data are received and visualize the data in the UARTActivity. So mainly functions onDataReceived, onDataSent, and next my functions from UARTService or UARTManager. In other features Nordic is using initialize manager for bridge the callbacks if I understand it right, like here HRS

    @Override
    protected BleManager<HRSManagerCallbacks> initializeManager() {
        final HRSManager manager = HRSManager.getInstance(getApplicationContext());
        manager.setGattCallbacks(this);
        return manager;
    }

I would like to have in UARTActivity for example this function:

    @Override
    public void onDataReceived(final BluetoothDevice device, final String data) {
        Log.d("UART_ACTIVITY","comming data: "+data);
    }

Please could you help me and write me an example how to do it? I cant continue without this big Help. Thank you

  • Hi,

    HRS sample in nRF Toolbox isn't using services, that's why you create the manager there. I wouldn't recommend this approach.

    There are 2 alternatives, depending on how your app should work:

    1. If you plan to have background ble operations (that is when your app is in background, but your device is still connected and sending/receiving some data) the service approach is recommended. The service could run independent to your activity, handle connection, etc, and send events to the activity (if one is open) using local broadcasts, like we do in the nRF Toolbox uart sample. There, the service initializes and holds a reference to the BleManager which keeps the connection, and on each event in UartCallbacks it either does some logic or it sends a broadcast to the activity. Activity, when started, registers to those broadcasts and displays the data.

    2. If you plan to have the connection only in the activity, for a short period of time (can be to connect, display/send data, and disconnect, or to provision a device, etc), have a look at nRF Blinky app on GitHub. It's using the new Architecture Components on Android, and the connection and BleManager is handled in a ViewModel. It preserves orientation changes but is killed together with your activity.

    If you decide to go with the first approach, you also may have the onDataReceived(...) callback in the activity. Check out other profiles, like RSC if it's not in uart. The service, whuch is the true callback for the manager, receives an event, send a broadcast and the RscActivity receives it and passed to a callback. See here: 

    github.com/.../RSCActivity.java
  • Thank you Mr Nowakowski, it was exactly what I needed to know. I did it according 1., it works great.

  • Great! Btw, I just edited the answer making it more in English :) Sorry for all the mistakes before.

Related