B4R Tutorial BLE (HM-10 module) - Broadcasting a single byte

SS-2016-04-13_17.54.33.jpg


In this example I'm using a BLE shield to broadcast the state of a connected button.

The byte is sent as part of the advertising data so there is no need for a connection. Any device that scans for BLE peripherals will be able to detect it.

The HM 10 module is connected as a serial device to pins 2 and 3. I'm using the new rSoftwareSerial library.

The HM 10 module allows us to set the value of a single byte that will be sent as part of the advertising data. This is done with the AT+FLAG command.


The B4R code is very simple:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
   Private softserial As SoftwareSerial
   Private btn As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(9600, 2, 3)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
   btn.AddListener("btn_StateChanged")
End Sub

Sub Btn_StateChanged (State As Boolean)
   Dim b As Byte
   If State Then b = 0 Else b = 1
   astream.Write("AT+FLAG".GetBytes)
   astream.Write(Array As Byte(b))
End Sub

Sub astream_NewData (Buffer() As Byte)
   Log("recieve: ", Buffer)
End Sub

The B4A and B4i projects are attached. There are some differences between the ways that the advertising data is parsed between Android and iOS. You can see it in the projects.
The relevant advertising record starts with B000 and its type is 22 (this is the service data type).
 

Attachments

  • B4A_BLE_ButtonState.zip
    7.9 KB · Views: 924
  • B4i_BLE_ButtonState.zip
    2.9 KB · Views: 474

tufanv

Expert
Licensed User
I could not find a shield in a store but there is a hm10 : See : http://urun.gittigidiyor.com/ev-ele...ble-hm-10-kablosuz-iletisim-arduino-211512177

Is it ok for this example ? or do i need to have the same shield to make this work ?
SS-2016-04-13_17.54.33.jpg


In this example I'm using a BLE shield to broadcast the state of a connected button.

The byte is sent as part of the advertising data so there is no need for a connection. Any device that scans for BLE peripherals will be able to detect it.

The HM 10 module is connected as a serial device to pins 2 and 3. I'm using the new rSoftwareSerial library.

The HM 10 module allows us to set the value of a single byte that will be sent as part of the advertising data. This is done with the AT+FLAG command.


The B4R code is very simple:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
   Private softserial As SoftwareSerial
   Private btn As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(9600, 2, 3)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
   btn.AddListener("btn_StateChanged")
End Sub

Sub Btn_StateChanged (State As Boolean)
   Dim b As Byte
   If State Then b = 0 Else b = 1
   astream.Write("AT+FLAG".GetBytes)
   astream.Write(Array As Byte(b))
End Sub

Sub astream_NewData (Buffer() As Byte)
   Log("recieve: ", Buffer)
End Sub

The B4A and B4i projects are attached. There are some differences between the ways that the advertising data is parsed between Android and iOS. You can see it in the projects.
The relevant advertising record starts with B000 and its type is 22 (this is the service data type).
 
Top