Android Tutorial [B4X] The Networker's Guide To The Galaxy

Discussion in 'Tutorials & Examples' started by Erel, Jul 14, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User



    The purpose of this tutorial is to cover the various communication options available for B4X developers.

    Will start with two important utilities:
    • AsyncStreams: Asynchronously reads and writes to any stream and avoids blocking the main thread.
      Do not assume that messages will arrive as a single packet. Messages can be split or merged.
      • Prefix mode: When connecting to another B4X program (excluding B4R) then use prefix mode. In this mode AsyncStreams will take care of collecting the data and the NewData event will only be raised with full messages.
        Prefix mode also supports sending huge files with AStream.WriteStream and NewStream event.
        Tip: Do not use modal dialogs (Msgbox in B4A) when working with AsyncStreams as the messages may arrive in the wrong order.
      • AsyncStreamsText: An alternative to prefix mode for text based protocols that end with a specific character(s).
    • B4XSerializator: Converts primitive types, arrays, collection and custom types to an array of bytes and vice versa. The serialized data is cross platform compatible (excluding B4R). This means that you can easily send complex messages between different platforms.
    And now for the different communication technologies:
    • TCP / Server + ServerSocket + AsyncStreams (Network, RandomAccessFile libraries): Direct connection between two devices that are connected to the same local network.
      • Streaming protocol.
      • Low level solution. Difficult to use if more than two devices are involved (see this example).
    • UDP / UDPPacket + UDPSocket (Network library): Connection-less protocol. Just open the port and send packets. Devices need to be connected to the same local network.
      • As there is no connection step, there is no guarantee that the messages have arrived to their destination.
      • Packet based protocol.
      • Supports broadcasting by sending messages to the broadcasting address.
      • B4J UDPSocket supports sharing of the port which means that multiple processes can listen to the same port.
      • Used for example in the External Serial Connector tool.
      • Good choice for inter-process communication.
    • Classic Bluetooth (Serial, jSerial, jBluetooth libraries): Direct connection between two devices.
      • Supported by B4A, B4J and B4R. Not supported on iOS.
      • Good solution for direct connections when there is no local network.
      • Streaming protocol. Should be used together with AsyncStreams.
      • Less reliable than TCP connections.
    • B4J Server (jServer) - Http server. Simple and powerful server.
      • Clients connect with OkHttpUtils2 or iHttpUtils2.
      • Very reliable and supports many concurrent connections.
      • Can run in the local network or over the internet.
      • Works with browsers as well.
      • Requests / response based protocol.
    • B4J Server WebSockets - jServer supports both HTTP requests and WebSockets.
      • Can be used as a backend solution with WebSocketClient libraries.
      • Client and server maintain an active and symmetric connection.
      • Supports web applications: ABMaterial.
      • Can run in the local network or over the internet.
      • The server can connect to local or remote databases (ConnectionPool object).
      • Good choice for backend solutions.
    • MQTT - A publish / subscribe messaging. Simple and powerful.
      • No server component. Only a "black box" broker.
      • The broker can run inside a B4A or B4J app or as an external program.
      • Many to many protocol.
      • Excellent choice for local network solutions.
    • Serial communication (UsbSerial or felUsbSerial libraries). Using a USB to serial converter allows B4A and B4J programs to communicate with serial devices.
      • This feature relies on USB host mode. Not all Android devices support it (properly).
      • UsbSerial works with AsyncStreams however it doesn't support prefix mode. You can use AsyncStreamsText or felAsyncStreamsText.
      • B4R supports both hardware and software serial communication.
    More esoteric options:
     
  2. wonder

    wonder Expert Licensed User

    Thank you so much for this thread, @Erel.

    Given all the presented possibilities, which solution/protocol would be the most recommended for real-time multiplayer gaming? I'm very inclined to MQTT.
     
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    I found this video interesting when you uploaded it onto YouTube a month ago, it's an excellent example of how B4X can be used to communicate over different devices.

    Cheers @Erel...
     
    wonder likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    MQTT is possible. However as it will require an online server with a database then a B4J server with WebSockets might be a better solution.
     
    wonder likes this.
  5. Rantor777

    Rantor777 Member Licensed User

    Thank you for the Post, Excellent BTW!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice