Skip to content

Finding your Tethered Apps

RAD Studio XE6, Delphi XE6 and C++Builder XE6 include support for tethering your VCL and FMX apps together. This blog post shows you how to display all of the tethered apps that you can find on your subnet. App Tethering includes two components: TTetheringManager and TTetheringAppProfile. For this article I’ve built a Delphi VCL application that uses these two components to discover the other tethered apps that are running on the my subnet. The example tether application demo I used is the PhotoWall sample that ships with the release (you can find it in the C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\Object Pascal\RTL\Tethering\PhotoWall folder)

Here is the main form for my application showing a TButton, TSpinEdit, two TListbox and some TLabels.  The TSpinEdit is used to set the timeout for the TetheringManager.DiscoverManagers optional second parameter.

The app tethering discovery works by using UDP to broadcast a discovery message across ports 2020-2040. The default timeout for the manager discovery period is 1500 milliseconds. The default timeout for the profiles discovery is 5000 milliseconds. Make sure that you open up the app tethering ports in your firewall if they are not already open. If your network is busy, you might want to set the discovery timeout longer than 1500 milliseconds (I normally use 3000 milliseconds at work and the default at home).

The TButton onClick event handler code is very straightforward:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I : integer;
    begin
      // discover all managers
      Button1.Enabled := false;
      Label1.Caption := '0';
      ListBox1.Items.Clear();
      Label2.Caption := '0';
      TotalProfiles := 0;
      ListBox2.Items.Clear();
      for I := TetheringManager1.PairedManagers.Count - 1 downto 0 do
        TetheringManager1.UnPairManager(TetheringManager1.PairedManagers[I]);
        // Tethering Port Range: 2020 to 2040
        // Manager discovery default timeout is 1500 msec
      TetheringManager1.DiscoverManagers(DiscoveryTimeoutSpinEdit.Value);
    end;

In my Find Tethered Apps application I provide three event handlers for my TetheringManager: OnEndManagersDiscovery, OnEndProfilesDiscovery and OnRequestManagerPassword.

If your Tethered Apps have a password set in their TetheringManager property, you’ll need to provide a n OnRequestManagerPassword event handler to get any responses from the remote tether apps.

    procedure TForm1.TetheringManager1RequestManagerPassword(const Sender: TObject;
      const RemoteIdentifier: string; var Password: string);
    begin
      Password := '1234';
    end;

After the call to DiscoverManagers, the app tethering technology listens for replies from other TetherManagers and TetherProfiles. When the timeout period completes, the OnEndManagersDiscovery and OnEndProfilesDiscovery events are executed. The event handlers code displays the TetherManagers with their IPaddress and Port and the TetherProfiles with their GUID.

    procedure TForm1.TetheringManager1EndManagersDiscovery(const Sender: TObject;
      const RemoteManagers: TTetheringManagerInfoList);
    var
      I: Integer;
    begin
      // end of managers discovery - list those found
      // use RemoteManagers parameter to build ListBox1
      Label1.Caption := IntToStr(RemoteManagers.Count);
      for I := 0 to RemoteManagers.Count-1 do begin
        ListBox1.Items.Add(RemoteManagers.List[I].ManagerName
          + ' - '
          + RemoteManagers.List[I].ConnectionString
        );
        TetheringManager1.PairManager(RemoteManagers[I]);
      end;
      Button1.Enabled := true;
    end;
    
    procedure TForm1.TetheringManager1EndProfilesDiscovery(const Sender: TObject;
      const RemoteProfiles: TTetheringProfileInfoList);
    var
      I: Integer;
    begin
        // Tethering Port Range: 2020 to 2040
        // Profile discovery default timeout is 5000 msec
      Inc(TotalProfiles);
      // use RemoteProfiles parameter to build ListBox1
      Label2.Caption := IntToStr(TotalProfiles);
      for I := 0 to RemoteProfiles.Count-1 do begin
        ListBox2.Items.Add(RemoteProfiles[I].ProfileText
          + ' - '
          + RemoteProfiles.List[I].ProfileIdentifier
        )
      end;
      Button1.Enabled := true;
    end;

For my test run, I started the VCL and FMX PhotoWall desktop apps and then started the mobile PhotoWall apps on my iPhone and Android phone. The resulting display shows the four tethered apps in my FindTetheredApps demo along with pictures that I took on my two mobile devices and sent to the two desktop apps.

The source code to my Delphi Find Tethered Apps project is available in CodeCentral at http://cc.embarcadero.com/item/29849. Have fun adding app tethering to your VCL and FMX Delphi and C++ XE6 applications.

{ 4 } Comments

  1. Mark | April 28, 2014 at 5:08 pm | Permalink

    When’t the BlueTooth version going to be available?

  2. David Intersimone | April 28, 2014 at 5:19 pm | Permalink

    Mark - R&D is working on blue tooth support for the future. When? When it is ready. The architecture underpinnings are there. Note the use of the "profiles" in the network version for this release :)

  3. Torbins | April 30, 2014 at 5:52 am | Permalink

    "discovery works by broadcasting a message across TCP ports 2020 to 2040"
    ? Probably broadcasting is done via UDP, and the rest of communication via TCP.

  4. David Intersimone | April 30, 2014 at 11:48 am | Permalink

    Torbins - you are right - we use UDP for the broadcast on the subnet and then TCP/IP for the communications. I will update my blog post to make this even clearer than just using the "broadcast" word and assuming everyone knows this means UDP :) I should also learn how to spell "milliseconds" - I fixed those typos as well :(

{ 2 } Trackbacks

  1. [...] created a blog post back in April, "Finding your Tethered Apps", that contains additional information about App Tethering and shows you how to find tethered apps [...]

  2. [...] David I blog – “Finding your Tethered Apps” [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 4 access attempts in the last 7 days.

Close