Android Open Source - Android_CommandlineLikeDisplay Example Service






From Project

Back to project page Android_CommandlineLikeDisplay.

License

The source code is released under:

Copyright (c) 2011, Jerome Schneider All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met...

If you think the Android project Android_CommandlineLikeDisplay listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cld.CommandlineLikeDisplay;
/*www  .j  ava 2  s. com*/
import android.util.Log;
import blue.mesh.BlueMeshService;

public class ExampleService {
  
  private static final String TAG = "ExampleService";
  private CLDMessage myCLDMessage;
  private BlueMeshService bms;
  private boolean stop = false;
  
  //example service constructor
  public ExampleService( CLDMessage a_CLDMessage ){
    ///////////////////////////////////////////////
    //TODO
    //
    //Create a CLDMessage with the parameter as CLDMessage
    //which will be your object for io
    ///////////////////////////////////////////////  
    myCLDMessage = a_CLDMessage;
  }
  
  //////////////////////////////////////////////////////////
  //Example Functions
  //////////////////////////////////////////////////////////
  //Example that using CLDMessage is thread safe :)
  /*
  public void start(){
    myCLDMessage.print_normal("Starting testThread!");
    testThread a_testThread = new testThread();
    a_testThread.start();
    myCLDMessage.print_normal("It's thread safe!");
  }
  
  class testThread extends Thread{
    public void run(){
      myCLDMessage.print_normal("What is your name?");
      String in = myCLDMessage.getLine();
      myCLDMessage.print_normal("Your name is " + in); //what?!
        // chica chica chica (slim shady!)
    }
  }
  */
  public void stop(){
    this.stop = true;
    myCLDMessage.notifyGetLine();
    Log.d(TAG, "Stopping service");
  }
  
  public void start(){
    myCLDMessage.print_normal("one");
    try{
      bms = new BlueMeshService();
    }
    catch(NullPointerException e){
      Log.e(TAG, "BlueMeshService Constructor failed");
      myCLDMessage.print_normal("Bluetooth not enabled");
      return;
    }
    myCLDMessage.print_normal("two");
    bms.launch();
    myCLDMessage.print_normal("three");
    readThread reader = new readThread();
    reader.start();
    while( true ){
      
      if (this.stop)
      {
        Log.d(TAG, "2 Stopping service");
        break;
      }
        
      String input = myCLDMessage.getLine();
      bms.write(input.getBytes());
    }
    Log.d(TAG, "3 Stopping service");
    Log.d(TAG, "quit");
    bms.disconnect();
    reader.interrupt();
    return;
  }
  
  public class readThread extends Thread{
    public readThread(){
      
    }
    public void run(){
      while (true){
        if(stop){
          Log.d(TAG, "readThread interrupted");
          return;
        }
        byte bytes[] = bms.pull();
        if( bytes == null){
          myCLDMessage.print_normal("<NOTHING>");
          try {
            readThread.sleep(1000);
          } catch (InterruptedException e) {
            myCLDMessage.print_debug("Could not wait");
            e.printStackTrace();
            return;
          }
        }
        else{
          String messageString = new String(bytes);
          myCLDMessage.print_normal(messageString);
        }
      }
    }
    
  }
  
  //////////////////////////////////////////////////////////

}




Java Source Code List

cld.CommandlineLikeDisplay.CLDMessage.java
cld.CommandlineLikeDisplay.CommandlineLikeDisplayActivity.java
cld.CommandlineLikeDisplay.Constants.java
cld.CommandlineLikeDisplay.ExampleService.java