Android Open Source - SmartGlove Main Activity

From Project

Back to project page SmartGlove.


The source code is released under:

MIT License

If you think the Android project SmartGlove 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.example.smartgloveproject;
//from   www .j ava2s . c  om
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.telephony.SmsManager;
import android.text.TextUtils;
import at.abraxas.amarino.Amarino;
import at.abraxas.amarino.AmarinoIntent;

public class MainActivity extends ListActivity {

  // DEVICE_ADDRESS must be exactly the MAC address of the bluetooth dongle
  private static final String TAG = "SmartGlove";
  private static final String DEVICE_ADDRESS = "00:06:66:64:45:DA";
  // register the receiver that will listen for data from the arduino
  private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();
  // avoid sending too much data
  final int DELAY = 1000;
  long lastChange;
  protected void onCreate(Bundle savedInstanceState) {
    // connect to the arduino bluetooth
    //Amarino.connect(this, DEVICE_ADDRESS);
    List<Sms> smsList = new ArrayList<Sms>();
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor c = getContentResolver().query(uri, null, null, null, null);
    if (c.moveToFirst()) {
      for (int i = 0; i < 20 && i < c.getCount(); i++) {
        String message = c.getString(c.getColumnIndexOrThrow("body"));
        String number  = c.getString(c.getColumnIndexOrThrow("address"));
        Date date = new Date(Long.parseLong(c.getString(c.getColumnIndexOrThrow("date"))));
        String displayName = getContactNameFromNumber(number);
        smsList.add(new Sms(number, message, date, displayName));
    setListAdapter(new ListAdapter(this, smsList));
  protected void onStart() {
    Log.d("arduino", "starting receiver");
    registerReceiver(arduinoReceiver, new IntentFilter(AmarinoIntent.ACTION_RECEIVED));
    Amarino.connect(this, DEVICE_ADDRESS);
  protected void onStop() {
    Amarino.disconnect(this, DEVICE_ADDRESS);
    Log.d("arduino", "stopping receiver");
    // can save state variables here, e.g.
    // PerferenceManager.getDefaultSharedPreferences(this).edit().putInt("red", red).commit();
  protected void onNewIntent(Intent intent) {
  private void processNewTexts(Intent intent) {
    Bundle b = intent.getExtras();
    ArrayList<Sms> newSms = b.getBundle("txtContainer")
    Log.d("new sms", "got messages (" + newSms.size() + ")");
    ArrayAdapter<Sms> boundData = (ArrayAdapter<Sms>)getListAdapter();
    for (int i = 0; i < newSms.size(); i++) {
      Sms thisSms = newSms.get(i);
      boundData.insert(newSms.get(i), i);

  private String getContactNameFromNumber(String number) {
    // see if we can't get the contact name
    Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor phoneCursor = managedQuery(phoneUri, new String[] {PhoneLookup.DISPLAY_NAME}, null, null, null);
    String displayName = "Unknown";
    if (phoneCursor != null && phoneCursor.moveToFirst())
      displayName = phoneCursor.getString(0);
    return displayName;
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Sms sms = (Sms)getListAdapter().getItem(position);
    Toast.makeText(getApplicationContext(), sms.getMessage(), Toast.LENGTH_LONG).show();
    // send text to arduino, less delay
    if (System.currentTimeMillis() - lastChange > DELAY) {
      lastChange = System.currentTimeMillis();
  private void sendUpdate(Sms sms) {
    String contact = sms.getContactName();
    if (contact.equalsIgnoreCase("unknown")) contact = sms.getNumber();
    final String message = sms.getMessage();

    Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', contact);
    // instead of SystemClock.sleep(300), do the following
    final MainActivity thisAct = this;
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      public void run() {
        Amarino.sendDataToArduino(thisAct, DEVICE_ADDRESS, 'm', message);    
    }, 300);    
  /* old method
  protected void onCreate(Bundle savedInstanceState) {

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;
  public void makeArbitraryToast(String str) {
    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
  public void sendResponseSmsToLastText() {
    // get the last text
    ArrayAdapter<Sms> boundData = (ArrayAdapter<Sms>)getListAdapter();
    Sms lastTxt = boundData.getItem(0);
    Log.d("arduino", "sending response text to" + lastTxt.getNumber());
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(lastTxt.getNumber(), null, "I'm busy presenting myself right now, I'll contact you soon!", null, null);
   * Hold the receiver for communication from the Arduino
  public class ArduinoReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
      String data = null;
      final String address = intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS); // for fun, i guess
      final int dataType = intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1); // dunno, this is what the tutorial does
      Log.d("arduino", "received some data");
      if (dataType == AmarinoIntent.STRING_EXTRA) {
        data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);
        if (data != null) {
          // do different things based on the data received
          Log.d("arduino", "received data:" + data);

Java Source Code List