B4AServer - simplifies development of enterprise in-house Android applications

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4AServer is now deprecated. There are simpler and more powerful solutions available with B4J.

Overview

B4AServer is now available. The purpose of B4AServer is to simplify development of in-house applications.
B4AServer makes it simple to reliably communicate one or more Android devices with a desktop computer acting as the server over the local network. The desktop computer is a regular computer running the desktop component of B4AServer.

Currently the server supports three "actions":
- Download files (desktop to device).
- Upload files (device to desktop).
- Run predefined executables or shell commands and return the result to the device.

Using these three actions you can easily integrate your Android application with an existing system. For example you can write a desktop program (using your preferred programming language) that exports data from your database based on the arguments it receives and stores the data in a file. The device will send a shell request that will run the program and when it completes successfully it will download the file.

The desktop server can also serve as a complementary service for tasks that are much easier to handle on a desktop. For example the device can upload a file and then send a shell request that will print this file.

While this post is pretty long, the actual configuration is rather simple. I recommend you to give it a try. B4AServer can solve many tasks easily and reliably.

Components and usage
B4AServer is made of three components.
Desktop server, device client service and code modules and "board" web service (php script).

To overcome firewall issues with incoming connections, the desktop server connects to the device. When a device wants to connect to the desktop server it calls the board web service and sends the board web service its own IP address. The desktop server checks the web service for waiting devices every couple of seconds. When a waiting device is discovered the server tries to connect to this device. Once there is a connection the communication is done directly between the device and the desktop server.

All the client communication issues are implemented inside the service and code modules. The public interface is very simple.

Device code and configuration:
The device program needs two parameters. The URL of the board web service and the name of the desktop server. The name of the desktop server can be any string that you want. It is used by the board service to differentiate between different desktop servers using the same board service.

In this code we set the URL, server name and the Activity that will handle the TaskComplete event:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        B4AServer.Initialize("http://xx.xx.xx.xx/b4a_server.php", ServerName, "Main")
    End If
Note that the live "board" link included in the examples code should only be used for development. There are no guarantees whatsoever regarding its availability.

Working with B4AServer is done by sending tasks. There are three relevant methods in B4AServer module: SendFile, ReceiveFile and Shell. Each of these subs return an Int value which serves as the task id.
Later when the task is completed (whether successful or not) the TaskComplete sub will be called. The task id will be passed to this method as part of the TaskResult parameter.

For example to download an image named logo.jpg from the desktop server and set it as the Activity background we can use this code:
B4X:
Sub btnReceiveFile_Click
    'imgTask is a global variable
    imgTask = B4AServer.ReceiveFile("logo.jpg") 'Sends a download file request
End Sub

Sub TaskComplete(Result As TaskResult)
    If Result.Id = imgTask AND Result.Success = True Then
      Activity.SetBackgroundImage(LoadBitmap(B4AServer.DownloadFilesFolder, Result.Id))
    End If
End Sub
The other two subs are:
B4AServer.SendFile (Dir As String, FileName As String) As Int
Which uploads a file to the server.
B4AServer.Shell (ShellCommand As String, Args As List) As Int
Which makes the server run the predefined command with the given arguments. Pass Null if no arguments are required.

Configuring the desktop server
The desktop server configuration is stored in a file named config.properties.
You must edit this file and set the ServerName property to match the chosen server name.
B4X:
#This is the unique server name you choose.
ServerName=test1
#Folder for files that can be downloaded to a device
InputFolder=files
#folder for uploaded files
OutputFolder=files
#number of devices that can connect at the same time
WorkerThreads=5
#link to the board  server
BoardUrl=http://xx.xx.xx.xx/b4a_server.php
#Interval measured in seconds. The desktop server polls the board server every x seconds.
CheckInterval=5
#list of shell commands
Shell.Print="c:\\program files\\PrintHTML\\printhtml.exe" printername="novapdf" header="" footer=""
Shell.Dir=cmd.exe /c dir files
You can set any number of shell commands. The format is Shell.<shellcommand>.
Not that you should escape forward slashes with another forward slash as shown above in Shell.Print.

The desktop server is a java program so it can run on any operating system with Java installed. A batch file is included named run.bat that will start the server. After changing a configuration parameter you will need to restart the server by closing its window and running it again.

Configuring the board web service (can be skipped during development)
During development you can use the web service link that is included in the examples.

The board web service is a php script that communicates with a MySQL database.
You will need to create a database and a user account that can access the database.
Then edit b4a_server.php and set the four fields at the top of the file:
B4X:
$databasehost = "localhost";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
The script will create the tables on the first run.
A shared hosting that supports MySQL and PHP should suffice for running the board web service.

Please feel free to suggest any suggestion or possible use cases.

The files are available here. See the second post there for information about using the emulator with B4AServer.

Examples:
Printing with B4AServer: http://www.b4x.com/forum/basic4andr...inting-android-using-b4aserver.html#post53634
Managing deployment and updates of multiple applications on multiple devices: http://www.b4x.com/forum/basic4andr...ons-multiple-devices-b4aserver.html#post53738
Accessing any database or system with B4AServer: http://www.b4x.com/forum/basic4andr...-any-database-system-b4aserver.html#post53786
 
Last edited:

adex1

Member
Licensed User
Longtime User
Alright, meaning Home PC can be use as service for Android devices. If I'm right?

B4AServer is not a development tool. It can be considered a small framework that helps with integrating Android devices in existing systems and with desktop-device communication.
 

gobblegob

Member
Licensed User
Longtime User
Can this server host multiple php pages? or can it be run side by side with a web server like xampp .

cheers Waz
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Alright, meaning Home PC can be use as service for Android devices. If I'm right?
Yes. In order for the devices to connect they should be part of the same local network.
Can this server host multiple php pages? or can it be run side by side with a web server like xampp .
Yes. The only requirement is that MySQL will also be installed.
 

gobblegob

Member
Licensed User
Longtime User
Excellent, Thanks Erel. I will have a play with it over the next few days and maybe upload a sample my php is better than B4A :)

cheers Waz
 

Asmoro

Active Member
Licensed User
Longtime User
Erel:

Few things to know before I want to start with b4a server utility.

- I want to use my laptop as a server (next time with an external usb storage disk, connected
on a wifi router (N)), do I still need a php script or database like MySQL to work.
As I don't have an active website running.

Or is it enough just to make folders uploading, downloading files.

- If so, how to display a received data, text and images from the (server) laptop with a scrollview.
Do I need to make a layout of a scrollview first and referring the data with the views layout?

grtz
Asmoro

well, never mind, it seems you need a php script to work.
 
Last edited:

optimist

Member
Licensed User
Longtime User
The purpose of the "board" server is to overcome firewall issues related to Windows not accepting incoming connections.

Tested it. But with T-Mobile (one of germanys biggest providers) it does not work, because we have a firewall issue in mobile connection.
T-Mobile provide acess with an "secure" APN with includes something like transparent surfproxy and nat-router.

I could imagine only one solution. Users would need to communicate via board in both directions. (Phone sends job to board, PC get this job and execute it and send result back to board. Phone get result from board)

Note, this is only for your information, not a wish. I am fine with an VPN and so dont need a board at all.
 

Asmoro

Active Member
Licensed User
Longtime User
Erel,

Just tweaking the ip adresses with your standard example receiving and sending a .png image, because it didn't work.

Yes I know, something to do with my port forwarding/router/firewall etc.

I'm using one of my dhcp ip adresses (my phone) instead of a 127.0.0.1 to get
working the example.

So, my question to you is; would it be allright using the B4A server with my dhcp ipadresses or do we getting some reliable, privacy or handling issues.

And do I have to change this ip in the B4A server properties file.

Second question will come later, this is more important to understand 'fully'.

grtz.
Asmoro
 

optimist

Member
Licensed User
Longtime User
The desktop server and the device(s) should share the same local network for the B4A Server to work.

Sorry, missed this point. Because for me the smartphone is (only) a mobile device. In local network (e.g. at home or in office) i use easily operated local devices.
Sorry again for inapplicable post.
 
Top