10.2 Tokyo handles BLE discovery on Android differently than 10.1 Berlin.

0

(My apologies for this lengthy entry)

I have had some problems getting BLE to work according to my needs on both iOS and Android, but after some work arounds (see my comment and issues on quality.embarcadero.com: RSP 16891, 17763, and 17764) things have worked quite well (although BLE on iOS seems more stable than on Android).

Having switched to Tokyo, However, I have had to revise my handling of BLE device discovery, and I wonder if anyone else has noticed this change, if my original way of handling things (which was convenient) was fundamentally wrong, and/or if the behavior on Tokyo is as intended and by design or a bug.

In my application, I often need to "rediscover" a BLE device that was "defined" a long time ago (often in a previous app session). I handle this by storing the device name and MAC address so that I quickly can run discovery using a TBluetoothLEScanFilterList with the proper LocalName and DeviceAddress at some later point in time when I need to communicate with the device.

Using Berlin, I simply used a procedure like InitializeMyDevice below where BluetoothLE1 is a TBluetoothLE, FMyBleDevice is a TBluetoothLEDevice, and BluetoothLE1 has an OnDiscoverLEDevice = BluetoothLE1DiscoverLEDevice. Everything works fine: if the device is found, descovery stops immediately; if not, discovery times out.

Using Tokyo, however, the same code doesn't work:  

1. during Application.ProcessMessages the bleMainDiscoverLEDeviceis never called and the discovery always times out;

2. after timeout and exit of InitializeMyDevice all calls to bleMainDiscoverLEDeviceis do occur, but this is "too late".

Of course, this issue can be circumvented using, e.g., a second procedure to capture the result, but the "Berlin" method was quite simple and convenient.

So, hopefully some of you can have a look at the code below and explain to me what's going on!

Thanks in advance /Per

Code:

procedure BluetoothLE1DiscoverLEDevice(...)

begin

  // Double check

  if ((ADevice.DeviceName = MyDeviceName) and (ADevice.Address = MyMacAddress)) then

  begin

    FMyBleDevice := ADevice;

    BluetoothLE1.CancelDiscovery;

  end;

end;

function InitializeMyDevice: Boolean;

var

  LBleFilterList: TBluetoothLEScanFilterList;

  LBleFilter: TBluetoothLEScanFilter;

begin

  LBleFilterList := TBluetoothLEScanFilterList.Create;

  LBleScanFilter := TBluetoothLEScanFilter.Create;

  LBleScanFilter.LocalName := MyDeviceName;

  LBleScanFilterList.Add(LBleScanFilter);

  LBleScanFilter.DeviceAddress := MyMacAddress;

  LBleScanFilterList.Add(LBleScanFilter);

  try

    FMyBleDevice := nil;

    BluetoothLE1.DiscoverDevices(3000, LBleScanFilterList);

    while (FMyBleDevice = nil) do

       Application.ProcessMessages;

     Result := (FMyBleDevice = nil);

  finally

    LBleScanFilterList.Free;

  end;

end;  

Responses (3)
  • Accepted Answer

    Saturday, June 24 2017, 01:17 PM - #Permalink
    0

    More Follow UP:  I have installed the Toolchain HotFix for 10.2 and it still will NOT discover any BLE devices.  I then installed the 6/23/17 Android patch and still will NOT discover any BLE devices.  Will be filling an issue report in the quality central now.

    The reply is currently minimized Show
  • Accepted Answer

    Saturday, June 24 2017, 11:35 AM - #Permalink
    0

    Follow UP:

    On a different computer I have installed 10.1 (update2) and 10.2 (No Hotfix).  No additional options were installed on either one except what was available in the installation menu.  So they are clean installations.  Win32, Win64 and Android were installed.  On the10.1 compiler the BLE project complies, installs and runs, finding all available BLE devices.  Using the exact same project in 10.2, the project complies, installs and runs, except it does not find any BLE devices.  The OnDiscoveryEnd event does fire, but it returns no BLE devices.

    So now I am at a loss to understand this. Any help would be greatly appreciated.

    The reply is currently minimized Show
  • Accepted Answer

    Tuesday, June 20 2017, 05:46 PM - #Permalink
    0

    I have also experienced that after changing to 10.2, BTLE discovery is not working anymore in Android.  I am using the framework and calling

    System.Bluetooth.TBluetoothLEManager.StartDiscovery

    It does return TRUE and the OnDiscoveryEnd event does fire, but no devices are found.  I have used the HeartRate sample and it does find the BTLE device, but it is using the BTLE Component, which looks like a major change for me. 

    I was just wondering if you ever solved your problem.  Thanks and good luck with your development.

    UPDATE:  Actually I have to use the Framework, since part of my app is in a service and UI is not allowed in a service.

    The reply is currently minimized Show
Your Reply

Please login to post a reply.........