示例#1
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // get handles to Views defined in our layout file
    mGraph = (GraphView) findViewById(R.id.graph);
    mValueTV = (TextView) findViewById(R.id.value);

    mGraph.setMaxValue(1024);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mGraph = (GraphView) findViewById(R.id.graph);
    mGraph.setMaxValue(1024);

    // Create TCP server (based on MicroBridge LightWeight Server)
    try {
      mServer = new Server(4568); // Same port number used in ADK firmware
      mServer.start();
    } catch (IOException e) {
      Log.e(TAG, "Unable to start TCP server", e);
      System.exit(-1);
    }

    mServer.addListener(
        new AbstractServerListener() {
          @Override
          public void onReceive(org.microbridge.server.Client client, byte[] data) {
            if (data.length < 2) return;
            mSensorValue = (data[0] & 0xff) | ((data[1] & 0xff) << 8);
            // Any update to UI can not be carried out in a non UI thread
            // like the one used for Server. Hence runOnUIThread is used.
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    new UpdateData().execute(mSensorValue);
                  }
                });
          }
        });
  }