Android Tutorial B4A-Bridge source code

B4A-Bridge is made of two components. One component is running on the device and the other is embedded in the IDE.
The device side is the server side. It waits for connections and when a connection is established it handles the "action" messages.

The code itself is made of a simple activity:


And a service that is doing most of the job.
All the network communication is handled by an AsyncStreams object. AsyncStreams are very useful for networking and other external communication.

In this case the AsyncStreams object is initialized in prefix mode. In this mode we always receive complete messages which makes things simpler.
See this tutorial for more information: AsyncStreams Tutorial

This code is a good example for networking and working with service.
You are welcomed to examine the code and ask any question about it.

Latest version source code is attached.


    29.4 KB · Views: 848
Last edited:


Active Member
Licensed User
Erel, could you consider adding the following feature? B4A-Bridge indicates when a file transfer is in progress with 'progress'. Reason: in lower quality/bandwidth Wifi environments, it would be nice to have an indication the transfer started, and that it is making progress (ie, didn't stall/die).
Basically a packet counter is added, and with a value of -1 indicates no transfer occurring, 0 indicates started, and >0 is the packet count. Status line is updated with the value when > -1, ie, started/in-progress. When complete status returns to "Connected".
In Service1:

Sub Process_Globals
    Dim APK_XFER_PacketCount As Int = -1

Sub HandleFileStart
    Log("Installing file.")
    APK_XFER_PacketCount = 0
    CallSubDelayed(Main, "UpdateStatus")

Sub HandleFilePacket(Buffer() As Byte)
        Out.WriteBytes(Buffer, 1, Buffer.Length - 1)
        APK_XFER_PacketCount = APK_XFER_PacketCount + 1
        CallSubDelayed(Main, "UpdateStatus")

Sub HandleFileClose
    APK_XFER_PacketCount = -1
    CallSubDelayed(Main, "UpdateStatus")

In B4a-Bridge.b4a:

Sub UpdateStatus
    If Service1.ConnectedStatus Then
        If Service1.APK_XFER_PacketCount > -1 Then
          lblStatus.Text = "File Transfer: " & Service1.APK_XFER_PacketCount
          lblStatus.Text = "Status: Connected"
        End If

I do some work near the limits of my current Wifi access point, and sometimes when I kick off an Alt-3, it never makes it to the "Install" step on the android, and the GUI sits in "Debug". Presently only an "Installing" makes it into the Log, and if the Wifi is good enough it shows in the GUI log.
You might also consider a packet number in each packet after the command byte, and a total count in the start/filename packet; then percentage could be displayed and you can detect a missed packet. However as you know that would require a B4A release, so in the future perhaps.

In the meantime I modified a copy of Bridge-B4A here.



Active Member
Licensed User
I have made a modded version of the B4A Bridge.
Maybe @Erel like it and will put it on Google Play ;)

- Tuned the design
- Added restart button (Stops and restarts the previous connection)
- Changed version from V2.10 to V2.11

I have attached the source code of the modded version (That in the "zip" archive)
And the APK for you guys to test it out :)


  • B4ABridgeModded.apk
    158 KB · Views: 418
  • B4A Bridge
    359.8 KB · Views: 736


Active Member
Licensed User
VersionCode and VersionName under Module Attributes also need to be changed.
These are the fields that Google Play and Android Installer will look at.


Well-Known Member
Licensed User
Hi Erel,
The source code of B4A-Bridge could be supported for android wear,couldn't it?


Active Member
Licensed User
Almost all my development is for Android devices with an RJ45 port.
It was annoying to switch between WiFi and cable all the time.
So this is a very small update to B4A Bridge that detects if you are connected using WiFi or cable.
Attached is source and apk.


    372 KB · Views: 472
  • B4ABridge.apk
    163.4 KB · Views: 382
Last edited:


Well-Known Member
Licensed User
I update version 2.14

Insert code of mr23 and changed the Manifest with code for insert icon into ANDROID TV.
Please insert this Manifest and the simply Activity (TVActivity) in your future update.


  • B4A
    395.8 KB · Views: 389
  • B4A-Bridge.apk
    167.3 KB · Views: 316


Licensed User
Try latest version (2.14) compile an error:

B4A version: 6.31
Parsing code. (0.01s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Organizing libraries. Error
Maven artifact not found:



Licensed User
Install two repositories, compile error:

B4A version: 6.31
Parsing code. (0.01s)
Compiling code. (0.05s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. Error
res\values-v20\theme.xml:3: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light'.



SDK install:


  • Android-SDK-Mgr-1.jpg
    220.6 KB · Views: 320
  • Android-SDK-Mgr-2.jpg
    241.6 KB · Views: 312