iOS Tutorial BLE Chat - Connecting Android and iOS

Discussion in 'iOS Tutorials' started by Erel, May 4, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User



    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:
    Code:
    Private Sub UUID(id As StringAs 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.

    The B4A program requires BLE2 v1.30+: https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/#content

    The B4i programs require iBLE v2.00+: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.48179/page-3#post-421081
     

    Attached Files:

  2. ilan

    ilan Expert Licensed 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?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. elpic76

    elpic76 Member Licensed 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
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The B4A BLE library currently doesn't support peripheral mode. Note that most Android devices don't support this feature.
     
  6. Hypnos

    Hypnos Active Member Licensed 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!
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can use regular / classic Bluetooth for Android to Android.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice