Guest User

Untitled

a guest
Apr 2nd, 2015
269
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (C) 2011 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5.  * use this file except in compliance with the License. You may obtain a copy of
  6.  * the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13.  * License for the specific language governing permissions and limitations under
  14.  * the License.
  15.  */
  16.  
  17. package com.cellbots.logger;
  18.  
  19. import android.app.Activity;
  20. import android.bluetooth.BluetoothDevice;
  21. import android.bluetooth.BluetoothManager;
  22. import android.content.ComponentName;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.ServiceConnection;
  26. import android.hardware.Camera;
  27. import android.os.Build;
  28. import android.os.Bundle;
  29. import android.os.IBinder;
  30. import android.util.Log;
  31. import android.view.View;
  32. import android.view.View.OnClickListener;
  33. import android.widget.Button;
  34. import android.widget.CheckBox;
  35. import android.widget.EditText;
  36. import android.widget.Toast;
  37.  
  38. import com.mbientlab.metawear.api.MetaWearBleService;
  39. import com.mbientlab.metawear.api.MetaWearController;
  40. import com.mbientlab.metawear.api.Module;
  41. import com.mbientlab.metawear.api.controller.Accelerometer;
  42. import com.mbientlab.metawear.api.controller.Accelerometer.Axis;
  43. import com.mbientlab.metawear.api.controller.Accelerometer.MovementData;
  44. import com.mbientlab.metawear.api.controller.MechanicalSwitch;
  45. //import com.cellbots.logger.SettingsFragment.SettingsState;
  46.  
  47. import com.cellbots.logger.localServer.ServerControlActivity;
  48.  
  49. /**
  50.  * A simple Activity for choosing which mode to launch the data logger in.
  51.  *
  52.  * @author clchen@google.com (Charles L. Chen)
  53.  */
  54. public class LauncherActivity extends Activity  implements ServiceConnection {
  55.     private MetaWearBleService mwService= null;
  56.     private CheckBox useZipCheckbox;
  57.     private MetaWearController mwCtrllr;
  58.     private final String MW_MAC_ADDRESS= "F5:49:5E:07:04:D2";
  59.     private Accelerometer accelCtrllr;
  60.  
  61.     @Override
  62.     public void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         ///< Bind the MetaWear service when the activity is created
  65.         getApplicationContext().bindService(new Intent(this, MetaWearBleService.class),
  66.                 this, Context.BIND_AUTO_CREATE);
  67.         setContentView(R.layout.main);
  68.  
  69.         useZipCheckbox = (CheckBox) findViewById(R.id.useZip);
  70.  
  71.         final Activity self = this;
  72.         Button launchLocalServerButton = (Button) findViewById(R.id.launchLocalServer);
  73.         launchLocalServerButton.setOnClickListener(new OnClickListener() {
  74.             @Override
  75.             public void onClick(View v) {
  76.                 Intent i = new Intent(LauncherActivity.this, ServerControlActivity.class);
  77.                 startActivity(i);
  78.                 finish();
  79.             }
  80.         });
  81.         Button launchVideoFrontButton = (Button) findViewById(R.id.launchVideoFront);
  82.         launchVideoFrontButton.setOnClickListener(new OnClickListener() {
  83.             @Override
  84.             public void onClick(View v) {
  85.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_FRONT, useZipCheckbox.isChecked());
  86.             }
  87.         });
  88.         Button launchVideoBackButton = (Button) findViewById(R.id.launchVideoBack);
  89.         launchVideoBackButton.setOnClickListener(new OnClickListener() {
  90.             @Override
  91.             public void onClick(View v) {
  92.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_BACK, useZipCheckbox.isChecked());
  93.             }
  94.         });
  95.         final EditText pictureDelayEditText = (EditText) findViewById(R.id.pictureDelay);
  96.         Button launchPictureButton = (Button) findViewById(R.id.launchPicture);
  97.         launchPictureButton.setOnClickListener(new OnClickListener() {
  98.             @Override
  99.             public void onClick(View v) {
  100.                 Intent i = new Intent(self, LoggerActivity.class);
  101.                 i.putExtra(LoggerActivity.EXTRA_MODE, LoggerActivity.MODE_PICTURES);
  102.                 i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZipCheckbox.isChecked());
  103.                 int delay = 30;
  104.                 try {
  105.                     delay = Integer.parseInt(pictureDelayEditText.getText().toString());
  106.                 } catch (Exception e) {
  107.                     Toast.makeText(self,
  108.                             "Error parsing picture delay time. Using default delay of 30 seconds.",
  109.                             Toast.LENGTH_LONG).show();
  110.                 }
  111.                 i.putExtra(LoggerActivity.EXTRA_PICTURE_DELAY, delay);
  112.                 startActivity(i);
  113.                 finish();
  114.             }
  115.         });
  116.         // The code we are using for taking video through the front camera
  117.         // relies on APIs added in SDK 9. Don't offer the front video option to
  118.         // users on devices older than that OR to devices who have only one
  119.         // camera. Currently assume that if only one camera is present, it is
  120.         // the back camera.
  121.         if (Build.VERSION.SDK_INT < 9 || Camera.getNumberOfCameras() == 1) {
  122.             launchVideoFrontButton.setVisibility(View.GONE);
  123.         }
  124.     }
  125.     @Override
  126.     public void onDestroy() {
  127.         super.onDestroy();
  128.  
  129.         if (mwService != null) {
  130.             ///< Don't forget to unregister the MetaWear receiver
  131.             mwService.unregisterReceiver(MetaWearBleService.getMetaWearBroadcastReceiver());
  132.         }
  133.         getApplicationContext().unbindService(this);
  134.     }
  135.  
  136.     @Override
  137.     public void onServiceConnected(ComponentName name, IBinder service) {
  138.         Log.i("logdebug","service connected started");
  139.         mwService= ((MetaWearBleService.LocalBinder) service).getService();
  140.  
  141.         final BluetoothManager btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  142.         final BluetoothDevice mwBoard = btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);
  143.         mwCtrllr= mwService.getMetaWearController(mwBoard);
  144.         mwCtrllr.setRetainState(false);
  145.         ///< Register the callback, log message will appear when connected
  146.         mwCtrllr.addDeviceCallback(dCallbacks);
  147.  
  148. ///< Remove the callback, no feedback for when a ble connection is made
  149.         //mwCtrllr.removeDeviceCallback(dCallbacks);
  150.         mwCtrllr.connect();
  151.         Log.i("logdebug","connecting..");
  152.     }
  153.  
  154.     private MetaWearController.DeviceCallbacks dCallbacks= new MetaWearController.DeviceCallbacks() {
  155.         @Override
  156.         public void connected() {
  157.             Log.i("logdebug", "A Bluetooth LE connection has been established!");
  158.             Toast.makeText(getApplicationContext(), R.string.toast_connected, Toast.LENGTH_SHORT).show();
  159.             accelCtrllr= ((Accelerometer) mwCtrllr.getModuleController(Module.ACCELEROMETER));
  160.             accelCtrllr.enableShakeDetection(Axis.X);
  161.             accelCtrllr.startComponents();
  162.  
  163.             mwCtrllr.addModuleCallback(new Accelerometer.Callbacks() {
  164.                 @Override
  165.                 public void shakeDetected(MovementData moveData) {
  166.                     Toast.makeText(getApplicationContext(), "SHAKE!", Toast.LENGTH_SHORT).show();
  167.                     Log.i("logdebug", "Shake Detected!");
  168.  
  169.                 }
  170.             });
  171.  
  172.  
  173.         }
  174.  
  175.         @Override
  176.         public void disconnected() {
  177.             Log.i("logdebug", "Lost the Bluetooth LE connection!");
  178.         }
  179.  
  180.     };
  181.  
  182.     ///< Don't need this callback method but we must implement it
  183.     @Override
  184.     public void onServiceDisconnected(ComponentName name) {
  185.         Log.i("logdebug", "service disconnected");
  186.     }
  187.  
  188.     private void launchLoggingActivity(int mode, boolean useZip) {
  189.         Intent i = new Intent(LauncherActivity.this, LoggerActivity.class);
  190.         i.putExtra(LoggerActivity.EXTRA_MODE, mode);
  191.         i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZip);
  192.         startActivity(i);
  193.         finish();
  194.     }
  195. }
RAW Paste Data