Android Question BLE extended and password connection

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

I am using a HM10 BLE device together with BLE extended and BLE2 librares.

BLE2 is used to scan the devices
BLE ext to connect to them and send AT commands to the beacon

Everything is working very fine but when I use the "password protected" mode of the beacon, I get the "connected event" before I enter the PIN code.

B4X:
Sub BLE_Connected (Services As Map)
   If ConnectState = cstatePreConnect Then
           ConnectState = cstateConnect       
           'Log("BLE Connected")
           bleService = Services.GetValueAt(2)                           
           readCharacteristic = bleService.GetCharacteristics.GetValueAt(0)
           Manager.ReadCharacteristic(readCharacteristic)
           Manager.SetCharacteristicNotification(readCharacteristic, True)   
           writeCharacteristic = bleService.GetCharacteristics.GetValueAt(0) 
        ToastMessageShow(Locale("connected to " &TheConnectingBeacon.name), True)
    End If      
End Sub

So the input password wndows appears but as I can't trap it, my code fails as no actual connection is indeed achieved.

Do you know if there is a way to trap the "pairing in progress" event to wait for pairing completed before exiting this sub ?

Thank you in advance
 

freedom2000

Well-Known Member
Licensed User
Longtime User
You shouldn't use both libraries. Why can't you write with BLE2?
I came to the same conclusion.

Yesterday evening I started to modify the code to only use BLE2.
I have the connection working, service discovery working. But I fail on the characteristics discovery...
 
Last edited:
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Finally I got it working with BLE2 lib

Here is the piece of code I used both for connection, reading and writing

You should first intialize two variables :
B4X:
Dim TheServiceId As String =         "0000ffe0-0000-1000-8000-00805f9b34fb"
Dim TheCharacteristic As String =     "0000ffe1-0000-1000-8000-00805f9b34fb"

and then

B4X:
Sub Connect
    Try
        manager.Connect(BLE.TheBeacon.MAC)
    Catch
        Log(LastException)
    End Try
End Sub

Sub Disconnect
    manager.Disconnect
    FirstRead = True
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Try
        If FirstRead Then
            FirstRead = False
            manager.SetNotify(ServiceId, TheCharacteristic, True)
            Return
        End If
            Try
                Dim data() As Byte
                data = Characteristics.Get(TheCharacteristic)
                Log( BytesToString(data, 0, data.Length, "UTF8"))
            Catch
                Log(LastException)
            End Try
    Catch
        Log(LastException)
    End Try
End Sub

Sub Manager_Connected (Services As List)
    manager.ReadData2(TheServiceId, TheCharacteristic)
End Sub

Sub Write
    TheAction = "AT+VERS?"

    Try
        manager.WriteData(TheServiceId, TheCharacteristic, TheAction.GetBytes("UTF8") )
    Catch
        Log(LastException)
    End Try
End Sub

With a HM10 board whenever you call the Write you get the board version :)

Thank you again @Erel to put me on the right track
 
Last edited:
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
And I have tested as well with a "password protected" connection and it works.



For my application, BLE2 library is far better and easier to use than BLEext.
 
Last edited:
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi again,

<still the same problem... When the ibeacon is protected, even with BLE2 lib, I get the _Connected event before I can enter the password...

Do you know if there is a way to trap the "Bluetooth pairing" window before the _Connected event ?

bluetooth pairing.jpg
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
The HM-10 has three modes of connection.
Here is what says the datasheet (quite poor) :

AT+TYPE[para1] Para1: 0~2
0:Not need PIN Code
1:Auth not need PIN
2:Auth with PIN
3:Auth and bond
Default: 0
Important: If your module version is less than V515, please don’t use
this command. Under android 4.3 AT+TYPE1 is same to AT+TYPE2.
Note1: Value 3 is added in V524.

So I tried successfully the "No PIN code" mode and it works (see my iBeacon App)

The _connected event is trapped to launch the services discovery of the beacon :

B4X:
Sub Manager_Connected (Services As List)
    manager.ReadData2(TheServiceId, TheCharacteristic)
End Sub

When I am in mode 1, 2 or 3, I can't see any difference between them. And the PIN code windows pops up after the Connected event.

The consequence is that I launch the service discovery and get these results in the log (BLE2 logs) :

B4X:
Discovering services.
Setting descriptor. Success = true
writing descriptor: true

As the discovery is Ok, my code goes on its stuff and finally completely freezes the connection even if I enter the pin code.

I did try to modify the Manager_Connected sub to add a manual condition "if the beacon is locked by the user". My idea was to only automatically connect when in mode 0 and and to manually discover services when in password protected mode : it almost works but it is very cumbersome and not reliable :

B4X:
Sub Manager_Connected (Services As List)
    If Not(TheBeacon.Locked) Then manager.ReadData2(TheServiceId, TheCharacteristic)
    'manager.ReadData2(TheServiceId, TheCharacteristic)
End Sub

Sub Subscribe
    manager.ReadData2(TheServiceId, TheCharacteristic)
End Sub


So the best way would be to have the "system" PIN_code_Connected event to do things more properly.

Thanks for your help
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Are there any relevant messages in the unfiltered logs?

Can you test it with a different device?

I only have 2 HM-10 BLE devices. Both have the same behavior.
And BLEext and BLE2 trigger the "PIN code" window after the connection is done.

Please tell me how to access unfiltered logs...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Ok thank you for the tip for unfiltering the logs. I will check it.

And yes it's one of them. THey are equiped with the CC2541 Texas Instrument BLE chip.

Beware that the cheapest are not genuine HM-10 boards. See here how to recognize "fakes"

So the real ones should be one of these links :
http://www.ebay.com/itm/HM-10-CC254...716759?hash=item464031ced7:g:VdcAAOSwBLlVEsIN

I precisely bought these ones : http://www.ebay.com/itm/1PCS-HM-10-CC2541-CC2540-4-0-BLE-Bluetooth-UART-Transceiver-Central-Switching-/171735429514?hash=item27fc3ab18a:g:xq8AAOSwBLlVFhqP
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Well, you have to first configure the board with AT commands via the RS232 serial link.
If you switch it to "AT+MODE2" then AT commands can also go, via the BLE link, to the chip while "other (non AT+xx)" serial messages can go from Android to RS232 port and vice versa.
It is then possible to control / command the board with AT commands directly with the BLE link.
It's what they call the "remote control" mode in the datasheet

This mode is obviously dependant on the firmware into the board. That's the reason why it's important to buy a real HM-10 with original latest version of the firmware...
 
Last edited:
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Yes this board is 3.3V compliant thus it should work.

However it's a little "overskilled" to only send AT commands.

A simple JY MCU bluetooth to serial board @2$ would have done the job (it's my solution !)
or a USB to serial 3.3V ttl module @2$

You can use these boards from a PC via Putty or Arduino IDE and send ascii commands via RS232 terminal.
Both would also allow to upload firmware to ESP8266 modules or any other arduino board
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
If you are connected in USB debug mode then you can see the unfiltered logs by unchecking the Filter option in the Logs tab.

I have just tested with unfiltred log and here is what I see :

B4X:
** Activity (main) Pause, UserClosed = false **
Ignoring event as service was destroyed: udp_packetarrived
** Activity (slider) Create, isFirst = true **
Ignoring event as service was destroyed: udp_packetarrived
** Activity (slider) Resume **
Ignoring event as service was destroyed: udp_packetarrived
Current page: 1
Ignoring event as service was destroyed: udp_packetarrived
error markersbound
Ignoring event as service was destroyed: udp_packetarrived
Discovering services.
Ignoring event as service was destroyed: udp_packetarrived
Setting descriptor. Success = true
Ignoring event as service was destroyed: udp_packetarrived
writing descriptor: false
Ignoring event as service was destroyed: udp_packetarrived
(RuntimeException) java.lang.RuntimeException: Error writing data to: 0000ffe1-0000-1000-8000-00805f9b34fb
Ignoring event as service was destroyed: udp_packetarrived
** Activity (slider) Pause, UserClosed = false **
Ignoring event as service was destroyed: udp_packetarrived

Forgetting the "junk" I can see that discovery of service and setting descriptor are Ok but writing descriptor fails

Seems consistent with the fact that password is entered too late ?
 
Upvote 0
Top