Android Open Source - WifiHotspotBTEnabler Bluetooth Utilities






From Project

Back to project page WifiHotspotBTEnabler.

License

The source code is released under:

GNU General Public License

If you think the Android project WifiHotspotBTEnabler 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 ca.diogosoares.android.utilities;
/*from  www. jav  a2s. com*/
import java.util.ArrayList;
import java.util.Set;

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.util.Log;
import ca.diogosoares.android.listeners.BluetoothUtilitiesListener;


/**
 * 
 * Require permissions for :
 * android.permission.BLUETOOTH
 * android.permission.BLUETOOTH_ADMIN
 * 
 * @author Diogo Soares, 02/2014
 */
public class BluetoothUtilities {
  private ArrayList<BluetoothUtilitiesListener> mListeners = null;
  
  private int mState = BluetoothAdapter.STATE_DISCONNECTED;
  private int mPreviousState = BluetoothAdapter.STATE_DISCONNECTED;
  
  private Context mContext = null;
  
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          final BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
          
      if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        mState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        mPreviousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE,BluetoothAdapter.ERROR);

        for (BluetoothUtilitiesListener listener : mListeners)
          listener.onBluetoothStateChanged();
        
      } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action) || BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) || BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
        for (BluetoothUtilitiesListener listener : mListeners)
          listener.onBluetoothACLChanged(action, device);
        
      } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        for (BluetoothUtilitiesListener listener : mListeners)
          listener.onBluetoothBondStateChanged();
        
      } else {
        Log.w("BluetoothUtilities::BroadcastReceiver::onReceive::LEAK",  action);
      }
    }
  };
  
  public BluetoothUtilities(Context context) {
    this.mContext = context;
    
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
    
    mListeners = new ArrayList<BluetoothUtilitiesListener>();
  }
  
  public void setListener(BluetoothUtilitiesListener listener) { this.mListeners.add(listener); }
  public void removeListener(BluetoothUtilitiesListener listener) { this.mListeners.remove(listener); }
  
  public int getState() { return this.mState; }
  public int getPreviousState() { return this.mPreviousState; }
  
  public Set<BluetoothDevice> getBondedDevices() { return BluetoothAdapter.getDefaultAdapter().getBondedDevices(); }
  
  public static String getStateString(int state) {
    switch (state) {
    case BluetoothAdapter.STATE_CONNECTED:
      return "Connected";
    case BluetoothAdapter.STATE_CONNECTING:
      return "Connecting";
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "Disconnected";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "Disconnecting";
    case BluetoothAdapter.STATE_OFF:
      return "Off";
    case BluetoothAdapter.STATE_ON:
      return "On";
    case BluetoothAdapter.STATE_TURNING_OFF:
      return "Turning Off";
    case BluetoothAdapter.STATE_TURNING_ON:
      return "Turning On";
    default:
      return "Unknown";
    }
  }

  public static String getMajorString(int major) {
    switch (major) {
    case BluetoothClass.Device.Major.AUDIO_VIDEO:
      return "AUDIO_VIDEO";
    case BluetoothClass.Device.Major.COMPUTER:
      return "COMPUTER";
    case BluetoothClass.Device.Major.HEALTH:
      return "HEALTH";
    case BluetoothClass.Device.Major.IMAGING:
      return "IMAGING";
    case BluetoothClass.Device.Major.MISC:
      return "MISC";
    case BluetoothClass.Device.Major.NETWORKING:
      return "NETWORKING";
    case BluetoothClass.Device.Major.PERIPHERAL:
      return "PERIPHERAL";
    case BluetoothClass.Device.Major.PHONE:
      return "PHONE";
    case BluetoothClass.Device.Major.TOY:
      return "TOY";
    case BluetoothClass.Device.Major.UNCATEGORIZED:
      return "UNCATEGORIZED";
    case BluetoothClass.Device.Major.WEARABLE:
      return "AUDIO_VIDEO";
    default:
      return "Unknown";
    }
  }

  public static String getDeviceType(int type) {
    switch (type) {
    case BluetoothDevice.DEVICE_TYPE_CLASSIC:
      return "CLASSIC";
    case BluetoothDevice.DEVICE_TYPE_DUAL:
      return "DUAL";
    case BluetoothDevice.DEVICE_TYPE_LE:
      return "LE";
    default:
      return "Unknown";
    }
  }
}




Java Source Code List

ca.diogosoares.android.HotspotBTEnabler.BTDevice.java
ca.diogosoares.android.HotspotBTEnabler.Config.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerListener.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerManagerFragment.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerManager.java
ca.diogosoares.android.HotspotBTEnabler.MainActivity.java
ca.diogosoares.android.listeners.BluetoothUtilitiesListener.java
ca.diogosoares.android.listeners.HotspotUtilitiesListener.java
ca.diogosoares.android.listeners.WifiUtilitiesListener.java
ca.diogosoares.android.utilities.BluetoothUtilities.java
ca.diogosoares.android.utilities.HotspotUtilities.java
ca.diogosoares.android.utilities.WifiUtilities.java