Author Topic: Bluetooth Device Discovery Hangs  (Read 5314 times)

Offline agentnobo

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Bluetooth Device Discovery Hangs
« on: January 08, 2007, 11:17:25 PM »
Hello, I'm evaluating your library to use it with outsourcing project.

I downloaded evaluation version and played with code.

One feature requirement for project is "constant detection for new and lost devices". Since library works in synchronous mode I created a thread that scans for devices in a loop and compares two device list - before and after the scan. Note that customer requirements is to use most of available bluetooth stacks (WIDCOMM preferred) and my code should be as universal as possible, and I can't use monitoring features available for MS stack only.

My code looks like this:

Code: [Select]
procedure TMonitoringThread.Execute;
begin
  while not Terminated do
  begin
    FNewestBTDeviceList := FBFBluetoothDiscovery.Discovery(False, True);
    <some code here comparing list of devices>
  end

where FBFBluetoothDiscovery - object of TBFBluetoothDiscovery that is class member and constructed in thread constructor:

Code: [Select]
constructor TMonitoringThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FBFBluetoothDiscovery := TBFBluetoothDiscovery.Create(nil);
end;

My primary development environment equipped with Logitech Bluetooth 2.0+EDR dongle and WIDCOMM  5.0.1.2500 drivers.

I tested the code and found that it works unreliable, it unpredictably hangs on FBFBluetoothDiscovery.Discovery method - thread just "freezes forever". Thread can make number of normal iterations but it freezes in 100% of cases - sooner or later.

I tested the code outside the thread and result is the same.

I played with BluetoothDiscovery demo and found that it also freezes if I make many menu cals "Discovery->Discovery".

I downloaded and tested multithreading AdSender demo suggested in this forum and found that it also freezes after some cycles of discovering for new devices.

But If I use "View devices in range" of "My Bluetooth Places" and updating it (with F5) while turning on and off devices, I can see that it works normally without dependence of how many times I'm updating it (I made *really* a lot of updates to make sure that it works normally).

I also tested some thirdparty software scanning for bluetooth devices and it works well.

So I'm completely sure that it is a bug in Bluetooth Framework and not in my code or machine configuration. Please review it or make suggestions.

Thank you, your library really looks promising!

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #1 on: January 09, 2007, 01:25:06 AM »
Good time of the day!

Thanks you for bag report. It is already fixed.

P.S. The BFBluetoothDiscovery component has methods and events for device monitoring. You need set handler for OnDeviceFound and OnDeviceLost events. And then call StartMonjitoring method (StopMonitoring to stops).
« Last Edit: January 09, 2007, 02:12:06 AM by Mike Petrichenko »
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline downstruck

  • Newbie
  • *
  • Posts: 10
  • Karma: 0
Re: Bluetooth Device Discovery Hangs
« Reply #2 on: January 09, 2007, 07:23:08 AM »
I also implement bluetooth discovery in thread. I don't think StartMonitoring/StopMonitoring will be fit in thread. I use Discovery method instead. The TBFBluetoothDiscovery object is created and destroyed in thread execution to make sure I always get fresh list and avoid unexpected error.

Here's my code:

Code: [Select]
procedure TDiscoverThread.Execute;
var
  BDL: TBFBluetoothDevices;
  BD: TBFBluetoothDevice;
  i: Integer;
  SL: TStringList;
  BFBD: TBFBluetoothDiscovery;
begin
  try
    BFBD := TBFBluetoothDiscovery.Create(Nil);
 
    try
      BDL := BFBD.Discovery(False, True);
 
      if (BDL <> Nil) then begin
        for i := 0 to BDL.Count - 1 do begin
          BD := BDL.Device[i];
          SL := TStringList.Create;
 
          try
            SL.Add(BD.Name);
            SL.Add(BD.Address);
            SL.Add(BD.ClassOfDeviceName);
            FDevices.Add(SL.CommaText);
 
          finally
            SL.Free;
          end;
        end;
 
        BDL.Free;
      end;
 
      FDoneMsg := FDevices.CommaText;
      FSuccess := True;
 
    finally
      BFBD.Free;
    end
 
  except
    on E: Exception do begin
      FDoneMsg := 'Discover is error. ' + E.Message;
      FSuccess := False;
    end;
  end;
end;

So far I have been messing with TBFBluetoothDiscovery class, I think I won't use built-in monitoring function.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #3 on: January 09, 2007, 07:27:55 AM »
Good time of the day!

Well. You can do it like you want :) But monitoring works in thread and have several things to fix some device detection bugs in MS and BlueSoleil stack :)
Your method can report that device still in range for MS stack.

See BluetoothDiscovery demo application for how to use and how is work StartMonitoring/StopMonitoring.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #4 on: January 09, 2007, 08:00:14 AM »
In additions to my previouse post. This is the code from Monitoring thread of TBFBluetoothDiscovery component:

Code: [Select]
procedure TBFBluetoothMonitoringThread.Execute;
var
  Discovery: TBFBluetoothDiscovery;
  OldDevices: TBFBluetoothDevices;
  NewDevices: TBFBluetoothDevices;
  Devices: TBFBluetoothDevices;
  Loop: Integer;
  ADevice: TBFBluetoothDevice;
begin
  PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, 0, NM_DISCOVERY_BEGIN);

  // Initialization.
  Discovery := TBFBluetoothDiscovery.Create(nil);
  OldDevices := TBFBluetoothDevices.Create;
  NewDevices := TBFBluetoothDevices.Create;

  while not Terminated do begin
    PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, 0, NM_DISCOVERY_SEARCH_BEGIN);

    // Trying discovery devices.
    try
      Devices := Discovery.Discovery(False, FNeedServices);
    except
      Devices := nil;
    end;

    if Assigned(Devices) then begin
      // Building new devices list. We adds only connected devices. Not
      // remembered.
      for Loop := 0 to Devices.Count - 1 do
        if (API.BluetoothAPI in [baBlueSoleil, baWidComm]) or ((API.BluetoothAPI = baWinSock) and Devices[Loop].Connected) then begin
          ADevice := TBFBluetoothDevice.Create;
          ADevice.Assign(Devices[Loop]);
          NewDevices.FList.Add(ADevice);
        end;

      if FAlwaysNew then
        for Loop := 0 to NewDevices.Count - 1 do begin
          // All founded devices reports as New.
          ADevice := TBFBluetoothDevice.Create;
          ADevice.Assign(NewDevices[Loop]);

          // Main thread must dispose object.
          PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, Integer(ADevice), NM_DISCOVERY_DEVICE_FOUND);
        end
       
      else
        // Checks for new founded devices.
        for Loop := 0 to NewDevices.Count - 1 do
          if OldDevices.IndexOf(NewDevices[Loop].FBTAddress) = -1 then begin
            // New device found. Send message to main thread about this fact.
            // Main thread MUST dispose object.
            ADevice := TBFBluetoothDevice.Create;
            ADevice.Assign(NewDevices[Loop]);

            PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, Integer(ADevice), NM_DISCOVERY_DEVICE_FOUND);
          end;

      // Check for losted devices.
      for Loop := 0 to OldDevices.Count - 1 do
        if NewDevices.IndexOf(OldDevices[Loop].FBTAddress) = -1 then begin
          // Old device losted. Send message to main thread about this fact.
          // Main thread MUST dispose object.
          ADevice := TBFBluetoothDevice.Create;
          ADevice.Assign(OldDevices[Loop]);

          PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, Integer(ADevice), NM_DISCOVERY_DEVICE_LOST);
        end;

      // Now nes devices be a old devices.
      OldDevices.Assign(NewDevices);
      NewDevices.FList.Clear;

      // Dispose object.
      Devices.Free;
    end;

    PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, 0, NM_DISCOVERY_SEARCH_END);

    // Delay.
    Sleep(FDiscovery.FDelay);
  end;

  // Finalization.
  NewDevices.Free;
  OldDevices.Free;
  Discovery.Free;

  PostMessage(FDiscovery.Wnd, BFNM_BLUETOOTH_DISCOVERY_EVENT, 0, NM_DISCOVERY_END);
end;
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline downstruck

  • Newbie
  • *
  • Posts: 10
  • Karma: 0
Re: Bluetooth Device Discovery Hangs
« Reply #5 on: January 09, 2007, 05:20:18 PM »
Actually my current development target are Windows 2000 PCs, where there is no Microsoft Stack for that platform. We use WidComm and my code is the most efficient meaning accurate and error free. Yes, I had tried the demo your mentioned (and other demos) since I downloaded your product. I have reached final decision that I should always put BTF components in threads. Well, perhaps it's just my personal taste.

I am kinda surprised that you don't use proper try-finally block for dynamic creation object. Are you sure there won't be any garbage or, even worse, unhandled exception?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #6 on: January 09, 2007, 05:37:04 PM »
Ah. OK. For WidComm you choise very good solution. My code must be universal. But of cause your will be better for your tasks.
Why you think that i am not use try/except? It used. But not in this part of code. In this part error is impossible. :) All needed tests for correct params and so on makes in other part BEFORE creating thread.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline agentnobo

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Bluetooth Device Discovery Hangs
« Reply #7 on: January 09, 2007, 09:55:31 PM »
Today I make comprehensive test for this problem with the new version of BTF 5.0 and my WIDCOMM stack on WinXP SP2 machine.
Good news - I'm not completely sure but can say that problem with Discovery method freezing disappears! I tested my code, code posted by downstruck and Bluetooth disovery demo and can't reproduce the problem.  8)

While making tests I've got a new problem - monitoring events (or discovery method) often reports about false losts of devices - the device was available all time and it founds on next first (rarely second) iteration. It is interesting that this problem specific only to some devices, for example for my smartphone lost/found triggers one time for 7-10 minutes but usual phone does this more frequently - one time for 2-4 minutes. Some devices are *never* lost and all OK with them.

I'm sure that is not BTF bug but device-related problem. But anywhay it should be solved.
Are any methods to improve reliability for device discovering (maybe some timeouts turning etc.)? Or maybe it is possible to quickly re-test only one specific device, not all of them?
Waiting for any comments or suggestions. ???

Thank you!

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #8 on: January 09, 2007, 10:29:52 PM »
Good time of the day!
Yes. You right. This is device problem. For example Siemens need about 2 min for reset bluetooth stack (if it freez and it freezing very often on my Siemens S65 :).

There is Interval property which used for intervals between each discovery loop. Try to set it longer.

P.S. The usage of Monitoring is depended from your tasks. If you need just see is device available then yes. Use this method. But if you need discovery devices and then send to them something - the better way is manual discovering.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline agentnobo

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Bluetooth Device Discovery Hangs
« Reply #9 on: January 09, 2007, 11:53:22 PM »
Thank you for your quick answer!

I have to maintan list of currently available devices and monitoring suits me OK.
While trying to increase the interval only make things worse - if device lost I have to wait longer time to found it.
IMHO discovery can't find the device not for bluetooth stack resetting but for inadecvate timeouts somewhere in drivers. For example I tried to extensively use the phone and number of lost/found triggers increases comparing to stanby mode. Looks like device can't answer to discovery request in time.

Maybe it is possible to re-test only one device with specific BT phisical address somehow? Something like Device.IsAlive?

Thank you!

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
  • Karma: 1000
    • Wireless Communication Library
Re: Bluetooth Device Discovery Hangs
« Reply #10 on: January 10, 2007, 01:03:46 AM »
Good time of the day!

Unfortunatelly. This is not drivers problems. Because the same situation with any drivers (MS, WidComm, BlueSoleil).
Fo example. My Nokia 3230 works fine any time. But Siemens S65 didn't work sometime. This depended on device.

May be the solution is report that device losted on second OnDeviceLostEvent.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager