Android Open Source - miner Bt State Monitor






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 a v  a  2 s  .c  om*/
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.kolomiyets.miner.Miner;
import com.kolomiyets.miner.bt.notification.BtNotification;
import com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice;
import com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState;
import com.kolomiyets.miner.bt.notification.BtNotificationPower;

public class BtStateMonitor extends BroadcastReceiver {

  private String TAG = BtStateMonitor.class.getSimpleName();
  
  private final FragmentActivity activity;
  
  public BtStateMonitor(FragmentActivity activity) {
    this.activity = activity;
  }
  
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
      int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
      String stateStr;
      switch (state) {
      case BluetoothAdapter.STATE_ON:
        stateStr = "<ON>";
        break;
      case BluetoothAdapter.STATE_OFF:
        stateStr = "<OFF>";
        break;
      case BluetoothAdapter.STATE_TURNING_ON:
        stateStr = "<TURNING_ON>";
        break;
      case BluetoothAdapter.STATE_TURNING_OFF:
        stateStr = "<TURNING_OFF>";
        break;
      default:
        stateStr = "<unknown>";
        break;
      }
      publish(context, new BtNotificationPower(state));
      if(Miner.D) Log.d(TAG, "power state: "+stateStr);
    } else if(action.equals(BluetoothDevice.ACTION_FOUND)){
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      BluetoothClass clas = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
      String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
      Short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
      
      if(Miner.D) Log.d(TAG, "new device: "+device.getName()+"["+device.getAddress()+"]");
      
      publish(context, new BtNotificationDiscoveryDevice(device, clas, name, rssi));
    } else if(action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
      int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.STATE_DISCONNECTED);
      String stateStr;
      switch (state) {
      case BluetoothAdapter.STATE_CONNECTED:
        stateStr = "<CONNECTED>";
        break;
      case BluetoothAdapter.STATE_DISCONNECTED:
        stateStr = "<DISCONNECTED>";
        break;
      case BluetoothAdapter.STATE_CONNECTING:
        stateStr = "<CONNECTING>";
        break;
      case BluetoothAdapter.STATE_DISCONNECTING:
        stateStr = "<DISCONNECTING>";
        break;
      default:
        stateStr = "<unknown>";
        break;
      }
      if(Miner.D) Log.d(TAG, "connection state: "+stateStr);
    } else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
      if(Miner.D) Log.d(TAG, "DISCOVERY_STARTED");
      publish(context, new BtNotificationDiscoveryState(true));
    } else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
      if(Miner.D) Log.d(TAG, "DISCOVERY_FINISHED");
      publish(context, new BtNotificationDiscoveryState(false));
    }
  }

  public void startMonitoring() {
    IntentFilter filter = new IntentFilter();
    
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    
    activity.registerReceiver(this, filter);
  }
  
  public void stopMonitoring() {
    activity.unregisterReceiver(this);
  }
  
  private void publish(Context context, BtNotification notification){
    ((Miner)context.getApplicationContext())
    .getNotificationManager().postNotification(notification);
  }
}




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