This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Apple Client
#3
For future reference, this code has got me a response from the raspberry pi. It still needs refinement:


build.gradle:
implementation "org.java-websocket:Java-WebSocket:1.3.0"

Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />



Main Activity:

private WebSocketClient mWebSocketClient;

static String TAG = "***WEBSOCKET TEST***";
private static final String SERVER = "ws://10.10.10.1:3000/signalk/v1/stream";
private static final int TIMEOUT = 5000;


private void connectWebsocket() {
   URI uri;
   try {
       uri = new URI(SERVER);
       Log.d(TAG, "URI COMPLETE");
   } catch (URISyntaxException e) {
       Log.d(TAG, "URI SYNTAX EXCEPTION: " + e);
       return;
   }

   mWebSocketClient = new WebSocketClient(uri) {
       @Override
       public void onOpen(ServerHandshake serverHandshake) {
           Log.d(TAG, "SERVER HANDSHAKE");
           mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
       }

       @Override
       public void onWebsocketHandshakeSentAsClient(WebSocket conn, ClientHandshake request) throws InvalidDataException {
           super.onWebsocketHandshakeSentAsClient(conn, request);
           Log.d(TAG, "HANDSHAKE SENT AS CLIENT");
       }

       @Override
       public void onWebsocketHandshakeReceivedAsClient(WebSocket conn, ClientHandshake request, ServerHandshake response) throws InvalidDataException {
           super.onWebsocketHandshakeReceivedAsClient(conn, request, response);
           Log.d(TAG, "HANDSHAKE RECEIVED AS CLIENT");
       }


       @Override
       public void onMessage(String s) {
           final String message = s;
           Log.d(TAG, "ON MESSAGE: " + s);
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   //TextView textView = (TextView)findViewById(R.id.messages);
                   //textView.setText(textView.getText() + "\n" + message);
               }
           });
       }

       @Override
       public void onMessage(ByteBuffer bytes) {
           super.onMessage(bytes);
           Log.d(TAG, "ON MESSAGE");
       }

       @Override
       public void onClose(int i, String s, boolean b) {
           Log.d(TAG, "Closed " + s);
       }

       @Override
       public void onError(Exception e) {
           Log.d(TAG, "ERROM MESSAGE: " + e.getMessage());
       }
   };
   mWebSocketClient.connect();
}
Reply


Messages In This Thread
Apple Client - by mikeh - 2019-03-05, 09:03 AM
Apple Client - by tkurki - 2019-03-05, 06:17 PM
RE: Apple Client - by mikeh - 2019-03-07, 07:05 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)