B4R Tutorial Connecting to Arduino with BLE (Bluetooth Low Energy)

Discussion in 'B4R Tutorials' started by Erel, Apr 14, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    In the previous example we broadcasted a single byte as part of the advertising data: https://www.b4x.com/android/forum/threads/ble-hm-10-module-broadcasting-a-single-byte.65785/

    In this example the Android or iOS device (only one at a time) will connect to the BLE peripheral and read and write data to the Arduino board.

    The phone controls the intensity of the two leds:


    The steps required are:
    1. Scan for BLE devices.
    2. Connect to the BLE.
    3. On iOS we need to call ReadData once to discover the characteristics.
    4. Call SetNotify to be notified whenever the Arduino writes to the BLE.
    5. Call WriteData to send data to the Arduino.

    The B4R code:

    Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private softserial As SoftwareSerial
    Private timer1 As Timer
    Private pins(2As Pin
    End Sub

    Private Sub AppStart
       timer1.Enabled = 
    'Must be pwm pins as we are using AnalogWrite
       pins(0).Initialize(5, pins(0).MODE_OUTPUT)
    1).Initialize(6, pins(1).MODE_OUTPUT)
    End Sub

    Sub Timer1_Tick
    "Time: ".GetBytes)
    NumberFormat(Millis, 00).GetBytes)
    End Sub

    Sub astream_NewData (Buffer() As Byte)
    For i = 0 To Buffer.Length - 2 Step 2
    Dim pinIndex As Byte = Buffer(i)
    Dim pinValue As Byte = Buffer(i + 1)
    'using constrain to avoid non allowed values
         pins(Constrain(pinIndex, 01)).AnalogWrite(pinValue)
    End Sub
    The B4A and B4i projects are attached.

    Attached Files:

    Last edited: Jan 17, 2018
    miker2069, MarkusR, Beja and 3 others like this.
  2. dilettante

    dilettante Active Member Licensed User

    In case anyone got this far down without moving on in frustration...

    BLE = Bluetooth Low Energy, and requires some sort of (BLE) Shield.
    Erel likes this.
  3. tufanv

    tufanv Expert Licensed User

    Is it possible for you to post a tutorial like this but with a rgb led so we can control the color of the rgb led remotelyu with our phone ?
  4. Erel

    Erel Administrator Staff Member Licensed User

    There is no logical difference between regular leds and rgb leds. An rdb led is the same as three leds.
    tufanv likes this.
  5. tufanv

    tufanv Expert Licensed User

    Yes but the regular leds have a color and you just switch them on , but rgb leds as i know we can set the color witg rgb values, ı want to know how can i set the rgb value for the led
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question / request in the questions forum.
  7. tufanv

    tufanv Expert Licensed User

    I have a little problem. I have connected one of the leds to 5 an the other one to 6, with other legs connected to gnd via 330. i run the b4r app, green led is always on with full brightness and red is not. Later i run the b4i app, i get the connected msg. When i change one of the sliders red goes on but nothing changes when i slide it, nothing happens to green with the other slider. What did i make wrong any ideas ?
  8. Erel

    Erel Administrator Staff Member Licensed User

    The first step in debugging this is to use ByteConverter to log the data received:
    Also test it without the BLE connection. Set the pins values directly and make sure that the leds react as expected.

    Note that there is a bug in v1.00 beta 6 related to AsyncStreams. Beta 7 will be released today.
    tufanv likes this.
  9. tufanv

    tufanv Expert Licensed User

    i used the upper slider in b4i and the log on b4r was:

    Out of bounds error. 
    Array length = 7, Index = 7
    I get the same error for the other slider and time stops after this error and nothing happens.
  10. Erel

    Erel Administrator Staff Member Licensed User

    I've updated the for line to:
    For i = 0 To Buffer.Length - 2 Step 2
    This will fix the out of bounds error. As I wrote you should wait for beta 7 before you further tests it.
  11. tufanv

    tufanv Expert Licensed User

    ok I will change the code and wait for the beta7 =) thanks
  12. tufanv

    tufanv Expert Licensed User

    by the way i just changed the code and wanted to test it, and it works fine now with beta 6.
    Erel likes this.
  13. stari

    stari Active Member Licensed User

    Until now, I used another tool for Arduino (microBasic for AVR). Now I use only B4R.
    Question: How can I communicate simultaneously with multiple HM-10 devices?
  14. Erel

    Erel Administrator Staff Member Licensed User

    stari likes this.
  15. stari

    stari Active Member Licensed User

  16. kostas3001

    kostas3001 Member Licensed User

    What if there are 4 arduino equal teams, with HM-10 each and only 1 smartphone? I want to communicate only with one of them. How can I do it?
  17. Erel

    Erel Administrator Staff Member Licensed User

    You will need to connect to the correct one. The DeviceFound event will be raised multiple times. You need to use the Name and DeviceId parameters to decide whether it is the correct one.
    stari likes this.
  18. Erel

    Erel Administrator Staff Member Licensed User

    The B4A example was updated with the changes required when setting targetSdkVersion to 26.
    Beja likes this.
  19. Beja

    Beja Expert Licensed User

    Hi Erel
    Can I use HC-31 instead of the ble shield with Arduino Uno?

  20. Erel

    Erel Administrator Staff Member Licensed User

    I think that HC-31 is based on HC-06. This is classic Bluetooth module, not BLE.
    Beja likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice