Android Open Source - watcher Comm Link






From Project

Back to project page watcher.

License

The source code is released under:

CC0 1.0 Universal Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent...

If you think the Android project watcher 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 com.appspot.staffordbears.watcher;
//w  ww  . j a  v a 2s  .c o  m
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.IBinder;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.util.UUID;
import java.util.Properties;

public class CommLink extends Service {

    public Hermit mApp;
    protected ZilogPir mZpSlave;
    protected int m_cfg_sensitivity;
    protected String m_cfg_address;

    private static final String TAG = "watcher.CommLink";

    protected class ZilogPir {
  SerialProtocol.LinkUp pLU;

  private Handler mCommandHandler;
  private Handler mResponseHandler;
  private Handler mErrorHandler;

  private Thread workerCommand = new Thread() {
    @Override
    public void run() {
        try {
      Looper.prepare();
      mCommandHandler = new Handler();
      Looper.loop();
        } catch (Throwable t) {
      Log.e(TAG, "command thread halted due to an error", t);
        }
        Log.i(TAG, "command thread completion");
        mCommandHandler = null;
    }
      };

  private Thread workerResponse = new Thread() {
    @Override
    public void run() {
        try {
      Looper.prepare();
      mResponseHandler = new Handler();
      Looper.loop();
        } catch (Throwable t) {
      Log.e(TAG, "response thread halted due to an error", t);
        }
        Log.i(TAG, "response thread completion");
        mResponseHandler = null;
    }
      };

  private Thread workerError = new Thread() {
    @Override
    public void run() {
        try {
      Looper.prepare();
      mErrorHandler = new Handler();
      Looper.loop();
        } catch (Throwable t) {
      Log.e(TAG, "error thread halted due to an error", t);
        }
        Log.i(TAG, "error thread completion");
        mErrorHandler = null;
    }
      };

  ZilogPir() {
      pLU = null;
      workerCommand.start();
      workerResponse.start();
      workerError.start();
  }

  public boolean connected() {
      Log.i(TAG, "connection check");
      if (pLU != null && 
    pLU.mSocket != null) {
    try {
        InputStream is = pLU.mSocket.getInputStream();
        int foo = is.available();
        Log.i(TAG, "available = " + foo);
        return true;
    } catch (IOException e) {
        return false;
    }
      } else {
    return false;
      }
  }

  public void start() {
      if (connected()) {
        return;
    }

      int[] cmd_1234 = {49,50,51,52};
      int[] cmd_128 = {128};
      int[] cmd_sense = {m_cfg_sensitivity};
      int[] cmd_30 = {30};
      int[] cmd_M = {77};
      int[] cmd_S = {83};
      int[] cmd_X = {88}; 
      int[] cmd_Y = {89};
      int[] exp_none = {};

      // bring up link then wait 2 secs
      pLU = new SerialProtocol.LinkUp(m_cfg_address);
      SerialProtocol.Sleep p_wait_link = new SerialProtocol.Sleep(2);

      // reset pir then wait 5 seconds
      SerialProtocol.Send p_reset_1 = new SerialProtocol.Send(pLU, cmd_X); 
      SerialProtocol.Expect p_reset_2 = new SerialProtocol.Expect(pLU, 6, cmd_M); // ascii ACK
      SerialProtocol.Send p_reset_3 = new SerialProtocol.Send(pLU, cmd_1234);
      SerialProtocol.Expect p_reset_4 = new SerialProtocol.Expect(pLU, 6, cmd_M); // ascii ACK
      SerialProtocol.Sleep p_wait_reset = new SerialProtocol.Sleep(5);

      // set sensitivity, set unsolicited mode
      SerialProtocol.Send p_sense_1 = new SerialProtocol.Send(pLU, cmd_S);
      SerialProtocol.Expect p_sense_2 = new SerialProtocol.Expect(pLU, 16, exp_none); // default value
      SerialProtocol.Send p_sense_3 = new SerialProtocol.Send(pLU, cmd_sense);
      SerialProtocol.Expect p_sense_4 = new SerialProtocol.Expect(pLU, 6, exp_none); // ascii ACK
      SerialProtocol.Send p_unsol_1 = new SerialProtocol.Send(pLU, cmd_M);
      SerialProtocol.Expect p_unsol_2 = new SerialProtocol.Expect(pLU, 78, exp_none); // ascii 'N'
      SerialProtocol.Send p_unsol_3 = new SerialProtocol.Send(pLU, cmd_Y);
      SerialProtocol.Expect p_unsol_4 = new SerialProtocol.Expect(pLU, 6, exp_none); // ascii ACK

      // monitor or fail
      SerialProtocol.Post p_monitor = new SerialProtocol.Post(pLU, mApp);
      SerialProtocol.Status pFail = new SerialProtocol.Status("start fail", mApp);

      mApp.mLogMessages.add("starting pir, sensitivity" + m_cfg_sensitivity);

      // link
      pLU.chain(p_wait_link, pFail);
      p_wait_link.chain(p_reset_1, pFail);

      // reset
      p_reset_1.chain(p_reset_2, pFail);
      p_reset_2.chain(p_reset_3, pFail);
      p_reset_3.chain(p_reset_4, pFail);
      p_reset_4.chain(p_wait_reset, pFail);
      p_wait_reset.chain(p_sense_1, pFail);

      // sensitivity and mode, then monitor
      p_sense_1.chain(p_sense_2, pFail);
      p_sense_2.chain(p_sense_3, p_sense_3); // old value doesn't really matter
      p_sense_3.chain(p_sense_4, pFail);
      p_sense_4.chain(p_unsol_1, pFail);
      p_unsol_1.chain(p_unsol_2, pFail);
      p_unsol_2.chain(p_unsol_3, pFail);
      p_unsol_3.chain(p_unsol_4, pFail);
      p_unsol_4.chain(p_monitor, pFail);

      Log.i(TAG, "dispatching start");
      mCommandHandler.post(pLU);
      return;
  }

  public void stop() {
      if (connected()) {
    SerialProtocol.LinkDown pLD = new SerialProtocol.LinkDown(pLU.mSocket);
    SerialProtocol.Status pDone = new SerialProtocol.Status("stop complete", mApp);
    SerialProtocol.Status pFail = new SerialProtocol.Status("stop fail", mApp);
    pLD.chain(pDone, pFail);

    Log.i(TAG, "dispatching link down");
    mErrorHandler.post(pLD);
      }
      return;
  }

    }
        
    @Override
    public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
    }
    
    @Override
    public void onCreate() {
  // code to execute when the service is first created
  mApp = (Hermit) getApplicationContext();
  mApp.mService = this;

  Properties properties = new Properties();
  try {
      FileInputStream in = new FileInputStream("/sdcard/Watcher/watch.properties");
      properties.load(in);
      in.close();
  } catch (Exception e) {
      Log.i(TAG, "CREATE - properties load failed ");
  }

  m_cfg_sensitivity = Integer.parseInt(properties.getProperty("sensitivity"));
  m_cfg_address = properties.getProperty("address");

    }
    
    @Override
    public void onDestroy() {
  // code to execute when the service is shutting down
  mZpSlave.stop();
  mZpSlave = null;
  mApp.mService = null;
  mApp.mLogMessages.add("SERVICE destroyed");
    }
    
    @Override
    public void onStart(Intent intent, int startid) {
  // code to execute when the service is starting up
  mApp.mLogMessages.add("SERVICE start: sense=" + 
            new Integer(m_cfg_sensitivity).toString() + ", addr=" + 
            m_cfg_address);
  mZpSlave = new ZilogPir();
  mZpSlave.start();
    }

    boolean connected() {
  if  ( mZpSlave != null ) {
      return mZpSlave.connected();
  }
  return false;
    }
        
}




Java Source Code List

com.appspot.staffordbears.watcher.CommLink.java
com.appspot.staffordbears.watcher.Hermit.java
com.appspot.staffordbears.watcher.SerialProtocol.java
com.appspot.staffordbears.watcher.SmsReceiver.java
com.appspot.staffordbears.watcher.Snap.java
com.appspot.staffordbears.watcher.Watch.java