Author: Luis Navarro
I’ve always loved developing Windows applications using Delphi and RAD Studio. But it’s getting ever more exciting with the latest releases as it’s possible to go beyond Windows and create a new ecosystem that includes the capabilities of the mobile apps, IoT devices, etc…
RAD Studio 10 Seattle brought the Android services feature, and since the very first day I’ve been thinking about connecting a Windows application with an Android service. This offers a lot of possibilities, like notifying to an Android device that an operation on the server has finished, 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 on a 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 the demo, we have a Windows VCL application that acts as a server and an Android app client that connects to that server and gets messages from it. Of course, if the Windows application is closed or there are network issues the communication is broken, as for this demo there is no implementation of fault tolerance mesures.
On the tech side, a TCP socket is being used to communicate the Windows VCL server with the Android service. This way we optimize the battery consumption while keeping a permanent communication. On the VCL application, when the user clicks the “Init Server” button, the socket is created and starts to listen for clients. On the Android side, by clicking on “Connect to Server” button, a service is launched and it creates a socket and starts receiving from it. If there was already a service running, the Android app connects to it. After receiving a message on the socket, the Android service sends a notification to the main application, when that notification is opened, the main Android application binds to the service and reads the message directly from it.
In the video you can see the interaction between the VCL application and the Android app, even when this one is running on background.
And here it is the source code: VCLAndroidService
To make it work, please ensure to change the IP on 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. Then compile the service and add it to your Android application as explained in the docwiki 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.
If you have any doubts, please don’t hesitate contacting me. I would love to hear about the ideas and the use you do for this feature.