iOS Tutorial BLE Chat - Connecting Android and iOS


This example is based on the new PeripheralManager type introduced in iBLE v2.00.
Please start with this tutorial: https://www.b4x.com/android/forum/threads/ble-bluetooth-low-energy-peripheral.66542/

There are three programs: BlePeripheral, BleCentral_iOS and BleCentral_Android

BlePeripheral is the one that implements the peripheral role.
The other devices will connect to the peripheral and communicate with it. The peripheral will relay the messages to all connected devices.

The code is quite simple. I will go over some interesting points:
- The peripheral stores the connected central ids in a Map (as keys). This makes it easy to add or remove centrals without needing to deal with duplicates.

- The centrals maintain a list with the messages that need to be sent. Only one message can be sent at a time so when WriteComplete is raised the next message is sent.

- In B4i we need to call WriteWithResponse instead of Write as a response is expected. In B4A it is handled automatically.

- The UUID sub is a useful utility that normalizes the 16 bit ids (4 hex characters) based on the platform:
B4X:
Private Sub UUID(id As String) As String
#if B4A
   Return "0000" & id.ToLowerCase & "-0000-1000-8000-00805f9b34fb"
#else if B4I
   Return id.ToUpperCase
#End If
End Sub

- In B4i central we need to first call ReadData before we can call SetNotify and wait for the DataAvailable event.

- In B4A the communication code is implemented in the Starter service.

Update: Add to the B4i examples:

B4X:
#PlistExtra: <key>NSBluetoothAlwaysUsageDescription</key><string>Explanation here</string>
 

Attachments

  • BleCentral_B4i.zip
    3.3 KB · Views: 1,696
  • BleCentral_B4A.zip
    9 KB · Views: 963
  • BlePeripheral_B4i.zip
    2.9 KB · Views: 805
Last edited:

ilan

Expert
Licensed User
Longtime User
This is great.

Like this i can make a multiplayer game between devices that are connected via bluetooth.

Is there a limit of text length that can be sent?

Can we also send images to each other with this lib?
 

elpic76

Member
Licensed User
Longtime User
Hi all,
is it possible to have an example of a peripheral in B4A (BlePeripheral_B4A)? Or maybe the example is already available on the forum but I didn't find.
Thanks in advance
 

Hypnos

Active Member
Licensed User
Longtime User
Good! I have an App on both iOS and Android platform, and now with this library I can implement some useful feature by the BLE communication.

iDevice <-> iDevice worked
iDevice <-> Android worked

But for Android <-> Android, I need to find another way to communication. Hopefully the Android BLE library can support Peripheral mode later even there are not too many android device support it at this moment!

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
BleCentral_B4A and BlePeripheral_B4i were updated. There are a few new requirements:
In B4i:
B4X:
#PlistExtra: <key>NSBluetoothAlwaysUsageDescription</key><string>Bluetooth used to connect to ...</string>

In B4A:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
And handling it at runtime.

It would be easier to use B4XPages instead of splitting the implementation between the starter service and the main activity.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
I have problems with the BlePeripheral demo
The app crashes with this logs
Also I do not see the PlistExtra in the source: maybe the zip files were not updated?

B4X:
Copying updated assets files (1)
Application_Start
Application_Active
SignalHandler 6
Error occurred on line: 32 (Main)
Signal - 6
Stack Trace: (
    "0   B4i Example          SignalHandler + 120",
    "1   libsystem_platform.dylib            0x00000001fe81fc10 D5407B3F-F61C-3A1D-8A11-C713F8A9E920 + 11280",
    "2   libsystem_kernel.dylib              0x00000001efd20b4c F984BD5D-5EB8-3894-B57D-307EAB4E7B07 + 195404",
    "3   libsystem_kernel.dylib              0x00000001efd20b7c libsystem_kernel.dylib + 195452",
    "4   TCC                  45234C20-FBE5-3FD9-8B6B-A48371C107D6 + 26576",
    "5   TCC                  45234C20-FBE5-3FD9-8B6B-A48371C107D6 + 28296",
    "6   TCC                  45234C20-FBE5-3FD9-8B6B-A48371C107D6 + 16872",
    "7   libxpc.dylib         1E83D8C7-F19C-3365-8E69-FD77374BCF53 + 110968",
    "8   libxpc.dylib         1E83D8C7-F19C-3365-8E69-FD77374BCF53 + 61808",
    "9   libdispatch.dylib    ABE14CA7-0020-314D-BBA6-5F92F0BBB1C4 + 411712"
)
 

aeric

Expert
Licensed User
Longtime User
The updated B4X example based on B4XPages is here:
 
Top