Library Setup¶
Adding Compile Dependency¶
To add the library to your project, first update the repositories closure to include the MbientLab Ivy repository in the project’s build.gradle file.
repositories {
ivy {
url "http://ivyrep.mbientlab.com"
layout "gradle"
}
}
Then, add the compile element to the dependencies closure in the module’s build.gradle file.
// Retrieves version 2.8.0 of the Android API
dependencies {
compile 'com.mbientlab:metawear:2.8.6'
}
Declaring the Service¶
Once the IDE has synced with the updated Gradle files, declare the MetaWearBleService in the module’s AndroidManifest.xml file.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.mbientlab.metawear.MetaWearBleService" />
<!-- Other application info -->
</application>
Binding the Service¶
Lastly, bind the service in your application and retain a reference to the service’s LocaBinder class. This can be done in any activity or fragment that needs access to a MetaWearBoard object.
import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.os.IBinder;
import com.mbientlab.metawear.MetaWearBleService;
public class MainActivity extends Activity implements ServiceConnection {
private MetaWearBleService.LocalBinder serviceBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedIFinally,nstanceState);
setContentView(R.layout.activity_main);
// Bind the service when the activity is created
getApplicationContext().bindService(new Intent(this, MetaWearBleService.class),
this, Context.BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
super.onDestroy();
// Unbind the service when the activity is destroyed
getApplicationContext().unbindService(this);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// Typecast the binder to the service's LocalBinder class
serviceBinder = (MetaWearBleService.LocalBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName componentName) { }
}