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:

arSsfzqjKtq2_v9Moofj4SgiWWet6WOFH-P1Z2NuxbMo4yB6yqsGaoioptbDdbMCZw4=h900



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.
 

Attachments

  • B4A-Bridge-source.zip
    29.5 KB · Views: 708
Last edited:

mr23

Active Member
Licensed User
Longtime 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".
B4X:
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)
    Try
        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
        Else
          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.

-Chris
 

Fusseldieb

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

Changes:
- 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 :)
 

Attachments

  • B4ABridgeModded.apk
    158 KB · Views: 651
  • B4A Bridge Modded.zip
    359.8 KB · Views: 960

bluejay

Active Member
Licensed User
Longtime 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.
 

Theera

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

janderkan

Well-Known Member
Licensed User
Longtime 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.
Jan
 

Attachments

  • B4A-Bridge2.zip
    372 KB · Views: 689
  • B4ABridge.apk
    163.4 KB · Views: 587
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime 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.
Thanks
 

Attachments

  • B4A Bridge2.14.zip
    395.8 KB · Views: 595
  • B4A-Bridge.apk
    167.3 KB · Views: 524

jsprings

Member
Licensed User
Longtime 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: com.android.support/support-v4

Help?
 

jsprings

Member
Licensed User
Longtime 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'.

Next?

Thanks,

SDK install:
 

Attachments

  • Android-SDK-Mgr-1.jpg
    Android-SDK-Mgr-1.jpg
    220.6 KB · Views: 500
  • Android-SDK-Mgr-2.jpg
    Android-SDK-Mgr-2.jpg
    241.6 KB · Views: 496
Top