Android Open Source - compCellScope Main Activity






From Project

Back to project page compCellScope.

License

The source code is released under:

MIT License

If you think the Android project compCellScope 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.wallerlab.compcellscope;
//w ww.  j  av a  2 s .c  o  m
import com.wallerlab.compcellscope.BluetoothService;
import com.wallerlab.compcellscope.AcquireActivity;
import com.wallerlab.compcellscope.MultiModeViewActivity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
  private static final String TAG = "cCS_main";
  private static final boolean Debug = true;
  String defaultAddress = "00:06:66:66:23:53";
  
  // Message types sent from the BluetoothChatService Handler
  public static final int MESSAGE_STATE_CHANGE = 1;
  public static final int MESSAGE_READ = 2;
  public static final int MESSAGE_WRITE = 3;
  public static final int MESSAGE_DEVICE_NAME = 4;
  public static final int MESSAGE_TOAST = 5;

  // Key names received from the BluetoothChatService Handler
  public static final String DEVICE_NAME = "device_name";
  public static final String DEVICE_ADDRESS = "device_address";
  public static final String TOAST = "toast";

  // Intent request codes
  private static final int REQUEST_CONNECT_DEVICE = 1;
  private static final int REQUEST_ENABLE_BT = 2;
  
  // Name of the connected device
  private String mConnectedDeviceName = null;
  private String mConnectedDeviceMACAddress = null;
  // Array adapter for the conversation thread
  private ArrayAdapter<String> mConversationArrayAdapter;
  // String buffer for outgoing messages
  private StringBuffer mOutStringBuffer;
  
  // Local Bluetooth adapter
  private BluetoothAdapter mBluetoothAdapter = null;
  // Member object for the chat services
  private BluetoothService mBluetoothService = null;
  
  Intent serverIntent = null;
  public boolean btConnection;
  
  boolean on = false;

  Button btnConnectBluetooth, btnSettings, btnAcquireFullScan,btnAcquireBFScan, btnAcquireMultiMode,btnMultiModeViewer, btnGallery;
  TextView connStatusTextView, connDeviceNameTextView, connMACAddressTextView;
  Spinner ledArraySpinner;
   
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    btnMultiModeViewer = (Button) findViewById(R.id.btnMultiModeViewer);
    btnConnectBluetooth = (Button) findViewById(R.id.btnConnectBluetooth);
    btnAcquireBFScan = (Button) findViewById(R.id.btnAcquireBFScan);
    btnAcquireFullScan = (Button) findViewById(R.id.btnAcquireFullScan);
    btnAcquireMultiMode = (Button) findViewById(R.id.btnAcquireMuitimode);
    btnGallery = (Button) findViewById(R.id.btnGallery);
    
    connStatusTextView = (TextView) findViewById(R.id.connStatusTextView);
    connDeviceNameTextView = (TextView) findViewById(R.id.connDeviceNameTextView);
    connMACAddressTextView = (TextView) findViewById(R.id.connMACAddressTextView);
    
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    
    // Create the BT service object
  mBluetoothService = new BluetoothService(this, mHandler);
  
    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    
    // See if we've already got an instance of the bluetooth connection going, and if so update the UI to reflect this
  BluetoothApplicationClass BTAppClass = (BluetoothApplicationClass) getApplication();
  if (BTAppClass.getBluetoothService() != null)
  {
    //Toast.makeText(this, "Bluetooth class is active!", Toast.LENGTH_LONG).show();
    mBluetoothService = BTAppClass.getBluetoothService();
    
        connDeviceNameTextView.setText(mBluetoothService.getDeviceName());
        connMACAddressTextView.setText(mBluetoothService.getDeviceAddress());
    
        connStatusTextView.setText("Connected to Array");
        btnConnectBluetooth.setText("Disconnect from Array");
        btConnection = true;
  } else {
    // Try and connect to the default class
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(defaultAddress);
        // Attempt to connect to the device
        mBluetoothService.connect(device);
    BTAppClass.setBluetoothService(mBluetoothService);
  }
    
    btnConnectBluetooth.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          if (btConnection)
          {
            sendData("endConnection");
            stopBTService();
          }
          else
            startBTService();
        }
      });
    
    btnAcquireBFScan.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          startAcquireActivity("Brightfield_Scan");
        }
      });
    
    btnAcquireFullScan.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          startAcquireActivity("Full_Scan");
        }
      });
    
    btnAcquireMultiMode.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          startAcquireActivity("MultiMode");
          
        }
    });
    
    btnMultiModeViewer.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          startMultiViewerActivity();
          
          
        }
    });
    
    btnGallery.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
      startGallery();
    }
  });
    
    
    
    }

  @Override
  public void onResume() {
    super.onResume();
    Log.d(TAG, "...onResume - try connect...");
  }
  
  @Override
  public void onPause() {
    super.onPause();
    Log.d(TAG, "...In onPause()...");
  }
  
  //ensure app is in portrait orientation
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }

  //fire intent to start activity with proper configuration for acquire type
  protected void startAcquireActivity(String type) {
      Intent intent = new Intent(this, AcquireActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("type", type);
      startActivity(intent);
  }
  
  protected void startMultiViewerActivity() {
      Intent intent = new Intent(this, MultiModeViewActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
  }

  protected void startBluetooth(String deviceName) {
      if (!mBluetoothAdapter.isEnabled()) {
        Log.d(TAG, "Asking to enable BT");
          Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
      } else {
          if (mBluetoothService == null) {
            startBTService();
          }
      }
  }
  
  protected void startGallery(){
      Intent intent = new Intent(this, Folder_Chooser.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
  }
  
  // The Handler that gets information back from the BluetoothChatService
  private final Handler mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          switch (msg.what) {
          case MESSAGE_STATE_CHANGE:
              if(Debug) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
              switch (msg.arg1) {
              case BluetoothService.STATE_CONNECTED:
                connStatusTextView.setText("Connected to Array");
                btnConnectBluetooth.setText("Disconnect from Array");
                btConnection = true;
                  break;      
              case BluetoothService.STATE_CONNECTING:
                connStatusTextView.setText("Connecting to Array");
                  connDeviceNameTextView.setText("");
                  connMACAddressTextView.setText("");
                btConnection = false;
                  break;
              case BluetoothService.STATE_LISTEN:
                connStatusTextView.setText("Disconnected");
                btnConnectBluetooth.setText("Connect to Array");
                  connDeviceNameTextView.setText("");
                  connMACAddressTextView.setText("");
                btConnection = false;
                break;
              case BluetoothService.STATE_NONE:
                connStatusTextView.setText("Disconnected");
                btnConnectBluetooth.setText("Connect to Array");
                  connDeviceNameTextView.setText("");
                  connMACAddressTextView.setText("");
                btConnection = false;
                  break;
              }
              break;
          case MESSAGE_WRITE:
              byte[] writeBuf = (byte[]) msg.obj;
              // construct a string from the buffer
              String writeMessage = new String(writeBuf);
              mConversationArrayAdapter.add("Me:  " + writeMessage);
              break;
          case MESSAGE_READ:
              //byte[] readBuf = (byte[]) msg.obj;
              // construct a string from the valid bytes in the buffer
              //String readMessage = new String(readBuf, 0, msg.arg1);
              //mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
              break;
          case MESSAGE_DEVICE_NAME:
              // save the connected device's name
              mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
              mConnectedDeviceMACAddress = msg.getData().getString(DEVICE_ADDRESS);
              Toast.makeText(getApplicationContext(), "Connected to "
                             + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
              connDeviceNameTextView.setText(mConnectedDeviceName);
              connMACAddressTextView.setText(mConnectedDeviceMACAddress);
              break;
          case MESSAGE_TOAST:
              Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                             Toast.LENGTH_SHORT).show();
              break;
          }
      }
  };
  
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(Debug) Log.d(TAG, "onActivityResult " + resultCode);
      switch (requestCode) {
      case REQUEST_CONNECT_DEVICE:
          // When DeviceListActivity returns with a device to connect
          if (resultCode == Activity.RESULT_OK) {
              // Get the device MAC address
              String address = data.getExtras().getString(BluetoothDeviceListActivity.EXTRA_DEVICE_ADDRESS);
              // Show the address for debugging
              if(Debug)Toast.makeText(this, address, Toast.LENGTH_SHORT).show();
              // Get the BLuetoothDevice object
              BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
              // Attempt to connect to the device
              mBluetoothService.connect(device);
          }
          break;
      case REQUEST_ENABLE_BT:
          // When the request to enable Bluetooth returns
          if (resultCode == Activity.RESULT_OK) {
              // Bluetooth is now enabled, so set up the Bluetooth service
            startBTService();
          } else {
              // User did not enable Bluetooth or an error occured
              Log.d(TAG, "BT not enabled");
              Toast.makeText(this, "BT not enabled", Toast.LENGTH_SHORT).show();
              finish();
          }
      }
  }
  
  private void startBTService() 
  {
    Log.d(TAG, "Starting BT Service");
  
    // Initialize the buffer for outgoing memBtAdapterssages
    mOutStringBuffer = new StringBuffer("");
    
      serverIntent = new Intent(this, BluetoothDeviceListActivity.class);
      startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
      
    // Tie the service to the global application context
    BluetoothApplicationClass BTAppClass = (BluetoothApplicationClass) getApplication();
    BTAppClass.setBluetoothService(mBluetoothService);
      
  }
  private void stopBTService() {
    Log.d(TAG, "Stopping BT Service");
    mBluetoothService.stop();
  }
  
  
  public void sendData(String message) {
      // Check that we're actually connected before trying anything
      if (mBluetoothService.getState() != BluetoothService.STATE_CONNECTED) {
          Toast.makeText(this, "NOT CONNECTED", Toast.LENGTH_SHORT).show();
          return;
      }

      // Check that there's actually something to send
      if (message.length() > 0) {
          // Get the message bytes and tell the BluetoothChatService to write
          byte[] send = message.getBytes();
          mBluetoothService.write(send);
      }
  }
}




Java Source Code List

com.wallerlab.compcellscope.AcquireActivity.java
com.wallerlab.compcellscope.AcquireActivity.java
com.wallerlab.compcellscope.AcquireSettings.java
com.wallerlab.compcellscope.AcquireSurfaceView.java
com.wallerlab.compcellscope.AcquireSurfaceView.java
com.wallerlab.compcellscope.BluetoothActivity.java
com.wallerlab.compcellscope.BluetoothApplicationClass.java
com.wallerlab.compcellscope.BluetoothApplicationClass.java
com.wallerlab.compcellscope.BluetoothDeviceListActivity.java
com.wallerlab.compcellscope.BluetoothDeviceListActivity.java
com.wallerlab.compcellscope.BluetoothService.java
com.wallerlab.compcellscope.BluetoothService.java
com.wallerlab.compcellscope.BurstModeActivity.java
com.wallerlab.compcellscope.DirectoryChooserDialog.java
com.wallerlab.compcellscope.Folder_Chooser.java
com.wallerlab.compcellscope.ImageProgressTask.java
com.wallerlab.compcellscope.ImageProgressTask.java
com.wallerlab.compcellscope.Image_Gallery.java
com.wallerlab.compcellscope.MainActivity.java
com.wallerlab.compcellscope.MainActivity.java
com.wallerlab.compcellscope.MultiModeViewActivity.java
com.wallerlab.compcellscope.MultiModeView.java
com.wallerlab.compcellscope.PortraitCameraView.java