Back to project page watcher.
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.
package com.appspot.staffordbears.watcher; // w ww .j a v a 2s . co m import android.util.Log; import java.io.IOException; import java.util.Date; import java.util.UUID; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.os.Looper; import android.widget.TextView; import android.os.Handler; import java.lang.Runnable; import java.io.BufferedReader; import java.io.FileReader; public class SerialProtocol { // well known SPP UUID private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // a protocol node that does nothing... public static class NullProto implements Runnable { enum State { READY, RUNNING, COMPLETE, FAILED } protected static final String TAG = "watcher.Protocol"; State mState = State.READY; NullProto mSuccessNext; NullProto mFailNext; public void run() { // head of the sequence execute(); } public void chain(NullProto success, NullProto fail) { mSuccessNext = success; mFailNext = fail; } public void execute() { finish(State.COMPLETE); } public void finish(State result) { mState = result; if (result == State.COMPLETE) { if (mSuccessNext != null) { mSuccessNext.execute(); } } else { if (mFailNext != null) { mFailNext.execute(); } } } public State get_state() { if (mState == State.COMPLETE) { if (mSuccessNext != null) { return mSuccessNext.get_state(); } } else if (mState == State.FAILED) { if (mFailNext != null) { return mFailNext.get_state(); } } return mState; } } // a protocol node to make the connection public static class LinkUp extends NullProto { protected BluetoothDevice mDevice; protected BluetoothAdapter mAdapter; protected String mBluetoothAddress = null; public BluetoothSocket mSocket = null; public LinkUp(String address) { // read the Bluetooth mac address mBluetoothAddress = address; mBluetoothAddress.toUpperCase(); } public void execute() { mAdapter = BluetoothAdapter.getDefaultAdapter(); if(mAdapter == null) { Log.w(TAG, "BTConnect -- no adapter"); } if(mAdapter.isEnabled() == false) { Log.w(TAG, "BTConnect -- not enabled"); } mDevice = mAdapter.getRemoteDevice(mBluetoothAddress); // the LinkUp function mState = State.RUNNING; try { mSocket = mDevice.createRfcommSocketToServiceRecord(SPP_UUID); // discovery is a heavyweight process so // disable while making a connection mAdapter.cancelDiscovery(); mSocket.connect(); super.finish(State.COMPLETE); } catch (IOException e) { Log.e(TAG, "LinkUp", e); super.finish(State.FAILED); } } } // a protocol node to break the connection public static class LinkDown extends NullProto { BluetoothSocket mSocket; public LinkDown(BluetoothSocket socket) { mSocket = socket; } public void execute() { // the LinkUp function mState = State.RUNNING; Log.i(TAG, "Beginning LinkDown"); try { Log.i(TAG, "LinkDown : closing input"); mSocket.getInputStream().close(); Log.i(TAG, "LinkDown : closing output"); mSocket.getOutputStream().close(); mSocket.close(); super.finish(State.COMPLETE); } catch (Exception e) { Log.e(TAG, "LinkDown", e); super.finish(State.FAILED); } } } // a protocol node to send a character public static class Send extends NullProto { LinkUp mLink; int[] mData; public Send(LinkUp link, int[] data) { mLink = link; mData = data; } public void execute() { // the Send function mState = State.RUNNING; try { for (int code : mData) { Log.i(TAG, "Sending : " + code); mLink.mSocket.getOutputStream().write(code); } Log.i(TAG, "Send complete. flushing..."); mLink.mSocket.getOutputStream().flush(); Log.i(TAG, "Flush complete"); super.finish(State.COMPLETE); } catch (Exception e) { Log.e(TAG, "Send", e); super.finish(State.FAILED); } } } // a protocol node to wait for a specific character public static class Expect extends NullProto { LinkUp mLink; int mData; int[] mIgnore; public Expect(LinkUp link, int data, int[] ignore) { mLink = link; mData = data; mIgnore = ignore; } public void execute() { // the Send function mState = State.RUNNING; Log.i(TAG, "Expect waiting for : " + mData); int b = 0; try { boolean skip = true; while ( skip ) { b = mLink.mSocket.getInputStream().read(); // if b == -1, socket is closed Log.i(TAG, "Expect received : " + b); skip = false; for (int iv:mIgnore) { if (b == iv) { skip = true; break; } } } if (b == mData) { super.finish(State.COMPLETE); } else { super.finish(State.FAILED); } } catch (IOException e) { Log.e(TAG, "Expect" + e); super.finish(State.FAILED); } } } // protocol node for a status message public static class Status extends NullProto { String mMessage; Hermit mApp; public Status(String message, Hermit application) { mMessage = message; mApp = application; } public void execute() { // the Send function mState = State.RUNNING; // Log.i(TAG, "MESSAGE" + mMessage); mApp.mLogMessages.add(mMessage); super.finish(State.COMPLETE); } } // protocol node to log characters till connection is closed public static class Post extends NullProto { LinkUp mLink; Hermit mApp; public Post(LinkUp link, Hermit application) { mLink = link; mApp = application; } public void execute() { // the Send function mState = State.RUNNING; mApp.mLogMessages.add("waiting for data..."); int b = 0; Date lastp = new Date(0); try { // if b == -1, socket is closed while ( b != -1 ) { Log.i(TAG, "Post waiting"); b = mLink.mSocket.getInputStream().read(); Log.i(TAG, "Post received : " + b); if (b == 77) { Date now = new Date(); mApp.mMotions.add(now); // photos no less than 4 seconds apart if ( now.getTime() - lastp.getTime() > 4000 ) { Snap snapper = new Snap(mApp); snapper.start(); lastp = new Date(); } } } super.finish(State.COMPLETE); } catch (IOException e) { Log.e(TAG, "Post" + e); super.finish(State.FAILED); } } } // protocol node for sleep public static class Sleep extends NullProto { int mSecs; public Sleep(int seconds) { mSecs = seconds; } public void execute() { // the Send function mState = State.RUNNING; // Log.i(TAG, "MESSAGE" + mMessage); try { Thread.sleep(mSecs*1000); } catch (InterruptedException e) { Log.e(TAG, "Sleep" + e); } super.finish(State.COMPLETE); } } }