Android Question Bluetooth Low Energy (BLE) library - BLE Manager scan duration, stop scan

foakgul

Member
Licensed User
Hello,

I was able to use the BLEmanager succesfully to scan for BLE MACs. I have 3 questions:

1) Are the MACs returned only for BLE devices (including iBeacon) or can they be regular bluetooth devices as well?

2) Default example sets scan period to 10 seconds. Does this mean the scanning is initiated every 10 seconds indefinitely? Or does it mean scanning continues for 10 seconds straight and then stops? How much time is actually spent to discover BLE devices?
B4X:
If manager.BleSupported Then
   manager.Scan(10000, Null)
End If
3) Is there a way to turn off BLEmanager, ie stop scan? Android API has this functionality thru stopScan(ScanCallback callback)

Thanks!
 

Erel

Administrator
Staff member
Licensed User
1) Only BLE macs. However you can use BluetoothAdmin to scan for other devices.

2) This means that the scan will stop after 10 seconds. The DeviceFound event will fire as soon as a device is found.

3) When you call BLE.Connect the scanning is stopped. It is possible to stop the scanning with Reflection library. However you can just ignore the event and it will stop automatically.
 

foakgul

Member
Licensed User
Basically I'd like to scan for BLE beacons and get their info:

1) MAC
2) name
3) RSSI
4) UUID
5) Major
6) Minor
7) Distance
8) Descriptor

So when a device is found I do BLE.connect but I'm not sure which characteristics correspond to the values above.

As soon as device info is obtained, I'd like to continue scanning as well.

Pseudocode of what I'd like to do:

B4X:
while not discovery finished
{
  scan
  if BLEfound
     {
        get all charactertistics
        read all characteristics
      }
}
Here's a code that I have but really not sure if I'm placing the readCharacteristic in the right place below. Should I let the BLE device return the BLE characteristic read event before searching for the next characteristic? Also when should I fire scan command again to search for other BLE devices?

B4X:
Sub BLE_Connected (Services As Map)
   For Each s As BleService In Services.Values
    For Each key As String In s.GetCharacteristics.Keys
     Log(key)
      Dim c As BleCharacteristic = s.GetCharacteristics.Get(key)
      manager.ReadCharacteristic(c)
    Next
   Next
End Sub

Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)
  Log("CR: " & Success & ": " & Characteristic.Uuid)
   If Success Then Log("cr: " & Characteristic.GetStringValue(0))
End Sub
Thanks!
 
Last edited:
Top