B4A Library BLE 2 - Bluetooth Low Energy

Discussion in 'Additional libraries, classes and official updates' started by Erel, Nov 2, 2015.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    This library replaces the previous BLE library.
    It is simpler to use and it is more powerful. Its API is based on B4i iBLE library which makes it easy to reuse B4i code.

    See the iBLE tutorial: http://www.b4x.com/android/forum/threads/ble-bluetooth-low-energy-library.46099/#content

    Tips & Notes

    - You can call Manager.Scan2 with AllowDuplicates set to True if you want to monitor the state of an unconnected device.
    - The AdvertisingData map in DeviceFound event holds pairs of integers as keys (data types) and bytes arrays as values.
    - Call ReadData2 if you are only interested in a single characteristic.
    - BLE is only supported on Android 4.3+.

    See the attached example. Note that the important code in in the Starter service module.

    Edit:

    A new version of BLE_Example was uploaded. targetSdkVersion is now set to 26.
    Setting the targetSdkVersion to 26 requires some changes:

    1. Add the coarse location permission in the manifest editor.
    2. Request this permission with RuntimePermissions.

    Otherwise scanning will fail with a message visible in the unfiltered logs.
     

    Attached Files:

    Last edited: Jan 17, 2018
    MarkusR, ema01, Humberto and 21 others like this.
  2. EApreda

    EApreda Member Licensed User

    Hi Erel
    I have a problem: My application on a ble device sends an Advertisement Packet with few bites of payload. Now i need to read this payload on my smartphone, whithout making connection.
    I have tried to read the advertising map value as your example, but i received only strange numbers:

    Found: Adafruit Bluefruit LE, F8:61:A6:3D:39:AD, RSSI = -59, (MyMap) {75=[B@41cb8ed8, 9=[B@41cb8f48}

    Can you help me please?
    thanks
    Enrico
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Looks good. You received a map with two items:
    Code:
    Dim data1() As Byte = AdvertisingData.Get(75)
    Dim data2() As Byte = AdvertisingData.Get(9)
    'you can use byte converter to convert to hex if you like:
    Dim bc As ByteConverter
    Log(bc.HexFromBytes(data1))
    Log(bc.HexFromBytes(data2))
     
    EApreda likes this.
  4. EApreda

    EApreda Member Licensed User

    Ok it works perfectly!!!
    My data are in:
    AdvertisingData.Get(75)

    thank you very much
     
  5. AndOrNot

    AndOrNot Well-Known Member Licensed User

    Great job! Can i read files that are storage on device, or read messages that are sended by arduino?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You can read any data that is sent over the BLE connection.
     
  7. AndOrNot

    AndOrNot Well-Known Member Licensed User

    How i can this? I connect correctly the device and in the example when i click on read data i can read the specific of the device which is connected, but i don't understand how read messages that are send by connected device.. Can you help me?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You will only be able read data that is being sent from the device. Which device are you asking about?
     
  9. AndOrNot

    AndOrNot Well-Known Member Licensed User

    I use an s6 edge to connect with an Arduino nano with Bluetooth service. I ve configured Arduino to send data (it sends example text). With android studio I ve connected the device and I can read example text that is sended by Arduino, but I don't know how to intercept this text with b4a. I only intercept the Arduino Bluetooth characteristics
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the relevant Java code that you were using with Android Studio?
     
  11. AndOrNot

    AndOrNot Well-Known Member Licensed User

  12. Erel

    Erel Administrator Staff Member Licensed User

    This is the same API used by this library. You should start a new thread in the questions forum and post the code you use and the data you receive.
     
  13. EApreda

    EApreda Member Licensed User

    Hi Erel
    I have a problem. I need to read data sent from a ble by uart. Using old bleextended library, it works perfectly, but using ble2 when calling ReadData i have: No matching characteristic found. Can you help me? thanks
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question and post the code you are running.
     
  15. Turbo3

    Turbo3 Active Member Licensed User

    Why does the information displayed by the B4A BLE_Example code look nothing like the information displayed by the B4i BLE_Example code for the exact same BLE device?

    The information read from the device should be the same, right?

    For example the first Services shown on the B4i app is "180A" but on the B4A app it is "00001800-0000-1000-8000-00805f9b34fb".

    The B4i service of 180A is a valid service but the one by B4A is not.

    The B4i version shows 2 services while the B4A version shows 3 services.

    ==============================================

    Just ran a test with another Android BLE app and get the same strange (i.e. not matching what I get on iOS with B4i) results. So it seems the data you get depends on the operating system and not totally on the device.
     
    Last edited: Nov 27, 2015
  16. Erel

    Erel Administrator Staff Member Licensed User

    These are the ids as reported by the OS.
     
  17. Turbo3

    Turbo3 Active Member Licensed User

    Now that I have my app working with BLE2 is there any way to stop getting the "Writing data to: ..." message to the log every time I write some data? It is going to make it very hard to see any message I write to debug a problem if I get hundreds of these messages filling the log.

    Writing data to: 0000ffe1-0000-1000-8000-00805f9b34fb, success = true

    There are also a large number of these messages too at the start.

    Ignoring characteristic: 00002a01-0000-1000-8000-00805f9b34fb

    Basically can we get rid of all these library debug messages now that the library is released?
     
    Last edited: Dec 2, 2015
  18. Erel

    Erel Administrator Staff Member Licensed User

    I've removed these log messages. The updated jar file is attached to the first post.
     
  19. Turbo3

    Turbo3 Active Member Licensed User

    Erel, Thanks, much better only the single "Discovering services." message left.
     
  20. stari

    stari Active Member Licensed User

    OK, i trayed but program searches for devices and search is never ended. In log i have:
    Installing file.
    PackageAdded: package:BluetoothExample.com
    Copying updated assets files (1)
    ** Service (starter) Create **
    ** Service (starter) Start **
    ** Activity (main) Create, isFirst = true **
    Class not found: b4a.example.customlistview, trying: BluetoothExample.com.customlistview
    ** Activity (main) Resume **
    ** Activity (main) Pause, UserClosed = true **
     
Thread Status:
Not open for further replies.
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