OpenMarine
Apple Client - Printable Version

+- OpenMarine (https://forum.openmarine.net)
+-- Forum: OpenPlotter (https://forum.openmarine.net/forumdisplay.php?fid=1)
+--- Forum: How do I...? (https://forum.openmarine.net/forumdisplay.php?fid=3)
+--- Thread: Apple Client (/showthread.php?tid=1694)



Apple Client - mikeh - 2019-03-05

would anyone be able to share their knowledge of connecting an app to the rpi as a client? I would like to build an app that displays Engine data but am unable to connect to the rpi server. I am using the signal k script for apple since javascript into the Android base code is not clear to me. The code provided by signal K for apple client was used and cocoa pods were installed correctly shown on the following link.

https://github.com/SignalK/signalk-apple-client

host used is: "10-1-1-40.local"
port used is: 3000

why would the error code: "Request timed out" keep appearing? Running 10.10.10.1:3000 into my browser opens up signalk. Also, is there better instruction for running a client to retrieve data in an Android app then is provided by SignalK website?

cheers


Apple Client - tkurki - 2019-03-05

The error message sounds like you are trying to reach some other server than what you think.

The server’s api is just http and WebSocket, can’t you just google these and the programming language you are using? Java, Kotlin, Swift, ObjC? To get data by http or stream via ws should be very easy in any language. The advantage of a dedicated client library comes with more complicated stuff, for example access requests and authorization in general.


Sent from my iPhone using Tapatalk


RE: Apple Client - mikeh - 2019-03-07

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();
}