Android Question BLE communication problem

jefflynn1974

Member
Licensed User
Longtime User
Hi there.
I've been developing an application that communicates with a device using Bluetooth. The device has a RN4020 BLE module operating in MLDP mode, and a simple command-respone protocol. The problem is that when the app sends a command to the device, it executes it and replies, but the app doesn't receive the reply at all or receives a part of it. For example the reply is *OKxxxxxxxxxxxxx CR LF the app gets only xxxx or OKx or CR LF. I use the BleExtEx library and I read the incoming data using the ble_CharacteristicChanged event. I have tested on two different android tablets, one was a Samsung, the other one was Chuwi and neither worked well. I'm stuck here, any help appreciated.
 

JTmartins

Active Member
Licensed User
Longtime User
I had similar problems but only in debug mode while using the fast debugger. in release mode or legacy debug I never had problems.

Are you in debug mode ?
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
I've attached a bit of test I code I wrote to communicate with the RN4020 on a board I developed. It is very basic but you should be able to see what I've done and build on it or use it to help diagnose your issue. It requires the BLE2 library. Make sure to download the latest version (v1.14) as the earlier version was missing a few things.

The app sends a 2 byte command to the RN4020 which converts it to serial and sends it to a PIC. The PIC does some stuff and sends a response. Nothing fancy. The send code is:

B4X:
    Dim D(2) As Byte
    D(0) = 70        ' F
    D(1) = 63        ' ?
   
    manager.WriteData(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, D) 
    manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, True)

Which sends "F?" to the RN4020. Once the PIC responds, the DataAvailable sub is called which build the response. It will be better explained by looking at the code.
 

Attachments

  • RN4020_B4A.zip
    31.3 KB · Views: 239
Upvote 0

jefflynn1974

Member
Licensed User
Longtime User
Thanks for all the answers. I usually write my programs in debug mode, but switching to release mode didn't solve the problem. The communication is a little bit better, but still doesn't work well. I'll take Erel's advice and take a look at the BLE2 library.
 
Upvote 0

jefflynn1974

Member
Licensed User
Longtime User
I've attached a bit of test I code I wrote to communicate with the RN4020 on a board I developed. It is very basic but you should be able to see what I've done and build on it or use it to help diagnose your issue. It requires the BLE2 library. Make sure to download the latest version (v1.14) as the earlier version was missing a few things.

The app sends a 2 byte command to the RN4020 which converts it to serial and sends it to a PIC. The PIC does some stuff and sends a response. Nothing fancy. The send code is:

B4X:
    Dim D(2) As Byte
    D(0) = 70        ' F
    D(1) = 63        ' ?
  
    manager.WriteData(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, D)
    manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, True)

Which sends "F?" to the RN4020. Once the PIC responds, the DataAvailable sub is called which build the response. It will be better explained by looking at the code.

Your answer proved very helpful :) I haven't switched to BLE2 library in my project yet, but modified your test app to communicate to my device and it works flawlessly even in debug mode. There's one strange thing though. I get back the command that I have sent to the device after receiving the real response. For instance I send *STOP CRLF and I get *OK CRLF *STOP CRLF. I don't know if it has anything to do with thw BLE2 library, but anyway, it's not a big deal.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
My understanding is when the RN4020 is in MLDP mode, it is acting in a pass-thru mode; data received over BT is sent out over serial, date received over serial is sent out over BT. It could be part of the firmware that is on the board?

This is what I send to the RN4020 from the PIC to configure it.

"SF,1" = Reset to factory default configuration.
"SR,32000000" = Auto Advertise, Enable MLDP, UART flow control
"R,1" = Reboot RN4020 to make the changes effective.

Once the app connects, the PIC sends "I" to inter MLDP mode which allows 2 way communication
 
Upvote 0
Top