Android Question (Solved) BLE time to connect to RN4020 too long!

dagnabitboy

Active Member
Licensed User
Longtime User
Hi All! I'm developing a commercial product and using B4A to communicate with a Microchip RN4020 BLE module / PIC microcontroller combo. The app is working well with one exception: it takes typically 10 to 15 seconds to connect, sometimes as long as 30 seconds, which of course is a product killer! Once it's connected everything works as expected. Not all Android devices have the problem. Details:
Samsung S3: Android 4.1, connects immediately.
Amazon Fire Hd: Android 5.1 (?), connects within a second or two.
Ellipsys tablet (Verizon): Android 6.1, has above mentioned delay problem.
Google Pixel: Android 7.1, also has above delay problem.

Here's the kicker: There are two Microchip BLE apps that connect very rapidly on all above devices. One is called MLDP (not on the app store, but Microchip site), and the other is called SmartDiscover (on the app store). Two other app store BLE utilities, nRF Connect and BLE Scanner also connect rapidly. This tells me I'm probably missing some important detail. I don't know much about BLE, my code is pieced together from app on this site. I can supply the MLDP Java source code if that helps someone!

Here's my RN4020 setup:
SF,2 // factory default
SP,7 // power to the max
SR,32004000 // auto advertise, enb mldp, peripheral, mldp,cts/rts, IOS, automldp
ST,0064,0002,0064 // setup IOS required timing
SN,ZWGDO-01 // friendly bluetooth name
R,1 // reset
wait 1.5 seconds
set MLDP/CMD pin to 1 // sets MLDP mode)

Here's a capture from the log (with my comments):
Scanning
Found: ZWGDO-01, 00:1E:C0:3F:6C:15, RSSI = -47, (MyMap) {1=[B@fe6490c, 7=[B@a96f255, 9=[B@2d9446a, 0=[B@d569d5b} (found my device immediately)

(This is where the long 10 to 30 second delay happens)

Discovering services.
Connected (once connected everything works well)
BT connected (log message from my code)
Setting descriptor. Success = true
writing descriptor: true
ID=00035b03-58e6-07dd-021a-08123a000301
ID=00035b03-58e6-07dd-021a-08123a000301
ID=00035b03-58e6-07dd-021a-08123a000301
ID=00035b03-58e6-07dd-021a-08123a000301
Msg received: Welcome! (log message from my code)

Disconnecting
writing descriptor: true
Disconnected
Connect duration = 24484 miliseconds
** Activity (main) Pause, UserClosed = true **
Disconnecting

I've attached a zip of a "RN4020 Chat app" I created that shows the problem. I sure hope someone can help!!
 

Attachments

  • RN4020-chat.zip
    178.1 KB · Views: 268

JordiCP

Expert
Licensed User
Longtime User
From my experience with RN4020 modules, these apps are faster because they look explicitly (at least the ones from microchip) for some specific data that they know it is there.

I am not using MLDP, but I think this should not make any difference in the Android side. Try to add, after the factory reset:
B4X:
SS,80000001
This should make at least one public service (I think the 'Id') visible and will be easier for a 'scan' to find the device. This worked for me, hope it helps :)
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Perhaps some detail here can help

For what I remember, my 'process' to get it working (after lots of headaches :mad:) was
  • WAKE_SW and WAKE_HW active (with low energy modes it also works, but it is lazier and discovery is slower)
  • Each time I made any change to the modules, I also went to the Android Bluetooth Settings screen and forced it to 'forget' that device and scan again. Otherwise, it seems that it keeps some cache info that is only retrieved the first time and doesn't get updated, resulting in undesired behaviors.
  • My settings are similar to yours (without MLDP) but not forcing the timings. I experimented with these, and came to the conclusion (perhaps totally wrong) that only forcing the bit SR,----4--- seemed to be enough for IOs and Android
  • Also, check that module firmware is updated (specially for MLDP seems that 1.23 or something similar is recommended since the previous are really buggy).
As said, the complete settings were not very different from yours (apart from the mentioned MLDP and timing settings)
SF,2
SN,xxxxxxxx // Nombre identificador --deleted--
SR,20000000 // Modo periferico, y “advertising”
SR,20004000 // Compatibilidad ANDROID 6!!!!!
SS,80000001 // Mostrará perfil id y privados
R,1 // reboot
PS,112233445566778899AABBCCDDEEFF00 // Servicio privado
PC,112233445566778899AABBCCDDEEFF01,12,14 // Caract. privada read
PC,112233445566778899AABBCCDDEEFF02,04,14 // Caract. Privada write
R,1
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Thanks for the info. WAKE-SW and WAKE_HW are active (first low, delay 50ms, then high).
I tried setting peripherals to none, so I could pair my phone with the RN4020. It paired, but then
BLE advertising didn't work, and I couldn't connect at all. I am using Module rev 1.23, but I understand
there is a 1.33 available. I may try that. I was setting specific timings as recommended in the user manual and setting IOS mode as well because ultimately this must work for both Android and IOS. Making those settings made no difference to the connect time problem.

Thank you for your suggestions!
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Are you calling BLE.Connect? If so change it to:
B4X:
BLE.Connect2(DeviceID, False)
It disables the auto connect feature and it makes the connection much quicker.
Thanks Erel! I assumed 'auto connect' would be the preferred method! Changing to Connect2 as you suggested made a big improvement.. connect times now in the 1 to 3 second range.. although a couple of long wait times did still occur. I'll do a lot of testing and report back.
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Spent all morning testing! Your suggestion to use BLE.connect2 has worked well. :) Connect times are consistently in the 1 to 2 second time on all my devices. Once in awhile it takes a bit longer, and strangely I'm getting disconnects without a connect having occurred, but that was solved with a connect retry. So, thanks again Erel!
 
Upvote 0
Top