Android Open Source - HexNanoController_Android Ble Connectin Manager






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android 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

/**
 * //from  ww w  .  j av a2s.  co  m
 */
package com.hexairbot.hexmini.ble;


import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * @author koupoo
 *
 */
public class BleConnectinManager  {
  private static final String TAG = BleConnectinManager.class.getSimpleName();

  private BluetoothDevice currentDevice;
  
  private Context context;
  private BleConnectinManagerDelegate delegate;
  
  private BluetoothLeService mBluetoothLeService;
  
  private boolean isConnected;
  
   // Code to manage Service lifecycle.
    private  ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
            if (!mBluetoothLeService.initialize()) {
                Log.e(TAG, "Unable to initialize Bluetooth");
                mBluetoothLeService = null;
            }
            else{
                Log.e(TAG, "mBluetoothLeService is okay");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
          Log.e(TAG, "onServiceDisconnected");
           // mBluetoothLeService = null;
        }
    };
    
    // Handles various events fired by the Service.
    // ACTION_GATT_CONNECTED: connected to a GATT server.
    // ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
    // ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
    // ACTION_DATA_AVAILABLE: received data from the device.  This can be a result of read
    //                        or notification operations.
    private BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          
            final String action = intent.getAction();
            if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {  //??????
              Log.e(TAG, "Only gatt, just wait");
            } 
            else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { //???????
                Log.e(TAG,  "ACTION_GATT_DISCONNECTED");
                
                Log.e(TAG, "thread name:" + Thread.currentThread().getName());
                
              isConnected = false;
              
                if (delegate != null) {
          delegate.didDisconnect(BleConnectinManager.this);
        }
            }
            else if(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) //???????????
            {
              //Toast.makeText(BleConnectinManager.this.context, "???????????????????????", Toast.LENGTH_SHORT).show();
              isConnected = true;
              
              /*
            if(currentConnection != connection) {
              Log.d(TAG, "didConnect:old connection, just ignore");
            }
            */
            
            if (delegate != null) {
              delegate.didConnect(BleConnectinManager.this);
            }
            }
            else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { //???????
              Log.e(TAG, "RECV DATA");
              String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
              
              
              if (delegate != null) {
          delegate.didReceiveData(BleConnectinManager.this, data);
        }
            }
            else{
              Toast.makeText(BleConnectinManager.this.context, "Unkonwn??", Toast.LENGTH_SHORT).show();  
            }
        }
    };
  
  
  public BleConnectinManager(Context context){
    super();
    this.context = context;
    
    Intent gattServiceIntent = new Intent(this.context, BluetoothLeService.class);
          Log.d(TAG, "Try to bindService=" + this.context.bindService(gattServiceIntent, mServiceConnection, android.content.Context.BIND_AUTO_CREATE));
          
      this.context.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
  }
  
  
  public BluetoothDevice getCurrentDevice() {
    return currentDevice;
  }
  
  
  public void connect(BluetoothDevice device){
    Log.d(TAG, "try connect");
  
    if (device.equals(currentDevice)) {
      if (isConnected()) {
        return;
      } 
      else {
        if (mBluetoothLeService != null) {
          mBluetoothLeService.connect(device.getAddress());
        }
      }
    }
    else{
      closeCurrentGatt();
      
      currentDevice = device;
      
      if (mBluetoothLeService != null) {
        mBluetoothLeService.connect(currentDevice.getAddress());
      }
    }
  }
  
  
  public void disconnect(){
    if (mBluetoothLeService != null) {
      mBluetoothLeService.disconnect();
    } 

    return;
  }
  
  
  public void closeCurrentGatt(){
    if (mBluetoothLeService != null) {
      mBluetoothLeService.close();
      currentDevice = null;
    }
  }
  
  
  public void close() {
    Log.e(TAG, "release resource");
    
    if (mGattUpdateReceiver != null) {
      this.context.unregisterReceiver(mGattUpdateReceiver);
      mGattUpdateReceiver = null;
    }
    
    if (mServiceConnection != null) {
      this.context.unbindService(mServiceConnection);
      mServiceConnection = null;
    }
        
    if(mBluetoothLeService != null){
      mBluetoothLeService.close();
         mBluetoothLeService = null;
    }
    
        Log.d(TAG, "releaseSource");
  }
  

  public void sendData(String data) {
    if (mBluetoothLeService != null && isConnected()) {
      mBluetoothLeService.WriteValue(data);  
    }
  }
  
  
  public void sendData(byte[] data) {
    if (mBluetoothLeService != null && isConnected()) {
      mBluetoothLeService.WriteValue(data);
    }
  }
  
  
  public boolean isConnected(){
    return isConnected;
  }

  
  public BleConnectinManagerDelegate getDelegate() {
    return delegate;
  }

  
  public void setDelegate(BleConnectinManagerDelegate delegate) {
    this.delegate = delegate;
  }

  
  public Context getContext() {
    return context;
  }

  
  public void setContext(Context context) {
    this.context = context;
  }


  private  IntentFilter makeGattUpdateIntentFilter() {                        //?????????
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        intentFilter.addAction(BluetoothDevice.ACTION_UUID);
        return intentFilter;
    }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java