![android | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/android-1.jpg?resize=1140%2C628&ssl=1)
Author: Vincent P596
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:UsersPublicDocumentsEmbarcaderoStudio14.0SamplesObject PascalRTLTetheringPhotoWall 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
procedure TForm1.Button1Click(Sender: TObject);<br> var<br> I : integer;<br> begin<br> // discover all managers<br> Button1.Enabled := false;<br> Label1.Caption := '0';<br> ListBox1.Items.Clear();<br> Label2.Caption := '0';<br> TotalProfiles := 0;<br> ListBox2.Items.Clear();<br> for I := TetheringManager1.PairedManagers.Count - 1 downto 0 do<br> TetheringManager1.UnPairManager(TetheringManager1.PairedManagers[I]);<br> // Tethering Port Range: 2020 to 2040<br> // Manager discovery default timeout is 1500 msec<br> TetheringManager1.DiscoverManagers(DiscoveryTimeoutSpinEdit.Value);<br> 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.
1 2 3 4 5 |
procedure TForm1.TetheringManager1RequestManagerPassword(const Sender: TObject;<br> const RemoteIdentifier: string; var Password: string);<br> begin<br> Password := '1234';<br> 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
procedure TForm1.TetheringManager1EndManagersDiscovery(const Sender: TObject;<br> const RemoteManagers: TTetheringManagerInfoList);<br> var<br> I: Integer;<br> begin<br> // end of managers discovery - list those found<br> // use RemoteManagers parameter to build ListBox1<br> Label1.Caption := IntToStr(RemoteManagers.Count);<br> for I := 0 to RemoteManagers.Count-1 do begin<br> ListBox1.Items.Add(RemoteManagers.List[I].ManagerName<br> + ' - '<br> + RemoteManagers.List[I].ConnectionString<br> );<br> TetheringManager1.PairManager(RemoteManagers[I]);<br> end;<br> Button1.Enabled := true;<br> end;<br> <br> procedure TForm1.TetheringManager1EndProfilesDiscovery(const Sender: TObject;<br> const RemoteProfiles: TTetheringProfileInfoList);<br> var<br> I: Integer;<br> begin<br> // Tethering Port Range: 2020 to 2040<br> // Profile discovery default timeout is 5000 msec<br> Inc(TotalProfiles);<br> // use RemoteProfiles parameter to build ListBox1<br> Label2.Caption := IntToStr(TotalProfiles);<br> for I := 0 to RemoteProfiles.Count-1 do begin<br> ListBox2.Items.Add(RemoteProfiles[I].ProfileText<br> + ' - '<br> + RemoteProfiles.List[I].ProfileIdentifier<br> )<br> end;<br> Button1.Enabled := true;<br> 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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition