B4i Library BLE (Bluetooth Low Energy) library

Discussion in 'iOS Libraries' started by Erel, Oct 26, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    B4i BLE library is now available. This library allows you to connect and read data from BLE peripheral devices.

    Using this library is quite simple. There are three main steps:
    1. Search for devices (assuming that the state is STATE_POWERED_ON).
    2. Connect to a device.
    3. Read data from the device.

    There are several events which you need to handle:
    StateChanged (State As Int) - This event is raised when you initialize BleManager and when the BLE adapter state changes. Only if the state is STATE_POWERED_ON then you can use this feature.

    DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double) - Raised when a new device is discovered (after you call BleManager.Scan). The event parameters include the device Id, name, RSSI value and a Map with additional advertised data.

    Connected (Services As List) - Raised when a device is connected. The Services list holds the device service ids. The data on the device is stored in a tree like structure made of services that hold characteristics.

    DataAvailable (Service As String, Characteristics As Map) - Raised after you call Manager.ReadData. Service is the service that was read and Characteristics is a Map that holds the characteristics ids and values. The values are arrays of bytes.

    Disconnected - Raised after a failed connection or after a device has disconnected.

    Make sure to see the video in HD mode (click on the small gear button).

    iBLE is now included as a preinstalled library.

    Attached Files:

    Last edited: Oct 23, 2017
  2. walterf25

    walterf25 Well-Known Member Licensed User

    Great Job Erel, is it possible to communicate with bluetooth devices in the way its possible with android, I mean, can i connect to a bluetooth module to read and write to it? I did some research a long time ago and from what i understood you need to apply for some ID chip in order to make that possible, do you know anything about that?

    If it is possible i will soon be playing around with B4i, that would be the only reason why i would mess with B4i.

    I'll wait for your reply.

  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes. Normal developers (like us) cannot access the SPP profile which is the one usually used for custom Bluetooth communication. Only BLE is exposed. You can write and read to BLE devices. You cannot open an input or ouput stream like you do in Android.
  4. walterf25

    walterf25 Well-Known Member Licensed User

    In other words I wouldn't be able to write an app to control any light switches etc... through a bluetooth module connected to a microcontroller????

  5. Erel

    Erel Administrator Staff Member Licensed User

    Not exactly. There are many BLE adapters that allow you to do all kinds of things.

    I'm sure that there are BLE adapters for home automation.
    f0raster0 likes this.
  6. tuhatinhvn

    tuhatinhvn Active Member Licensed User

    i dont understand why library only have a file xml , in b4a we have 2 files (jar and xml). In b4i i want to code lỉbrary in xcode and use for my app in b4i,how to do it? Which files i need to create
    KY Leng likes this.
  7. moster67

    moster67 Expert Licensed User


    I suspect libraries (and local installation of the same) will be available when the version of B4I which permits compiling on local network will be released. For now, compilation (with libraries) probably takes place remotely and are probably added to the code. This is also easier for Erel in this initial phase, since he can easily fix bugs without publishing new libraries for all users to download.

    It is actually a genius way of Erel for beta-testing B4i ;)
  8. Erel

    Erel Administrator Staff Member Licensed User

    Libraries are actually made of three files: binary file, header file and xml file (generated automatically from the header file).

    The two first files are stored on the Mac compiler.
    tuhatinhvn likes this.
  9. tuhatinhvn

    tuhatinhvn Active Member Licensed User

    ok i understood
  10. Angel Maza

    Angel Maza Member Licensed User

    I've some problems using the characteristics values:
    Adding some code in the Sub Manager_DataAvailable, into the For Each id As String In Characteristics.Keys loop:
    For each abc As Byte In Characteristics.Get(id)
    When compiling I receive next error:
    initializing '__strong id<B4IIterable>' with an expression of incompatible type 'NSObject *'
  11. Angel Maza

    Angel Maza Member Licensed User

    Solved with iRandomAccessFile lib and ByteConverter
    Sub Manager_DataAvailable (Service As String, Characteristics As Map)
       ActivityIndicator2.Visible = 
    Dim bc As ByteConverter
    Service), 30"")
    For Each id As String In Characteristics.Keys
    id, Characteristics.Get(id)), 40"")
    Log( bc.HexFromBytes( Characteristics.Get(id) ) )
    End Sub
    Last edited: Oct 29, 2014
    Erel likes this.
  12. ilan

    ilan Expert Licensed User

    is it possible to send data to another device via bluetooth with this lib?
    would like to send text from one device to another is this possible?
  13. Erel

    Erel Administrator Staff Member Licensed User

    Currently it is not possible with this library.

    Even when all of the BLE features will be implemented it will still not be so useful for communication between devices unless you need to send very small messages.
  14. ilan

    ilan Expert Licensed User

    yes i need to send very small messages.. will be great if this could be possible.
    i would like to make a multiplayer game and need to send game progress from one device to other...
  15. bubels

    bubels New Member Licensed User

    I can see that data read is possible but what about sending ?
    I use modules that support BLE and 2.1 (dual). The manufacturer provides a program (ios) that allows the read and send ASCII string.
    Anyway, I'm doing the same thing with the android in b4a
    KY Leng likes this.
  16. Erel

    Erel Administrator Staff Member Licensed User

    Support for writing values will be added.
    riccf likes this.
  17. bubels

    bubels New Member Licensed User

    It's good news for me. Can you tell some thing about time? We are holding our project
    because we want to work in one platform.
    We have ready software for android but we need the same for IoS.
  18. Erel

    Erel Administrator Staff Member Licensed User

    I still don't know when it will be ready. Sorry.
  19. Erel

    Erel Administrator Staff Member Licensed User

    V1.10 is now available. It adds support for writing. It is a beta version.

    There is one new method named WriteData.
    This method expects three parameters: the service name, the characteristic uuid and the data.

    You must call ReadData once and wait for the DataAvailable event to be ready before you can call WriteData.

    Library installation

    The zip file contains three files with the following extensions: .xml, .a and .h.
    You need to copy the xml file to the internal libraries folder on the Windows computer.
    If you are using a local Mac builder then you need to copy the .a and .h files to the Mac Libs folder.
    chardenal likes this.
  20. andreahaku

    andreahaku Member Licensed User

    Thanks a lot Erel!
    I'll test it this week and let you know how it works.
    What about having write supported on BLE library for B4A?
  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