Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.xbmcgear.connection; // ww w.j a va 2 s . c o m import com.samsung.android.sdk.accessory.SASocket; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; class ProviderConnection extends SASocket { private static final int SERVICE_CHANNEL_ID = 109; private int mConnectionId; public ProviderConnection() { super(ProviderConnection.class.getName()); } public int getID() { return mConnectionId; } public void setID(int mConnectionId) { this.mConnectionId = mConnectionId; } @Override public void onError(int channelId, String errorString, int error) { } @Override public void onReceive(int channelId, byte[] data) { String request = new String(data); if (isNumeric(request)) { int reqInt = Integer.parseInt(request); JSONRPCRequest req = JSONRequestFactory.processQuery(reqInt); if (!Connection.getInstance().isConnectedToXBMC() && reqInt == JSONRequestFactory.REQ_SHUTDOWN) { Connection.getInstance().sendWol(); } else { Connection.getInstance().sendToXBMC(req); } } else { try { JSONObject js = new JSONObject(request); String type = js.optString("type"); JSONRPCRequest req = null; if (type.equals("playlist")) { req = JSONRequestFactory.goTo(js.optInt("go_to")); } else if (type.equals("volume")) { req = JSONRequestFactory.setVolume(js.optInt("value")); } Connection.getInstance().sendToXBMC(req); } catch (JSONException e) { e.printStackTrace(); } } } private boolean isNumeric(String data) { try { Integer.parseInt(data); } catch (NumberFormatException e) { return false; } return true; } @Override protected void onServiceConnectionLost(int errorCode) { ProviderService.getInstance().connectionLost(); } public void sendMessage(final String msg) { final ProviderConnection uHandler = ProviderConnection.this; new Thread(new Runnable() { public void run() { try { uHandler.send(SERVICE_CHANNEL_ID, msg.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }