Windows and beyond. Connecting a Windows application with an Android service
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.
Thursday, 5 November 2015
Desperate for C++ builder implementation of Android Service Projects. Please notify me off Beta.
Project requires Server to maintain list of listening devices so that Job information can be dispatched to the devices.
This will be a good starting point. I am downloading now. Thank you.
Please login first in order for you to submit comments
- Page :
Hi Luis, thank you very much for the article and the supplied demo.
Your code was very helpful as a starting point to develop our app.
We need the TCP server on the Android service and the TCP client on the Windows
side. The Android service is running continuously, receiving messages from the client,
that connects, sends the message and disconnects immediately.
We managed to have it working using Indy components (TCPClient and TCPServer).
It is running well when the app is in foreground, background, and when the device is in sleep mode,
as in your demo.
The only problem arises when the app is closed with the task manager; in this case I can see the service running from the Android app manager, but the TCP server is no longer active. It seems that the socket is closed by someone.
Could you give me a suggestion to solve the issue ?
Thanks in advance