BLE NANO 33 SENSE Rev2 RegisterForByte crash

Hi everyone.
I've been trying to send bytes of data from my NANO 33 Sense Rev2 to my mobile.
After reading the BLE documentation on ArduinoBLE - Arduino Reference the notify function seemed like a perfect solution for what i want to do, so i went in that direction.

Since im new to App inventor i thought i would use Mo-thunderz example to get the ball rolling.

Here is a picture of the blocks:

His example uses a ESP32 and therefore i cant try his code for my NANO 33 Sense Rev2 board.
Instead i took inspiration from Klaus_K on this post:
https://forum.arduino.cc/t/ble-very-weak-signal/631751/11
(post #12)
and
https://forum.arduino.cc/t/sending-float-over-ble/859240

If i use LightBlue or nRF Connect i'm able to both manually read the values sent from the NANO33 and subscribe(using notify) to the BLE UUID, so the arduino code must be working.

Unforturnately, as soon as i start using the .RegisterForBytes block my app instantly crashes, without any error message.

After reading through the forum it does not seem anyone has been having this issue where the App just straight up closes without any error message.

I've tried commenting out all code that write data to BLE, but the App still crashes right after connecting to the NANO 33.
The only thing that can prevent a crash is if i do not use the RegisterForBytes block.
It is the same for RegisterForIntegers and Floats.

If i can just get my App to stop crashing to I can debug on it, that would help a lot ! but right now it just wont allow me to even touch the RegisterForBytes block.

The Arduino code can be found below:

#include <ArduinoBLE.h>
//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------
#define BLE_UUID_TEST_SERVICE               "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define BLE_UUID_ACCELERATION               "beb5483e-36e1-4688-b7f5-ea07361b26a8"
//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------
BLEService testService( BLE_UUID_TEST_SERVICE );
BLEByteCharacteristic accelerationCharacteristic("BLE_UUID_ACCELERATION", BLERead | BLENotify);
uint8_t accelerationX = 0;

bool setupBleMode()
{
  if ( !BLE.begin() )
  {
    return false;
  }

  // set advertised local name and service UUID:
  BLE.setDeviceName( "Arduino Nano 33 BLE" );
  BLE.setLocalName( "Arduino Nano 33 BLE" );
  BLE.setAdvertisedService( testService );
  // BLE add characteristics
  testService.addCharacteristic( accelerationCharacteristic );
  // add service
  BLE.addService( testService );
  // set the initial value for the characeristic:
  accelerationCharacteristic.writeValue( 0 );
  // start advertising
  BLE.advertise();
  return true;
}

void setup()
{
  Serial.begin( 9600 );
  //  while ( !Serial );
  setupBleMode();
} // setup

void loop()
{
  static long previousMillis = 0;
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  if ( central )
  {
    Serial.print( "Connected to central: " );
    Serial.println( central.address() );

    while ( central.connected() )
    {
      long interval = 2000;
      unsigned long currentMillis = millis();
      if( currentMillis - previousMillis > interval )
      {
        previousMillis = currentMillis;
          accelerationCharacteristic.writeValue( accelerationX );
          accelerationX++;
          Serial.println( accelerationX );
      }

    } // while connected

    Serial.print( F( "Disconnected from central: " ) );
    Serial.println( central.address() );
  } // if central
} // loop

The Device Name/LocalName is too long. Try "Nano33"

Thank you for your reply Chris.
Unfortunately that doesnt help, the app still instantly crashes after it connects to the NANO33.
By the way i can see that it connects correctly on the serialmonitor on Arduino IDE, but right after connecting the app crashes - crashes within less than a second after connecting.

That's interesting - what does your App code do right after connection success?

BTW - You have got a Screen Error Block in place?

The first thing the app does after it connects is call the RegisterForBytes block.
If i disable that block then the app doesnt crash anymore, but then the notify functionality is also disabled and i wont receive data.

Hm no, i've never heard about "Screen Error Block" is it a function on the phone or an actual block in MIT App inventor?

EDIT: Okay i googled it, now i know what Screen Error Block is...
No the only thing in the initialize block is "AskForPermission" and set the BLE status to false

App Inventor Blocks, Screen1, 2nd Block from the top. Or:
https://www.professorcad.co.uk/appinventorsnippets#GetErrorInfo

Should not be assigned over and over again in the loop.

Okay i've added the following ErrorOccured block:
ErrorCode
but i still dont get an error code or pop up of any kind

If i out comment everything and just call the write function once the app still crashes.
Even if i out comment all the write functions it crashes.
And if i use LightBlue or nRF Connect i can read the values written just fine..
It is the RegisterForByte block that crashes the app.

You would either:

Display "ErrorInfo" in a Label or Notifier.......

Or save it to a file.

Lets see your Project File

Export Project

Here it is, it's almost identical to the one MoThunderz originally made.
BLE_controller.aia (193.1 KB)

I have also tried making a few copies myself where i build it all from scratch, in part for the pratice, but i was also hoping that maybe i would just need to import the newest BLE extension or something like that...
But after two attempts nothing has helped.

The Sketch is not configured to send the data to App Inventor - AI doesn't recognise type uint8_t, which is an Unsigned Int (8bits).

If you change the Sketch and App Code to Int, that should work (though there have been occasions where we have worked with ints in the Sketch, but had to send a char string to the App).

Alright, I've tried float, ints, chars, uint_8 and uint_32 and nothing worked... but i'll try with integer again just to be sure.

I changed the app to use the RegisterForInteger block like this:
IntReceived
The .IntegersReceived block might not work correctly, but the app crashes when the .RegisterForIntegers block is called, if i disable that block the app does not crash.

And changed the sketch code to this:

BLEIntCharacteristic accelerationCharacteristic("BLE_UUID_ACCELERATION", BLERead | BLENotify);
int accelerationX = 0;

OK - should communicate better with integers - what is the purpose of your code in the BLE Received Block?

Missing from Permissions: Fine Location - drag a Location Sensor component into Screen1

I would like the received integer to be shown in Label_Char1 so that i can see if an integer has been received. I was hoping to get the same result as in LightBlue where i can see the interger increment with 2 seconds delay.

Okay i'll try adding the permissions as you suggest, but it will have to be tomorrow... it's almost midnight now.

Thank you for your input/help i appreciate it.
I will continue debugging when i come home from work tomorrow.

1 Like

Use this simplified version of your App for tests.

BLE_controller_edit.aia (203.7 KB)

Hi Chris.
So... I was super unlucky the last few days.. I've picked up an infection in my blood and was admitted to the hospital Thirsday morning.
I just got released today but i still have a fever and will need more tests performed on Friday.

I will add the debug part of your program to mine and test it once my head stops spinning :slight_smile: