I am trying to establish a connection between my Android app and my rp2040 via BLE. On the rp2040 side i use a HM-11 modlue. This is my code for establishing a connection on the android side written in Kotlin:
private fun connectToDevice(device: BluetoothDevice) {
Log.d("name", device.name)
coroutineScope.launch{
}
device.connectGatt(this, false, gattCallback)
//coroutineScope.launch {
// val HM11_SERVICE_UUID: UUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb")
//}
}
private val gattCallback = object : BluetoothGattCallback(){
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
coroutineScope.launch {
}
gatt.discoverServices()
Log.d("conectsuccsess", "connection succsess")
Log.d("connectionsuccsess", gatt.discoverServices().toString())
} else if (newState == BluetoothProfile.STATE_DISCONNECTED){
coroutineScope.launch {
}
gatt.close()
Log.d("connectionNot", "no connection")
}
}else {
gatt.close()
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
if (status == BluetoothGatt.GATT_SUCCESS) {
// Find the characteristic you want to write to
val service = gatt.getService(suuid)
val characteristic = service?.getCharacteristic(cuuid)
if (characteristic != null) {
// Write data to the characteristic
val data = "AT".toByteArray()
characteristic.value = data
gatt.writeCharacteristic(characteristic)
val hexString = data.joinToString(separator = "") { byte -> "%02x".format(byte) }
Log.d("DataSent", hexString)
}
}
}
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
if (status == BluetoothGatt.GATT_SUCCESS) {
// Data was successfully written to the characteristic
Log.d("CharacteristicWrite", "Data written successfully.")
} else {
// There was an error writing data to the characteristic
Log.e("CharacteristicWrite", "Error writing data. Status code: $status")
}
}
}
this is my code on the rp2040 written in arduino:
#include <SoftwareSerial.h>
SoftwareSerial ble_device(17,16); // RX, TX
String str_ii = "";
int ii_0 = 0;
void setup() {
ble_device.begin(9600);
delay(100);
// Enter AT+ commands of interest here (BLE Address, UUIDs, Power settings)
ble_cmd("AT+NAMEBLExAR","Device Name: DSD TECH"); // printout device name
ble_cmd("AT+LADDR","Address: B0D2781E4EDE"); // printout BLE address
ble_cmd("AT+CHAR","Char UUID: "); // printout character UUID
ble_cmd("AT+VERSION","Version: DSD TECH V605"); // module version
ble_cmd("AT+RESET","Reset: "); // reset module
str_ii = "";
delay(500);
}
void loop() {
while (ble_device.available()){
char in_char = ble_device.read();
if (int(in_char)!=-1 and int(in_char)!=42){
str_ii+=in_char;
}
if (in_char=='\n'){
delay(20);
String msg = "Msg: ";
msg+=str_ii;
ble_device.print(msg);
str_ii = "";
Serial.println(msg);
}
}
}
String ble_cmd(String cmd_str,String desc_str){
str_ii = "";
unsigned long t1 = millis();
ble_device.println(cmd_str);
while (true){
if ((millis()-t1)>2000){ // 2 sec timeout
return "Err";
}
char in_char = ble_device.read();
if (int(in_char)==-1 or int(in_char)==42){
continue;
}
if (in_char=='\n'){
return str_ii;
}
str_ii+=in_char;
}
}
I thinnk that the connection is established, because of the log prints.
here are the logPrints:
2023-08-21 18:26:41.421 6794-6794 name com.playgroundagc.blescanner D BLExAR
2023-08-21 18:26:41.424 6794-6794 BluetoothGatt com.playgroundagc.blescanner D connect() - device: B0:D2:78:1E:4E:DE, auto: false
2023-08-21 18:26:41.424 6794-6794 BluetoothGatt com.playgroundagc.blescanner D registerApp()
2023-08-21 18:26:41.425 6794-6794 BluetoothGatt com.playgroundagc.blescanner D registerApp() - UUID=077f6f32-bb11-44c9-b3cb-bf738cb4d22a
2023-08-21 18:26:41.427 6794-6810 BluetoothGatt com.playgroundagc.blescanner D onClientRegistered() - status=0 clientIf=6
2023-08-21 18:26:41.700 6794-6809 BluetoothGatt com.playgroundagc.blescanner D onClientConnectionState() - status=0 clientIf=6 device=B0:D2:78:1E:4E:DE
2023-08-21 18:26:41.701 6794-6809 BluetoothGatt com.playgroundagc.blescanner D discoverServices() - device: B0:D2:78:1E:4E:DE
2023-08-21 18:26:41.707 6794-6809 conectsuccsess com.playgroundagc.blescanner D connection succsess
2023-08-21 18:26:41.707 6794-6809 BluetoothGatt com.playgroundagc.blescanner D discoverServices() - device: B0:D2:78:1E:4E:DE
2023-08-21 18:26:41.709 6794-6809 connectionsuccsess com.playgroundagc.blescanner D true
2023-08-21 18:26:42.137 6794-6809 BluetoothGatt com.playgroundagc.blescanner D onConnectionUpdated() - Device=B0:D2:78:1E:4E:DE interval=6 latency=0 timeout=500 status=0
2023-08-21 18:26:42.276 6794-6809 BluetoothGatt com.playgroundagc.blescanner D onSearchComplete() = Device=B0:D2:78:1E:4E:DE Status=0
2023-08-21 18:26:42.283 6794-6809 DataSent com.playgroundagc.blescanner D 4154
2023-08-21 18:26:42.284 6794-6809 CharacteristicWrite com.playgroundagc.blescanner D Data written successfully.
2023-08-21 18:26:42.340 6794-6808 BluetoothGatt com.playgroundagc.blescanner D onConnectionUpdated() - Device=B0:D2:78:1E:4E:DE interval=24 latency=0 timeout=600 status=0
however i am not seeing the message on the arduino serial. I tried this with the BLExAR app and it was working fine. The app just does not suit my purpose and therefor i need my own. can someone explain, why the message i write in my app does not get recieved by the rp2040?