0

I'm testing tethering on Delphi.

Everything works well in Windows 10 applications (both standard application and service). Everything is also fine in the standard android app.

Problems are in Android service. I can connect from external client to tethering in android service, receive and send resources and etc. But the following methods and events are not working: connection from client out (AutoConnect), OnEndManagersDiscovery, OnEndProfileDiscovery, OnNewManager does not fire and etc..

I couldn't figure out any reason why this was happening ...

Anybody have any idea where the bug could be?

pudnivec74
  • 845
  • 1
  • 8
  • 22
  • Possibly permissions. What version of Android? Use a logcat viewer to see what error messages (if any) arise. I have one here: https://github.com/DelphiWorlds/DeviceLens – Dave Nottage Feb 09 '20 at 02:01
  • Tested on Android 6.0, 8.0.0 and 9.0. Permissions (ACCESS_NETWORK_STATE, READ_PHONE_STATE) are set (for the service - although unnecessary) as well as for the standard Android application (which works perfectly without problems). Android device monitor does not report any error message. – pudnivec74 Feb 09 '20 at 06:43
  • Device Lens (ver. 1.1.0) does not report any error message too. – pudnivec74 Feb 09 '20 at 07:04

2 Answers2

0

I was unable to do so, so I examined the source code of System.Tether.Manager (the same goes for System.Tether.AppProfile) in detail, and most likely I found the cause of the problem.

When the TetheringManager.DiscoverManagers command is executed, no event occurs, but if the TetheringManager.RemoteManagers statement is executed after this command, all surrounding devices are listed.

The problem is that the library uses the TThread.Synchronize function to synchronize events. This feature requires a main UI thread for its functionality. This functionality does not have an android service. Therefore, the event update fails every time (for example, onEndManagersDiscovery). The library is not intended for use in the android service.

procedure TTetheringManager.DoEndManagersDiscovery(const ARemoteManagers: TTetheringManagerInfoList);
begin
  RegisterManagers(ARemoteManagers);
  if Assigned(FOnEndManagersDiscovery) then
  begin
    if SynchronizeEvents then
      TThread.Synchronize(nil,
        procedure
        begin
          FOnEndManagersDiscovery(Self, ARemoteManagers);
        end)
    else
      FOnEndManagersDiscovery(Self, ARemoteManagers);
  end
end;

Interestingly, for example, the OnRequestStorage event works because it uses directly FOnRequestStorage(Self, AStorage) instead of TThread.Synchronize to update the event.

procedure TTetheringManager.DoRequestStorage(var AStorage: TTetheringCustomStorage);
begin
  AStorage := nil;
  if Assigned(FOnRequestStorage) then
    FOnRequestStorage(Self, AStorage);
end;

Update: Now I have found that the easiest way to solve this problem will be to disable SynchroniyeEvents for booth TetheringManager and TetheringAppProfile.

pudnivec74
  • 845
  • 1
  • 8
  • 22
  • "The library is not intended for use in the android service." Interestingly, that was my first hunch when I read your question earlier. – Freddie Bell Feb 09 '20 at 13:36
0

if you do an AutoConnect without a timeout than the Event for EndofAutoConnect is fired! found that today ...