Skip to content Skip to sidebar Skip to footer

Android App Tcp Worker

I am planning out an Android app that communicates with a PC program via a TCP Socket. The app will have several Activities(layout) each of which represent a different physical dev

Solution 1:

We are building an Android app that is communicating with a hardware via TCP socket listening to various events from the device as well as communicating with it via user's actions. Similar to what you are about to do, our app has also several Activities that are getting different data based on the context of the activity. For the app, we have done the following approach:

  • Create a Response and Request Handler that is responsible for opening the socket output and inputstream.
  • We ran these two on its own Thread. The Request Handler is taking the request from a blocking queue that contains the request
  • Each Request is identified with its type, request id, timestamp and a Handler.
    • The type identifies the context of the request (in your case, the device type)
    • A Handler is using Android Handler that is responsible for handling a request with specific id and type. In your case you will have DeviceAHandler, DeviceBHandler etc, that you will associate with specific id and type. With this approach, your Handler can handle a specific UI update for specific device
  • For the Response Handler, we have a blocking inputstream that waits for the response, once a response is received, we match the response id to the request id and get the Handler that is associated with the id
  • Upon changing context to a specific activity, we are sending request with different request type. In your case, whenever you change the Activity, you can use onStart to send a request type switch to the server. Since previous handlers that you register handle specific context (through request type), they will ignore the response if it doesn't match with the new request type.

Both Request and Response Handler are implemented as AsyncTask, below are the stub for the ResponseHandler so that you could understand better what I wrote :

publicclassResponseHandlerextendsAsyncTask {
    boolean isConnectionClosed = false;

    @OverrideprotectedIntegerdoInBackground(Void... params) {
        int errorCode = 0;

        try {
            // while not connection is not closewhile(!isConnectionClosed){
                // blocking call from the device/serverString responseData = getResponse();

                // once you get the data, you publish the progress// this would be executed in the UI ThreadpublishProgress(responseData);
            }
        } catch(Exception e) {
            // error handling code that assigns appropriate error code
        }

        return errorCode;

    }

    @OverrideprotectedvoidonPostExecute(Integer errorCode) {
        // handle error on UI Thread
    }

    @OverrideprotectedvoidonProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        String responseData = values[0];

        // the response contains the requestId that we need to extract
        int requestId = extractId(responseData);

        // next use the requestId to get the appropriate handlerHandler uiHandler = getUIHandler(requestId);

        // send the message with data, note that this is just the illustration// your data not necessary be jut StringMessage message = uiHandler.obtainMessage();
        message.obj = responseData;
        uiHandler.sendMessage(message);
    }

    /***
     * Stub code for illustration only
     * Get the handler from the Map of requestId map to a Handler that you register from the UI
     * @param requestId Request id that is mapped to a particular handler
     * @return
     */privateHandlergetUIHandler(int requestId) {
        returnnull;
    }

    /***
     * Stub code for illustration only, parse the response and get the request Id
     * @paramresponseId
     * @return
     */private int extractId(String responseId) {
        return0;
    }

    /***
     * Stub code for illustration only
     * Call the server to get the TCP data. This is a blocking socket call that wait
     * for the server response
     * @return
     */privateStringgetResponse() {
        returnnull;
    }
}

Post a Comment for "Android App Tcp Worker"