iOS Tutorial BLE (Bluetooth Low Energy) Peripheral

There are two types of devices involved in BLE communication: peripheral devices and central devices.

Peripheral devices: heart rate monitors, beacons, Arduino with HM 10 module, etc.
Central devices: Usually phones.

Central devices connect to peripheral devices. BleManager type in both B4A and B4i is an implementation of the central mode.

iBLE v2.00 includes a new type named PeripheralManager. PeripheralManager is an implementation of the peripheral mode.
Configuring a device as a peripheral device allows other Android and iOS devices to directly connect to it.

Unlike class Bluetooth, BLE is not designed for streaming data. The throughput is quite low. About 300 bytes per second based on my measurements.

Using PeripheralManager:

1. Initialize the object:
B4X:
peripheral.Initialize("peripheral")
2. The StateChanged event will be raised. Assuming that the state is POWER_ON we can call Start:
B4X:
Sub Peripheral_StateChanged (State As Int)
   If State <> peripheral.STATE_POWERED_ON Then
     hd.ToastMessageShow("Bluetooth is not enabled", True)
   Else
     peripheral.Start("B4iPeripheral")
   End If
End Sub
Calling Start creates a service with the UUID of 0001. This service includes two characteristics:
1001 - Readable / notifiable characteristic.
1002 - Writable characteristic.
Centrals will set a notification for characteristic 1001 and will write to 1002.

3. When a central subscribes to the 1001 characteristic the Subscribe event will be raised. This means that there is a connected central.
Note that there can be several connected centrals.

4. When a central writes data to the 1002 characteristic the NewData event will be raised. Note that the maximum size of messages sent from a central is 512 bytes.

5. Call PeripheralManager.Write to send data to one or more centrals. The maximum size of messages sent from the peripheral is 20 bytes.
The DataAvailable event will be raised on the centrals.

6. The Unsubscribe event is raised when a central disconnects.

See the BLE chat for a full example.

 
Last edited:
Top