Android Open Source - miner Connection Base






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;
//  ww  w. ja  va 2 s  .c o m
import java.util.UUID;

import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.kolomiyets.miner.Miner;
import com.kolomiyets.miner.bt.notification.BtNotificationState;
import com.kolomiyets.miner.bt.notification.EConnectionSate;
import com.kolomiyets.miner.bt.notification.NotificationManager;
import com.kolomiyets.miner.bt.protocol.CmdBase;

public abstract class ConnectionBase {
  
  String TAG = ConnectionBase.class.getSimpleName();
  
//  public final static String MINER_BT_SERVER_NAME = "Miner Android Game";
  public final static UUID MINER_UUID = UUID.fromString("1C04DBB7-D264-4FCA-82E6-26103FB1FA48");
//  public final static UUID MINER_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  
  NotificationManager notificationManager;
  private Thread connectThread;
  private Thread disconnectThread;
  private ConnectionProcessor connectionProcessor;
  
  ConnectionBase(NotificationManager notificationManager) {
    this.notificationManager = notificationManager;
  }
  
  public synchronized void connect() {
    killThread(connectThread);
    connectThread = new Thread(){
      public void run() {
        setName("BlueTooth connect thread");
        
        doDisconnection(connectionProcessor);
        
        BluetoothSocket socket = doConnection();
        if(socket!=null){
          if(Miner.D) Log.d(TAG, "connect - " + "OK");
          connectionProcessor = new ConnectionProcessor(socket, notificationManager);
          connectionProcessor.start();
          notificationManager.postNotification(new BtNotificationState(EConnectionSate.CONNECTED));
        } else {
          if(Miner.D) Log.d(TAG, "connect - " + "ERR");
          notificationManager.postNotification(new BtNotificationState(EConnectionSate.FAILED));
          disconnect();
        }
      };
    }; 
    connectThread.start();
  }
  
  public synchronized void disconnect(){
    killThread(disconnectThread);
    disconnectThread = new Thread(){
      public void run() {
        setName("BlueTooth disconnect thread");
        doDisconnection(connectionProcessor);
        killThread(connectThread);
        if(Miner.D) Log.d(TAG, "disconnect - OK");
      };
    };
    disconnectThread.start();
  }
  
  public synchronized void sendCmd(CmdBase cmd){
    connectionProcessor.writeCmd(cmd);
  }
  
  public static void killThread(Thread thread){
    try {
      if(thread!=null){
        final Thread _thread = thread;
        _thread.interrupt();
        _thread.join(5000);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally{
      thread = null;
    }
  }
  
  abstract BluetoothSocket doConnection();
  abstract void doDisconnection(ConnectionProcessor connectionProcessor);
}




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