Guest User

Untitled

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