Hello, I am currently trying to connect my mobile phone to the soldered Bluetooth module on the RP2040. The input on the serial monitor and output on the mobile phone work perfectly. Only the other way around, unfortunately only to a limited extent (data arrives at the serial monitor but unfortunately only incorrect values). What's wrong with the code?
#include <ArduinoBLE.h>
//byte = uint8_t
byte senden = 0;
byte lesen = 0;
BLEService MessageService("19B10000-E8F2-537E-4F6C-D104768A1214");
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic messageCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify | BLEWrite);
void setup() {
Serial.begin(9600);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
}
// set advertised local name and service UUID:
BLE.setLocalName("Arduino Nano RP2040");
BLE.setAdvertisedService(MessageService);
// add the characteristic to the service
MessageService.addCharacteristic(messageCharacteristic);
// add service
BLE.addService(MessageService);
// start advertising
BLE.advertise();
Serial.println("Arduino, waiting for connections...");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to phone: ");
// print the central's MAC address:
Serial.println(central.address());
while (central.connected()) {
//Handy empfängt => eingabe via serial monitor
if(Serial.available() > 0) {
senden = (byte) Serial.read();
messageCharacteristic.writeValue(senden);
}
//Handy sendet => ausgabe via serial monitor
if (messageCharacteristic.written()) { //valueUpdated
messageCharacteristic.readValue(lesen);
Serial.println(lesen);
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from phone: "));
Serial.println(central.address());
}
}
bluetooth_test.ino (1.8 KB)