Android Open Source - miner Connection Manager






From Project

Back to project page miner.

License

The source code is released under:

Apache License

If you think the Android project miner 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.kolomiyets.miner.bt;
//w  w  w  .j  ava2  s. c om
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.util.Log;

import com.kolomiyets.miner.Miner;
import com.kolomiyets.miner.MinerActivity;
import com.kolomiyets.miner.R;
import com.kolomiyets.miner.bt.notification.BtListener;
import com.kolomiyets.miner.bt.notification.BtNotificationState;
import com.kolomiyets.miner.bt.protocol.CmdBase;
import com.kolomiyets.miner.bt.protocol.CmdPing;
import com.kolomiyets.miner.bt.protocol.EGameState;
import com.kolomiyets.miner.dialog.DialogProvider;
import com.kolomiyets.miner.dialog.EDialogType;
import com.kolomiyets.miner.dialog.IDialogResult;
import com.kolomiyets.miner.screen.ScreenManager.ScreenType;

public class ConnectionManager {
  
  String TAG = ConnectionManager.class.getSimpleName();
  
  MinerActivity activity;
  BluetoothAdapter adapter;
  ConnectionBase connection;
  String prevConName;
  
  public ConnectionManager(MinerActivity activity) {
    this.activity = activity;
    this.adapter = BluetoothAdapter.getDefaultAdapter();
  }
  
  public boolean isBlueToothAvailable(){
    return adapter != null;
  }
  
  public boolean isBlueToothEnabled(){
    return adapter.isEnabled() && adapter.getState()==BluetoothAdapter.STATE_ON;
  }
  
  public boolean isBlueToothDiscovarable(){
    return adapter.getScanMode() == 
      BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
  }
  
  public void requestBlueToothEnable(int requestCode){
    if(Miner.D) Log.d(TAG, "requesting to enable BlueTooth; requestCode="+requestCode);
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    activity.startActivityForResult(enableBtIntent, requestCode);
  }
  
  public void requestBlueToothDiscoverable(int requestCode){
    if(Miner.D) Log.d(TAG, "requesting to make BlueTooth discoverable; requestCode="+requestCode);
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    activity.startActivityForResult(enableBtIntent, requestCode);
  }
  
  public void initiateConnection(EConnectMethod connectionType, String connectionName, BluetoothDevice device){
    if(Miner.D) Log.d(TAG, "initiating BlueTooth connection of type "+connectionType);
    stateListener.register(activity);
    switch (connectionType) {
    case MASTER:
      prevConName = adapter.getName();
      boolean result = adapter.setName(connectionName);
      if(Miner.D) Log.d(TAG, "set BlueTooth name to "+connectionName+" - "+(result?"OK":"ERR"));
      connection = new ConnectionMaster(
          ((Miner)activity.getApplicationContext()).getNotificationManager());
      break;
    case SLAVE:
      
      connection = new ConnectionSlave(
          device,
          ((Miner)activity.getApplicationContext()).getNotificationManager());
      break;
    default:
      break;
    }
    
    connection.connect();
  }
  
  public void terminateConnection() {
    stateListener.unregister(activity);
    if(connection!=null){
      if(connection instanceof ConnectionMaster) {
        adapter.setName(prevConName);
      }
      connection.disconnect();
      connection = null;
    }
  }
  
  public synchronized void sendCmd(CmdBase cmd) {
    if(connection!=null){
      connection.sendCmd(cmd);
    }
  }
  
  BtListener<BtNotificationState> stateListener = new BtListener<BtNotificationState>(BtNotificationState.class) {
    
    @Override
    public void processNotification(BtNotificationState notification) {
      switch (notification.code) {
      case CONNECTED:
        if(connection instanceof ConnectionSlave){
          connection.sendCmd(new CmdPing());
        }
        break;
      case FAILED:
        terminateConnection();
        DialogProvider.showDialog(activity, EDialogType.INFO, 
            activity.getString(R.string.dlg_ttl_err), 
            activity.getString(R.string.dlg_msg_err_connect_device), 
            new IDialogResult() {
          
          @Override
          public void onResult(DialogFragment dlg, int result) {
            dlg.dismiss();
            activity.getScreenManager().goTo(ScreenType.START);
          }
        });
        break;
      case TERMINATED:
        if(!activity.getCurrentGameState().equals(EGameState.FINISH)){
          terminateConnection();
          DialogProvider.showDialog(activity, EDialogType.INFO, 
              activity.getString(R.string.dlg_ttl_err), 
              activity.getString(R.string.dlg_msg_err_interrupted), 
              new IDialogResult() {
            
            @Override
            public void onResult(DialogFragment dlg, int result) {
              dlg.dismiss();
              activity.getScreenManager().goTo(ScreenType.START);
            }
          });
        }
        break;
      default:
        break;
      }
    }
  };
  
  public EConnectMethod getConnectMethod(){
    if(connection instanceof ConnectionMaster){
      return EConnectMethod.MASTER;
    } else {
      return EConnectMethod.SLAVE;
    }
  }
}




Java Source Code List

com.kolomiyets.miner.MinerActivity.java
com.kolomiyets.miner.Miner.java
com.kolomiyets.miner.bt.BtStateMonitor.java
com.kolomiyets.miner.bt.ConnectionBase.java
com.kolomiyets.miner.bt.ConnectionManager.java
com.kolomiyets.miner.bt.ConnectionMaster.java
com.kolomiyets.miner.bt.ConnectionProcessor.java
com.kolomiyets.miner.bt.ConnectionSlave.java
com.kolomiyets.miner.bt.EConnectMethod.java
com.kolomiyets.miner.bt.IBtEnableCallback.java
com.kolomiyets.miner.bt.IRequestBtResult.java
com.kolomiyets.miner.bt.notification.BtListener.java
com.kolomiyets.miner.bt.notification.BtNotificationCmd.java
com.kolomiyets.miner.bt.notification.BtNotificationConnect.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscovery.java
com.kolomiyets.miner.bt.notification.BtNotificationHandshake.java
com.kolomiyets.miner.bt.notification.BtNotificationPower.java
com.kolomiyets.miner.bt.notification.BtNotificationState.java
com.kolomiyets.miner.bt.notification.BtNotification.java
com.kolomiyets.miner.bt.notification.EConnectionSate.java
com.kolomiyets.miner.bt.notification.NotificationManager.java
com.kolomiyets.miner.bt.protocol.CmdBase.java
com.kolomiyets.miner.bt.protocol.CmdFactory.java
com.kolomiyets.miner.bt.protocol.CmdFieldState.java
com.kolomiyets.miner.bt.protocol.CmdGameState.java
com.kolomiyets.miner.bt.protocol.CmdGameTeam.java
com.kolomiyets.miner.bt.protocol.CmdHandshake.java
com.kolomiyets.miner.bt.protocol.CmdPing.java
com.kolomiyets.miner.bt.protocol.CmdResponse.java
com.kolomiyets.miner.bt.protocol.ECmdName.java
com.kolomiyets.miner.bt.protocol.EGameState.java
com.kolomiyets.miner.dialog.DialogProvider.java
com.kolomiyets.miner.dialog.EDialogType.java
com.kolomiyets.miner.dialog.IDialogResult.java
com.kolomiyets.miner.screen.PlayGroundBase.java
com.kolomiyets.miner.screen.PlayGroundMiner.java
com.kolomiyets.miner.screen.PlayGroundSapper.java
com.kolomiyets.miner.screen.ScreenBase.java
com.kolomiyets.miner.screen.ScreenConnectingGame.java
com.kolomiyets.miner.screen.ScreenCreateGame.java
com.kolomiyets.miner.screen.ScreenDevices.java
com.kolomiyets.miner.screen.ScreenManager.java
com.kolomiyets.miner.screen.ScreenSelectPlayer.java
com.kolomiyets.miner.view.GridCell.java