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).