Author: Luis Navarro
I’ve always enjoyed working with Delphi and RAD Studio to create Windows applications. However, with the latest releases, it is possible to go beyond Windows and create a new ecosystem that includes the capabilities of mobile apps, IoT devices, and so on…
RAD Studio 10 Seattle introduced the Android services feature, and I’ve been thinking about connecting a Windows application to an Android service since the first day. This opens up a plethora of possibilities, such as notifying an Android device that a server operation has completed or sending commands to the Android app.
The following demo is just a proof of concept, and it will require some improvements to be used in production (for example a bi-directional communication, or including a list of messages on the server to allow more than one message) but it shows the main concepts to be applied in your development.
In this post, we’ll show a demo of a Windows VCL application acting as a server and an Android app client connecting to that server and receiving messages from it. Of course, communication is broken if the Windows application is closed or if there are network issues, as there is no implementation of fault tolerance measures in this demo.
On the technical side, a TCP socket is used to communicate between the Windows VCL server and the Android service. This way, we can reduce battery consumption while maintaining constant communication. When the user clicks the “Init Server” button in the VCL application, the socket is created and begins to listen for clients. By clicking the “Connect to Server” button on the Android side, the service is launched, which creates a socket and begins receiving from it. If a service is already running, the Android app connects to it. When the Android service receives a message on the socket, it sends a notification to the main application; when that notification is opened, the main Android application binds to the service and reads the message.
In the video, you can see the interaction between the VCL application and the Android app, even when this one is running in the background.
And here is the source code: VCLAndroidService
To make it work, please ensure to change the IP of the TNotificationServiceDM.WaitForNotification procedure of the NotificationServiceUnit.pas file in the libNotification.so project, and set the IP of the Windows machine that will host the VCL server application. Compile the service and add it to your Android application as explained in the docwiki documentation: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Android_Services#Adding_an_Android_Service_to_an_Application.
Ensure also that you have configured your Windows firewall to allow connections to the given port (4851), or just disable the firewall for testing.
Interested in building a Windows app that supports Android services?
Try out the Windows IDE and make the most of it. Request a Free Trial here.
Try out the Windows IDE and make the most of it. Request a Free Trial here.