package nl.rhinofly.twelvetalk; import java.io.InputStream; import java.io.OutputStream; import android.app.Application; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import de.tavendo.autobahn.WebSocketConnection; import de.tavendo.autobahn.WebSocketConnectionHandler; import de.tavendo.autobahn.WebSocketException; import de.tavendo.autobahn.WebSocketConnectionHandler; import de.tavendo.autobahn.WebSocketOptions; public class TTWebsocketService { private final WebSocketConnection wsConnection = new WebSocketConnection(); private TTApplication TTApp; private Handler toHandler; private WSConnectedThread wsThread = null; private boolean D = false; /** * Constructor. Prepares a new Websocket session. * @param context The UI Activity Context * @param handler A Handler to send messages back to the UI Activity */ public TTWebsocketService(Handler handler, Application app) { this.TTApp = (TTApplication) app; this.D = TTApp.getPrefs().getBoolean("debug", false); toHandler = handler; } public void startWebsocketCommunication(){ wsThread = new WSConnectedThread(); wsThread.setupWebSocketCommunication(); } private class WSConnectedThread extends Thread { private void setupWebSocketCommunication() { final String wsuri = TTApp.getPrefs().getString("socketServerUrl", "ws://192.168.1.58:8080"); final WebSocketOptions wsOptions = new WebSocketOptions(); wsOptions.setReconnectInterval(10000); try { wsConnection.connect(wsuri, new WebSocketConnectionHandler() { // this only handles open/close and incoming messages! @Override public void onOpen() { sendHandlerMessage(TTMainActivity.WS_MESSAGE_CONNECTED,"connected to " + wsuri); } @Override public void onTextMessage(String payload) { // send to bluetooth? } @Override public void onBinaryMessage (byte[] payload ){ toHandler.obtainMessage(TTMainActivity.WS_MESSAGE_READ, payload.length, -1, payload).sendToTarget(); //sendMessage(payload); } @Override public void onClose(int code, String reason) { sendHandlerMessage(TTMainActivity.WS_MESSAGE_DISCONNECTED,"connection lost to " + wsuri); } },wsOptions); } catch (WebSocketException e) { TTApp.log(e.toString()); } } } public void write(String message){ if (this.wsConnection.isConnected()){ this.wsConnection.sendTextMessage(message); } } public void writeBinary(byte[] byteArray){ if (this.wsConnection.isConnected()){ this.wsConnection.sendBinaryMessage(byteArray); } } public boolean isConnected(){ return this.wsConnection.isConnected(); } public void disconnect(){ if (this.isConnected()){ this.wsConnection.disconnect(); sendHandlerMessage(TTMainActivity.WS_MESSAGE_DISCONNECTED,"disconnected"); } } public void connect(){ if (!this.isConnected()){ wsThread.setupWebSocketCommunication(); } } private void sendHandlerMessage(int what,String mes){ Message msg = toHandler.obtainMessage(what); Bundle bundle = new Bundle(); bundle.putString(TTMainActivity.MESSAGE, mes); msg.setData(bundle); toHandler.sendMessage(msg); return; } private void sendLogMessage(String mes){ this.sendHandlerMessage(TTMainActivity.WS_MESSAGE_LOG, mes); return; } }