Android Question BLE2 library - update?

wes58

Active Member
Licensed User
Longtime User
Will BLE2 library be updated to conform to Google's API level?
Method used in BLE2 - startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) was deprecated in API level 21, now we are at 33?

The new method startScan (List<ScanFilter> filters, ScanSettings settings, ScanCallback callback) allows setting of scan filter to restrict scan results to only those that are of interest.
Current filtering on the following fields are supported:
  • Service UUIDs which identify the bluetooth gatt services running on the device.
  • Name of remote Bluetooth LE device.
  • Mac address of the remote device.
  • Service data which is the data associated with a service.
  • Manufacturer specific data which is the data associated with a particular manufacturer.
  • Advertising data type and corresponding data.
Another parameter is ScanSettings which allows to define parameters for the scan.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Will BLE2 library be updated to conform to Google's API level?
The fact that a method is deprecated doesn't mean that it doesn't conform to Google API level. It actually has nothing to do with "Google's API level".

Replacing it with the newer method means that the library will no longer be compatible with older devices. Not critical in this specific case but it will mean that a few developers will no longer be able to use the library.
There are examples in the forum of using the newer API with JavaObject.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
After spending some time searching and reading, before getting your response, I found your post https://www.b4x.com/android/forum/threads/ble-scansettings.94908/#post-600231.
So solution to the updating library was (because it was 2018) easy. What you had in Java object you could have added to the library, And everybody would be happy - because you would have two options to choose from.
Your software is called Basic for Android - so if you can avoid using Java Object you should do it. That's what updating the library would do for ALL the users.
As the saying goes - Where there is a will, there is a way.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
There are examples in the forum of using the newer API with JavaObject.
I was very hopeful that I found solution, but not for long.
In your code there is a function:
B4X:
Private Sub Scan_Result (Result As Object)
   Log("Scan_Result")
   Dim ScanResult As JavaObject = Result
   Dim device As JavaObject = ScanResult.RunMethod("getDevice", Null)
   Dim address As String = device.RunMethod("getAddress", Null)
   ManagerJO.GetFieldJO("devices").RunMethod("put", Array(address, device))
   Dim name As String
   Dim o As Object = device.RunMethod("getName", Null)
   If o = Null Then name = "" Else name = o
   Dim ScanRecord As JavaObject = ScanResult.RunMethod("getScanRecord", Null)
   Log(ScanRecord) 'you can extend the code to access the data.
   'https://developer.android.com/reference/android/bluetooth/le/ScanRecord
   Dim rssi As Double = ScanResult.RunMethod("getRssi", Null)
   Manager_DeviceFound(name, address, CreateMap(), rssi)
End Sub

The last line is the call that supposed to provide Adevertising Data - Manager_DeviceFound(name, address, CreateMap(), rssi)
But is just gives an empty map - CreateMap()
The whole point of scanning was to get advertising data!

So, I follow the link provided in this function to get advertising data:
Log(ScanRecord) 'you can extend the code to access the data.
'https://developer.android.com/reference/android/bluetooth/le/ScanRecord
And information is:
Map<Integer, byte[]> getAdvertisingDataMap()
Returns a map of advertising data type and its corresponding advertising data.
First I search the forum for "getAdvertisingDataMap". What I get is 7 pages of no results. How did the search engine come out with "Google Map" for a search term "getAdvertisingDataMap"?
Then I call Dim data As Object = ScanRecord.RunMethod("getAdvertisingDataMap", Null)
The result is HashMap<integer, byte[]> - {22=[B@28e36ed, 9=[B@397c922}
How to convert HashMap, or get data from HashMap with int value 22?
Searching the forum and trying different things gets me nowhere. Unfortunately, you created a Map object which is not compatible with Android Java HashMap!
I wasted another couple of hours.

No wonder that you put an empty map in the call Manager_DeviceFound(...)
Now you see that average forum user can't do much with this, and that updating/adding new features created by Google to the library would benefit all users
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Ok, Solved my issue.
In Sub Scan_result():
1. To get specific advertising data - in my case 22
B4X:
Dim ScanRecord As JavaObject = ScanResult.RunMethod("getScanRecord", Null)
Dim AdvertisingDataMap As JavaObject = ScanRecord.RunMethodJO("getAdvertisingDataMap", Null)    '"getBytes", Null)
Log("AdvertisingDataMap " & AdvertisingDataMap)
Dim mb() As Byte = AdvertisingDataMap.RunMethod("get", Array(22))      'Byte[]=HashMap.get(int)
Dim bc As ByteConverter
Log("bc "  & bc.HexFromBytes(mb))

2. Or to use a call to Manager_DeviceFound()
B4X:
Dim ScanRecord As JavaObject = ScanResult.RunMethod("getScanRecord", Null)
Dim AdvertisingDataMap As Object = ScanRecord.RunMethod("getAdvertisingDataMap", Null)    '"getBytes", Null)
Log("AdvertisingDataMap " & AdvertisingDataMap)
Dim rssi As Double = ScanResult.RunMethod("getRssi", Null)

Dim m As Map
m.Initialize
Dim mo As JavaObject = m
mo.RunMethod("putAll", Array(AdvertisingDataMap))
Manager_DeviceFound(name, address, m, rssi)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…