Android Open Source - BHRelay B G Service






From Project

Back to project page BHRelay.

License

The source code is released under:

GNU General Public License

If you think the Android project BHRelay 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.sjtu.bhrelay.service;
/*  w  w  w  .  j ava  2 s  .  c  om*/

//author:    clarkt
//email:    cleaktanglei@163.com



import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.sjtu.bhrelay.BHSearchActivity;
import com.sjtu.bhrelay.MainActivity;
import com.sjtu.bhrelay.util.BluetoothUtil;
import com.sjtu.bhrelay.util.BluetoothUtil.DataRecievedInterface;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.ArrayAdapter;

public class BGService extends Service{
  
  private static final String TAG = "BGService";
  public static final String ACTION = "bhrelay.service.BGService";
  public static final String ACTION_BLUETOOTH = ACTION + ".action_bluetooth";
  public static final String ACTION_NET = ACTION + ".action_net";
  public static final String ACTION_BH_SEARCH = ACTION_BLUETOOTH + ".search";
  public static final String ACTION_BH_ADDR = ACTION_BLUETOOTH + ".address";
  public static final String ACTION_BH_STATE = ACTION_BLUETOOTH + ".state";
  
  private static final int REQUEST_ENABLE_BT = 1;
  
  private BGServiceBroadcastReceiver mBroadcastReceiver;
  private IntentFilter intentFilter;
  private Context mcontext;
  
  private BluetoothUtil BHUtil = null;
  //BluetoothDevice BH_dev;
  //static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
  //private UUID uuid;
  //private BluetoothAdapter BH_adapter;  
  //private BluetoothSocket BH_socket = null;

  @Override
  public IBinder onBind(Intent intent) {
    Log.e(TAG,"onBind");
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.e(TAG,"onCreate");
    
    mcontext = this;
    
    //BH_adapter = BluetoothAdapter.getDefaultAdapter();
    //uuid = UUID.fromString(SPP_UUID);
    
    BHUtil = new BluetoothUtil(mcontext,BluetoothUtil.MODE_CLIENT);
    BHUtil.setOnDataRecievedListener(new DataRecievedInterface(){

      @Override
      public void onDataRecieved(String data) {
        // TODO Auto-generated method stub
        Log.e(TAG,"data recieved:"+data);
      }

      @Override
      public void onError(String error) {
        // TODO Auto-generated method stub
        Log.e(TAG,"error recieved:" + error);
      }
      
    });
    
      
    intentFilter = new IntentFilter();
    intentFilter.addAction(BGService.ACTION_BH_SEARCH); 
    intentFilter.addAction(BGService.ACTION_BH_ADDR); 
    intentFilter.addAction(BGService.ACTION_BH_STATE); 
    //intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 
    //intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);  
    //intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);  
    mBroadcastReceiver = new BGServiceBroadcastReceiver();
    registerReceiver(mBroadcastReceiver,intentFilter); 
    
  }
  
  /*
  private void BH_Bond(){
    
    int BH_connstate = BH_dev.getBondState();
    switch(BH_connstate){
    case BluetoothDevice.BOND_NONE:
      Log.e(TAG,"bond none");
      try {  
                Method createBondMethod = BluetoothDevice.class.getMethod("createBond");  
                createBondMethod.invoke(BH_dev);  
            } catch (Exception e) {   
                e.printStackTrace();  
            }  
      break;
    case BluetoothDevice.BOND_BONDED:
      Log.e(TAG,"bonded and start bluetooth connect!");
      BH_Connect();
      break;
    case BluetoothDevice.BOND_BONDING:
      Log.e(TAG,"bonding...");
      break;
    default:
      Log.e(TAG,"other bond state");
      break;
    }
  }
  
  private void BH_Connect(){
    Log.e(TAG,"start bluetooth connect");
    try {
      BH_socket = BH_dev.createRfcommSocketToServiceRecord(uuid);
      BH_socket.connect();
      BluetoothUtil.Broadcast(BHSearchActivity.ACTION_BH_CONNECTED,null,mcontext);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG,"bluetooth connect fail");
      e.printStackTrace();
    }
  }
  
  private void BH_Get_State(){
    Bundle bundle = new Bundle();
    if(null != BH_socket && BH_socket.isConnected()){
      bundle.putString("bh_state", BH_dev.getName() + "----" + BH_dev.getAddress());
    }else{
      bundle.putString("bh_state", "disconnect");
    }
    BluetoothUtil.Broadcast(BHSearchActivity.ACTION_BH_STATE,bundle,mcontext);
  }
  */
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.e(TAG,"onDestroy");
    unregisterReceiver(mBroadcastReceiver); 
  }
  
  private class BGServiceBroadcastReceiver extends BroadcastReceiver{
    
    public BGServiceBroadcastReceiver(){
      super();
    }
    
    @Override
    public void onReceive(Context context, Intent intent){
      Log.e(TAG,"on broadcast received");
      String action = intent.getAction();
      if(action.equals(BGService.ACTION_BH_ADDR)){
        Bundle bundle = intent.getExtras();
        String address = bundle.getString("address");
        Log.e(TAG,"get mac?" + address);
        //BH_dev = BH_adapter.getRemoteDevice(address);
        //BH_adapter.cancelDiscovery();          
        //BH_Bond();
        BHUtil.ClientConnect(address);
        
      }else if(BGService.ACTION_BH_STATE.equals(action)){
        Log.e(TAG,"get bluetooth state");
        
        //BH_Get_State();
      }
      /*
      else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
        Log.e(TAG,"action bond state change");
        //BH_Bond();
      }
      */
    }
     
  }

}




Java Source Code List

com.sjtu.bhrelay.BHSearchActivity.java
com.sjtu.bhrelay.MainActivity.java
com.sjtu.bhrelay.service.BGService.java
com.sjtu.bhrelay.util.BluetoothUtil.java
com.sjtu.bhrelay.util.TcpUtil.java