Android Open Source - final_project_android Arduino Receiver






From Project

Back to project page final_project_android.

License

The source code is released under:

MIT License

If you think the Android project final_project_android 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.geminnu.hexagon;
//  www .j a  v  a 2 s .c  o  m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ArduinoReceiver extends Thread{
  
  private final String TAG = "ArduinoReceiver";
  private final BluetoothSocket mSocket;
  private final Socket mWifiSocket;
    private InputStream mInStream;
    private InputStream tmpIn = null;
    private MessageListener mMsgListener;
    private boolean mConnected = false;

    public ArduinoReceiver(BluetoothSocket bluesocket, Socket wifisocket, MessageListener listener) {
        this.mSocket = bluesocket;
        this.mMsgListener = listener;
        this.mWifiSocket = wifisocket;
    }

    public void run() {
      if(mSocket != null) {
      try {
            tmpIn = mSocket.getInputStream();
            mInStream = tmpIn;
            mConnected = true;
        } catch (IOException e) {
            Log.d(TAG, "temp sockets not created");
            mConnected = false;
            mMsgListener.onDataReceived("problem");
        }

        Log.d(TAG, "Begin Listening");
        
        BufferedReader br = new BufferedReader(new InputStreamReader(mInStream));

        // Keep listening to the InputStream while connected
        while (mConnected && !Thread.interrupted()) {
            try {
              
              String data = br.readLine();
              
              if(data != null) {
                mMsgListener.onDataReceived(data);
                data = null;
              }
            } catch (IOException e) {
                Log.d(TAG, "disconnected");
                mConnected = false;
                mMsgListener.onDataReceived("problem");
                break;
            }
        }
        
        try {
      Log.d(TAG, "Close socket");
          br.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
      } else if(mWifiSocket != null){
        try {
                tmpIn = mWifiSocket.getInputStream();
                mInStream = tmpIn;
            } catch (IOException e) {
                Log.d(TAG, "temp sockets not created");
            }

            Log.d(TAG, "Begin Listening");
            
            BufferedReader br = new BufferedReader(new InputStreamReader(mInStream));

            // Keep listening to the InputStream while connected
            while (true && !Thread.interrupted()) {
                try {
                  
                  String data = br.readLine();
                  
                  if(data != null) {
                    mMsgListener.onDataReceived(data);
                    data = null;
                  } 
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    mMsgListener.onDataReceived("problem");
                    break;
                }
            }
            
            try {
          br.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } else {
        mConnected = false;
            mMsgListener.onDataReceived("problem");
      }
    }

}




Java Source Code List

com.geminnu.hexagon.Alert.java
com.geminnu.hexagon.ArduinoMessage.java
com.geminnu.hexagon.ArduinoParser.java
com.geminnu.hexagon.ArduinoReceiver.java
com.geminnu.hexagon.ArduinoService.java
com.geminnu.hexagon.ArduinoStatusEventListener.java
com.geminnu.hexagon.ArduinoStatusEvent.java
com.geminnu.hexagon.ArduinoStatusManager.java
com.geminnu.hexagon.ArduinoStatus.java
com.geminnu.hexagon.ArduinoTask.java
com.geminnu.hexagon.ArduinoTransmitter.java
com.geminnu.hexagon.BioSensorEventListener.java
com.geminnu.hexagon.BioSensorEvent.java
com.geminnu.hexagon.BioSensorListenerItem.java
com.geminnu.hexagon.BioSensorManagerService.java
com.geminnu.hexagon.BioSensor.java
com.geminnu.hexagon.Bluetooth.java
com.geminnu.hexagon.CoordinatorActionListener.java
com.geminnu.hexagon.Coordinator.java
com.geminnu.hexagon.MainActivity.java
com.geminnu.hexagon.MessageListener.java
com.geminnu.hexagon.MessageSender.java
com.geminnu.hexagon.MySQLiteHelper.java
com.geminnu.hexagon.Profile.java
com.geminnu.hexagon.Reading.java
com.geminnu.hexagon.Wifi.java