Ideas on using adb bridge from B4A DEVICE?

junglejet

Active Member
Licensed User
Longtime User
Hi all,

I am developing a hardware combination that is based on an Android as a USB slave device and some specific hardware as a USB host (http://www.ftdichip.com/Support/Documents/DataSheets/Modules/DS_Vinco.pdf), connected by a host/slave USB cable. I do not want OTG, because this is not reliably available before Android V4.

(To answer some answers upfront: due to restrictions on certification a cable based connection must be used, Wifi or Bluetooth is a no-go.)

The whole setup works together with a java application on the Android and some firmware in the host board. It seems as if a tunnel is opened over USB.
A serversocket server is opened on the Android device port 1337 and traffic is handled through this server.

Also USB debugging needs to be enabled. i.e. at the very low level the basic adb interface is used. It is claimed that the technique works on every device since Android V1.6, so it is indeed a very basic and attractive method.

As my java skills do not allow me to continue developing in that language I would like to continue exploring a way to use B4A for further developments. However, I would demand to understand how things work in general.

Has anybody got an idea and can hint me to more basic understanding of the mechanics of such an interface and the best way to replicate this by B4A? (I have the java sources of the application, but except for opening of the serversocket they do not tell me about the underlying low level stuff)

Thank you
Andy

Basic connection code from inside a device java application
B4X:
try
{
     //Create new server 
     ServerSocket socket = new ServerSocket(1337);
    
     //Wait until the andropodInterface is connected
     Socket andropodInterface = socket.accept();

     //Open streams
     PrintReader in = new BufferedReader(new InputStreamReader(andropodInterface.getInputStream()));
     PrintWriter out = new PrintWriter(andropodInterface.getOutputStream(), true);

    //Write what you want to write
    out.println("Hello Androdpod!");

     //Read what you want to read ;-)
    string message = in.readln();
}
catch (IOException e) 
{
    //Something went really wrong
}
 

junglejet

Active Member
Licensed User
Longtime User
Thanks Erel
I worked through the tutorial and the example and indeed just opening a tcp server does the job and connects to the ADB host. I ahd not thought it is that simple.

Here is the sample code for the archive:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim InputStream1 As InputStream
   Dim OutputStream1 As OutputStream
   Dim ServerSocket1 As ServerSocket
   Dim Socket1 As Socket


End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Initialize the server if not initialzed yet
   If ServerSocket1.IsInitialized = False Then
      ServerSocket1.Initialize(1337, "ServerSocket1")
   ToastMessageShow("My IP: " & ServerSocket1.GetMyIP & ":1337", True)
   End If 
End Sub

Sub Activity_Resume
   ServerSocket1.Listen
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then 
      Socket1.Close
      ServerSocket1.Close 'stop listening
   End If
End Sub

Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
   If Successful Then
      Socket1 = NewSocket
      InputStream1 = Socket1.InputStream
      OutputStream1 = Socket1.OutputStream
      ToastMessageShow("Connected", True)      
   Else
      Msgbox(LastException.Message, "Error connecting")
   End If
   ServerSocket1.Listen 'Continue listening to new incoming connections
End Sub
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Thanks junglejet.

I am also very interested in this topic. I read this article in a well-known electronics magazine.
Some time ago I bought the android USB development kit from Microchip. But in my phone does not work (it is a SGII). As with many others will not. Besides, it would also be very difficult to pass the Java libraries to B4A.
I will study your code, and if it works is a very good option.

Greetings.
 
Last edited:
Upvote 0

junglejet

Active Member
Licensed User
Longtime User
Hi Joan,

I have got the Andropod board working with B4A and with SG S2, now.
I have not tried the microbridge because I don't like the PIC hardware to implement into my own designs. The VNC2 is much smaller and easier.

The secret with the SGS2 is that its screen needs to go into suspend mode (dark) and only then USB debugging will be enabled and the low level USB connection to the board can be made. Then you can connect to the board with the B4A code above and wake up the S2 again and USB connection will remain.

I can also send data to the Andropod board, I am still working on polishing this and receiving data. But this is more because of my lack of skill in B4A that it takes so long :signOops:

For the archive, it is working like this:
Underlying is a USB slave on the Android device and a USB host on the board. These connect with the USB Android class (255), subclass 66, protocol 1.
When USB is connected a TCP server is opened on the device and the board connects to this server through a TCP client. Once TCP is connected a ADB host is opened on each the device and the board, and the hosts are opened for send and receive (CNXN). The hosts have a (very simple) protocol that can send data in both directions (WRTE, OKAY). On the Android device data traffic will end in a TCP client (socket) that is automatically opened, when the server is connected by another TCP client. The device server/client implementation then routes any data between the outside and the inside device ADB hosts.
The classes you need to implement this on the Android device are in the Network library as Erel stated above (Serversocket and Socket).

Maybe this is helpful.
Andy
 
Last edited:
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Thank you very much Andy.
This has been helpful, but still I have much to learn. I have little experience in TCP (also in B4A).
Yes, Vinculum can be a good option as it is much easier to program (than Microchip), but ultimately you must have to go to a small microcontroller (which is what controls all your electronics).
I will buy a development board like this (is cheap) and do the tests.
I will try, for now, communicate with the PC. Communicating with HyperTerminal I managed to make some progress (but I very slowly).
We'll see how it goes, if I am also able to communicate with the board.
Good luck with your project, surely you will end soon.
Greetings.
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Hi Andy.

I extended the sample you post, and I managed to send/receive on a PC (Hyperterminal).
When I thought everything was going well, I realized that I communicated via Wifi.
Without enabling the Wifi and only with the USB plugged in, the address has changed now (My IP: 10.10.18.127:1337). The Hyperterminal does not recognize the connection and disconnected by time-out. (note: I put the same IP and Port in the Hyperterminal).

I misunderstood what you said about the secret to SGSII, although I've tried many different ways, with no results.
I even removed the firewall and antivirus for Windows.

It is possible to communicate with the PC (HyperTerminal) via USB? I looked at the Erel's Network library, and yes, he says you could do. But this example does not work for me.

I do not know what I'm doing wrong and what else I can do

Greetings.
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Andy, I have finally solved the problem. I can communicate with the application, via the PC USB.
There were 2 problems:
The first, which you said "the secret of SGSII" (I guess). I have solved it by doing a "Reestart ADB Server" in tools in B4A.
The second, what said Erel in the tutorial Library Network. Some time ago I read, I tried the example but did not work. Until I read more about the ADB and I understood what he meant by the command "adb forward tcp: 5007 tcp: 2222". Here was the problem, you have to redirect the port.

Now I have a starting point that works to advance the project.

Greetings.
 
Upvote 0

junglejet

Active Member
Licensed User
Longtime User
Great,
obviously was a long night!

Exactly, there is an ADB protocol that needs to be used.
The Vinculo/Andropod firmware implements an ADB host inside the VNC2, that is compatible to the ADB host on a PC.

I can now transmit in both directions:

SG S2 with test application <-> Vinculo Board <-> UM232R USB/serial module <-> PC terminal

You just need the Vinculo Board and the VNC2 Vinco debugger, e.g. from RSonline. Also the USB/serial module is very helpful.

Instead of the UM232R my intention is now to connect my own microcontroller hardware via serial.

Cheers
Andy
 
Upvote 0
Top