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:
The B4A and B4i projects are attached.
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:
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private astream As AsyncStreams
Private softserial As SoftwareSerial
Private timer1 As Timer
Private pins(2) As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("appstart")
softserial.Initialize(9600, 2, 3)
astream.Initialize(softserial.Stream, "astream_newdata", Null)
timer1.Initialize("Timer1_Tick", 1000)
timer1.Enabled = True
'Must be pwm pins as we are using AnalogWrite
pins(0).Initialize(5, pins(0).MODE_OUTPUT)
pins(1).Initialize(6, pins(1).MODE_OUTPUT)
pins(1).DigitalWrite(True)
End Sub
Sub Timer1_Tick
astream.Write("Time: ".GetBytes)
astream.Write(NumberFormat(Millis, 0, 0).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, 0, 1)).AnalogWrite(pinValue)
Next
End Sub
The B4A and B4i projects are attached.
Attachments
Last edited: