Guest User

Untitled

a guest
Apr 2nd, 2015
206
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.support.v4.content.LocalBroadcastManager;
  31. import android.util.Log;
  32. import android.view.View;
  33. import android.view.View.OnClickListener;
  34. import android.widget.Button;
  35. import android.widget.CheckBox;
  36. import android.widget.EditText;
  37. import android.widget.Toast;
  38.  
  39. import com.mbientlab.metawear.api.MetaWearBleService;
  40. import com.mbientlab.metawear.api.MetaWearController;
  41. import com.mbientlab.metawear.api.Module;
  42. import com.mbientlab.metawear.api.controller.Accelerometer;
  43. import com.mbientlab.metawear.api.controller.Accelerometer.Axis;
  44. import com.mbientlab.metawear.api.controller.Accelerometer.MovementData;
  45. import com.mbientlab.metawear.api.controller.MechanicalSwitch;
  46. //import com.cellbots.logger.SettingsFragment.SettingsState;
  47.  
  48. import com.cellbots.logger.localServer.ServerControlActivity;
  49.  
  50. /**
  51.  * A simple Activity for choosing which mode to launch the data logger in.
  52.  *
  53.  * @author clchen@google.com (Charles L. Chen)
  54.  */
  55. public class LauncherActivity extends Activity  implements ServiceConnection {
  56.     private MetaWearBleService mwService= null;
  57.     private CheckBox useZipCheckbox;
  58.     private MetaWearController mwCtrllr;
  59.     private final String MW_MAC_ADDRESS= "F5:49:5E:07:04:D2";
  60.     private Accelerometer accelCtrllr;
  61.     private LocalBroadcastManager broadcastManager= null;
  62.  
  63.     @Override
  64.     public void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         ///< Bind the MetaWear service when the activity is created
  67.         getApplicationContext().bindService(new Intent(this, MetaWearBleService.class),
  68.                 this, Context.BIND_AUTO_CREATE);
  69.         setContentView(R.layout.main);
  70.  
  71.         useZipCheckbox = (CheckBox) findViewById(R.id.useZip);
  72.  
  73.         final Activity self = this;
  74.         Button launchLocalServerButton = (Button) findViewById(R.id.launchLocalServer);
  75.         launchLocalServerButton.setOnClickListener(new OnClickListener() {
  76.             @Override
  77.             public void onClick(View v) {
  78.                 Intent i = new Intent(LauncherActivity.this, ServerControlActivity.class);
  79.                 startActivity(i);
  80.                 finish();
  81.             }
  82.         });
  83.         Button launchVideoFrontButton = (Button) findViewById(R.id.launchVideoFront);
  84.         launchVideoFrontButton.setOnClickListener(new OnClickListener() {
  85.             @Override
  86.             public void onClick(View v) {
  87.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_FRONT, useZipCheckbox.isChecked());
  88.             }
  89.         });
  90.         Button launchVideoBackButton = (Button) findViewById(R.id.launchVideoBack);
  91.         launchVideoBackButton.setOnClickListener(new OnClickListener() {
  92.             @Override
  93.             public void onClick(View v) {
  94.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_BACK, useZipCheckbox.isChecked());
  95.             }
  96.         });
  97.         final EditText pictureDelayEditText = (EditText) findViewById(R.id.pictureDelay);
  98.         Button launchPictureButton = (Button) findViewById(R.id.launchPicture);
  99.         launchPictureButton.setOnClickListener(new OnClickListener() {
  100.             @Override
  101.             public void onClick(View v) {
  102.                 Intent i = new Intent(self, LoggerActivity.class);
  103.                 i.putExtra(LoggerActivity.EXTRA_MODE, LoggerActivity.MODE_PICTURES);
  104.                 i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZipCheckbox.isChecked());
  105.                 int delay = 30;
  106.                 try {
  107.                     delay = Integer.parseInt(pictureDelayEditText.getText().toString());
  108.                 } catch (Exception e) {
  109.                     Toast.makeText(self,
  110.                             "Error parsing picture delay time. Using default delay of 30 seconds.",
  111.                             Toast.LENGTH_LONG).show();
  112.                 }
  113.                 i.putExtra(LoggerActivity.EXTRA_PICTURE_DELAY, delay);
  114.                 startActivity(i);
  115.                 finish();
  116.             }
  117.         });
  118.         // The code we are using for taking video through the front camera
  119.         // relies on APIs added in SDK 9. Don't offer the front video option to
  120.         // users on devices older than that OR to devices who have only one
  121.         // camera. Currently assume that if only one camera is present, it is
  122.         // the back camera.
  123.         if (Build.VERSION.SDK_INT < 9 || Camera.getNumberOfCameras() == 1) {
  124.             launchVideoFrontButton.setVisibility(View.GONE);
  125.         }
  126.     }
  127.     @Override
  128.     public void onDestroy() {
  129.         super.onDestroy();
  130.  
  131.         if (mwService != null) {
  132.             ///< Don't forget to unregister the MetaWear receiver
  133.             mwService.unregisterReceiver(MetaWearBleService.getMetaWearBroadcastReceiver());
  134.         }
  135.         getApplicationContext().unbindService(this);
  136.     }
  137.  
  138.     @Override
  139.     public void onServiceConnected(ComponentName name, IBinder service) {
  140.         Log.i("logdebug","service connected started");
  141.         mwService= ((MetaWearBleService.LocalBinder) service).getService();
  142.  
  143.         broadcastManager= LocalBroadcastManager.getInstance(mwService);
  144.         broadcastManager.registerReceiver(MetaWearBleService.getMetaWearBroadcastReceiver(),
  145.                 MetaWearBleService.getMetaWearIntentFilter());
  146.         mwService.useLocalBroadcastManager(broadcastManager);
  147.  
  148.         final BluetoothManager btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  149.         final BluetoothDevice mwBoard = btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);
  150.         mwCtrllr= mwService.getMetaWearController(mwBoard);
  151.         mwCtrllr.setRetainState(false);
  152.         ///< Register the callback, log message will appear when connected
  153.         mwCtrllr.addDeviceCallback(new MetaWearController.DeviceCallbacks() {
  154.                        @Override
  155.                        public void connected() {
  156.                            Log.i("logdebug", "A Bluetooth LE connection has been established!");
  157.                            Toast.makeText(getApplicationContext(), R.string.toast_connected, Toast.LENGTH_SHORT).show();
  158.                            accelCtrllr= ((Accelerometer) mwCtrllr.getModuleController(Module.ACCELEROMETER));
  159.                            accelCtrllr.enableShakeDetection(Axis.X);
  160.                            accelCtrllr.startComponents();
  161.  
  162.                        }
  163.  
  164.                        @Override
  165.                        public void disconnected() {
  166.                            Log.i("logdebug", "Lost the Bluetooth LE connection!");
  167.                        }
  168.  
  169.         }).addModuleCallback(new Accelerometer.Callbacks() {
  170.             @Override
  171.             public void shakeDetected(MovementData moveData) {
  172.                 Toast.makeText(getApplicationContext(), "SHAKE!", Toast.LENGTH_SHORT).show();
  173.                 Log.i("logdebug", "Shake Detected!");
  174.  
  175.             }
  176.         });
  177.  
  178.         mwCtrllr.connect();
  179.         Log.i("logdebug","connecting..");
  180.     }
  181.  
  182.  
  183.  
  184.     ///< Don't need this callback method but we must implement it
  185.     @Override
  186.     public void onServiceDisconnected(ComponentName name) {
  187.         Log.i("logdebug", "service disconnected");
  188.     }
  189.  
  190.     private void launchLoggingActivity(int mode, boolean useZip) {
  191.         Intent i = new Intent(LauncherActivity.this, LoggerActivity.class);
  192.         i.putExtra(LoggerActivity.EXTRA_MODE, mode);
  193.         i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZip);
  194.         startActivity(i);
  195.         finish();
  196.     }
  197. }
RAW Paste Data