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