Android Open Source - cicada Device Service






From Project

Back to project page cicada.

License

The source code is released under:

Apache License

If you think the Android project cicada 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 org.cicadasong.cicada;
/*from  w ww.  j  a  v  a  2 s  .  co  m*/
import java.io.IOException;
import java.util.UUID;

import org.cicadasong.cicada.MetaWatchConnection.Mode;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class DeviceService extends Service {
  public static final String TAG = DeviceService.class.getSimpleName();

  public static final int MESSAGETYPE_ACTIVATE = 1;
  public static final int MESSAGETYPE_DEACTIVATE = 2;
  public static final int MESSAGETYPE_BUTTON_EVENT = 3;
  
  // The "well-known" UUID that the Android docs recommend using for SPP.
  public static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  private BluetoothSocket socket;
  private boolean connected;
  private MetaWatchConnection connection;
  private MetaWatchConnection.Listener listener;
  
  private final DeviceServiceBinder binder = new DeviceServiceBinder();
  
  public class DeviceServiceBinder extends Binder {
    public DeviceService getService() {
      return DeviceService.this;
    }
  }
  
  @Override
  public IBinder onBind(Intent intent) {
    Log.v(TAG, "onBind service");
    return binder;
  }
  
  @Override
  public void onCreate() {
    Log.v(TAG, "onCreate service");
    connectToDevice();

    super.onCreate();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(TAG, "onStartCommand");
    return super.onStartCommand(intent, flags, startId);
  }

  @Override
  public void onDestroy() {
    disconnectFromDevice();
    Log.v(TAG, "onDestroy service");
    super.onDestroy();
  }
  
  private void sendConnectionNotice() {
    Log.v(TAG, "Connected to device");
    
  }
  
  private void sendDisconnectionNotice() {
    Log.v(TAG, "Disconnected from device");
    
  }
  
  private void sendButtonPress() {
    
  }
  
  public void vibrate(int timeOnMs, int timeOffMs, int cycles) {
    connection.vibrate(timeOnMs, timeOffMs, cycles);
  }
  
  public void updateScreen(byte[] buffer, Mode mode) {
    if (connection != null) {
      connection.updateScreen(buffer, mode);
    }
  }
  
  public void setMode(Mode mode) {
    if (connection != null) {
      connection.updateDisplayToMode(mode);
    }
  }
  
  private void connectToDevice() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice device;
    String mac = PrefUtil.getWatchMAC(this);
    try {
      device = adapter.getRemoteDevice(mac);
    } catch (IllegalArgumentException e) {
      // TODO: Bad MAC address, tell the user
      Log.v(TAG, "MAC address not valid: " + mac);
      return;
    }
    try {
      socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
      socket.connect();
      connection = new MetaWatchConnection(socket, listener);
      sendConnectionNotice();
      connection.configureWatchMode(Mode.APPLICATION, 255, false);
      connection.vibrate(500, 0, 1);  // 01 0c 23 00 01 f4 01 00 00 01 ee 00 
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  }
  
  private void disconnectFromDevice() {
    if (connection != null) {
      connection.setNativeClockVisible(true);
      connection.flush();
    }
    
    if (socket != null) {
      try {
        socket.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    sendDisconnectionNotice();
  }
  
  public void setListener(MetaWatchConnection.Listener listener) {
    this.listener = listener;
    if (connection != null) {
      connection.setListener(listener);
    }
  }

  /**
   * Handler of incoming messages from CicadaService.
   */
  class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      switch(msg.what){
      case MESSAGETYPE_ACTIVATE:
//        if (!isActive) {
//          sessionId = msg.arg1;
//          activate(AppType.values()[msg.arg2]);
//        }
        break;

      case MESSAGETYPE_DEACTIVATE:
//        if (isActive) {
//          deactivate();
//        }
        break;
        
      case MESSAGETYPE_BUTTON_EVENT:
//        ButtonEvent event = new ButtonEvent((byte) msg.arg1);
//        onButtonPress(event);
        break;

      default:
        super.handleMessage(msg);
        break;
      }
    }
  }


}




Java Source Code List

com.hydraproductions.cicada.StopWatch.java
org.cicadasong.apollo.ApolloConfig.java
org.cicadasong.apollo.ApolloIntents.java
org.cicadasong.apollo.BitmapUtil.java
org.cicadasong.apollo.SimulatedDisplayView.java
org.cicadasong.cicada.AppDatabase.java
org.cicadasong.cicada.AppDescription.java
org.cicadasong.cicada.AppList.java
org.cicadasong.cicada.AppScanner.java
org.cicadasong.cicada.CicadaService.java
org.cicadasong.cicada.CicadaSettingsActivity.java
org.cicadasong.cicada.Cicada.java
org.cicadasong.cicada.DeviceService.java
org.cicadasong.cicada.HotkeySetupActivity.java
org.cicadasong.cicada.HotkeySetupEntry.java
org.cicadasong.cicada.MetaWatchConnection.java
org.cicadasong.cicada.NotificationRenderer.java
org.cicadasong.cicada.PackageMonitor.java
org.cicadasong.cicada.PackageUtil.java
org.cicadasong.cicada.PrefUtil.java
org.cicadasong.cicada.WidgetScreen.java
org.cicadasong.cicada.WidgetSetup.java
org.cicadasong.cicadalib.CicadaApp.java
org.cicadasong.cicadalib.CicadaIntents.java
org.cicadasong.cicadalib.CicadaNotificationManager.java
org.cicadasong.cicadalib.CicadaNotification.java
org.cicadasong.cicadalib.TextBlock.java
org.cicadasong.samples.bitly.Bitly.java
org.cicadasong.samples.digitalclock.DigitalClock.java
org.cicadasong.samples.hellocicada.HelloCicada.java
org.cicadasong.samples.nextbuses.NextBuses.java
org.cicadasong.samples.notificationdemo.NotificationDemo.java
org.cicadasong.samples.notifications.NotificationAccessibilityService.java
org.cicadasong.samples.notifications.Notifications.java
org.cicadasong.samples.quicktext.QuickTextSetup.java
org.cicadasong.samples.quicktext.QuickText.java
org.cicadasong.samples.smsnotifier.SMSBroadcastReceiver.java
org.cicadasong.samples.tubestatus.Preferences.java
org.cicadasong.samples.tubestatus.TubeStatusSettingsActivity.java
org.cicadasong.samples.tubestatus.TubeStatus.java
org.cicadasong.samples.webimageplayer.Preferences.java
org.cicadasong.samples.webimageplayer.WebImagePlayerSettingsActivity.java
org.cicadasong.samples.webimageplayer.WebImagePlayer.java