Android Open Source - BHRelay B H Search Activity






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;
//from w w  w  .  j a v  a 2 s  .com
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.sjtu.bhrelay.service.BGService;
import com.sjtu.bhrelay.util.BluetoothUtil;

import android.app.Activity;
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.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BHSearchActivity extends Activity {

  public static final String ACTION_ACTIVITY_FINISH = "bhrelay.bhsearchactivity.finish";
  public static final String ACTION_BH_STATE = "bhrelay.bhsearchactivity.get.bluetooth.state";
  public static final String ACTION_BH_CONNECTED = "bhrelay.bhsearchactivity.get.bluetooth.connect";
  
  private Context mcontext;
  private ImageButton ibtn_back;
  private Button btn_search;
  private TextView tv_btstate;
  
  private boolean is_BH_conn = false;
  private String BH_dev_info = "";
  
  private static final String TAG = "BHSearchActivity";
  private static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
  private static final int REQUEST_ENABLE_BT = 2;
  private UUID uuid;
  private BluetoothAdapter BH_adapter;
  
  private BHSearchActivityBroadcastReceiver mBroadcastReceiver;
  private IntentFilter intentFilter;
  
  private ListView lv_device;
  private ArrayAdapter<String> adtDevices;
  private List<String> listDevices = new ArrayList<String>();  
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bhsearch);
    mcontext = this;
    
    BluetoothUtil.Broadcast(BGService.ACTION_BH_STATE,null,mcontext);
    
    ibtn_back = (ImageButton)findViewById(R.id.ibtn_back);
    ibtn_back.setOnClickListener(new OnClickListener(){

      @Override
      public void onClick(View v) {
        finish();
      }
      
    });
    
    btn_search = (Button)findViewById(R.id.btn_search);
    btn_search.setOnClickListener(new OnClickListener(){

      @Override
      public void onClick(View v) {
        SearchDevice(); 
      }
      
    });
    
    tv_btstate = (TextView) findViewById(R.id.tv_btstate);
    
    lv_device = (ListView)findViewById(R.id.lv_device);
    adtDevices = new ArrayAdapter<String>(BHSearchActivity.this,
          android.R.layout.simple_list_item_1, listDevices);
    lv_device.setAdapter(adtDevices);
    lv_device.setOnItemClickListener(new OnItemClickListener(){
      
      @Override
      public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){
        String str = listDevices.get(arg2);
        if(!str.equals(BH_dev_info)){
          String[] values = str.split("----");
          Bundle bundle = new Bundle();
          bundle.putString("address", values[1]);
          BluetoothUtil.Broadcast(BGService.ACTION_BH_ADDR,bundle,mcontext);
          Log.e(TAG,"select this device:"+ values[1]);
        }else{
          Log.e(TAG,"bluetooth has connected doesn't need to connect");
        }
      }
    });
    
    BH_adapter = BluetoothAdapter.getDefaultAdapter();
    uuid = UUID.fromString(SPP_UUID);
    

    
    if(BH_adapter == null){
      Log.e(TAG, "No BlueToothDevice!");
      Toast.makeText(mcontext, "????????????", Toast.LENGTH_LONG).show();
      finish();
      return;
    }else{      
      SearchDevice(); 
    }
    
    intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    intentFilter.addAction(BHSearchActivity.ACTION_ACTIVITY_FINISH);
    intentFilter.addAction(BHSearchActivity.ACTION_BH_STATE);
    intentFilter.addAction(BHSearchActivity.ACTION_BH_CONNECTED);
    mBroadcastReceiver = new BHSearchActivityBroadcastReceiver();
    this.registerReceiver(mBroadcastReceiver, intentFilter);                                                   
  }
  
  private void SearchDevice(){
    if(!BH_adapter.isEnabled()){
      Log.e(TAG,"the bluetooth haven't open yet");
           Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
         startActivityForResult(enableIntent, REQUEST_ENABLE_BT);  
    }else{
      Log.e(TAG,"start discovery");
      tv_btstate.setText(mcontext.getString(R.string.search_onsearch));
      listDevices.clear();
      BH_adapter.startDiscovery();
    }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    
    if(requestCode == REQUEST_ENABLE_BT){  
            if(resultCode == RESULT_OK){  
              Log.e(TAG,"the bluetooth is opened");
              SearchDevice();
              tv_btstate.setText(mcontext.getString(R.string.search_onsearch));
            }  
    } 
  }
  
  private class BHSearchActivityBroadcastReceiver extends BroadcastReceiver{
    
    public BHSearchActivityBroadcastReceiver(){
      super();
    }
    
    @Override
    public void onReceive(Context context, Intent intent){
      String action = intent.getAction();
      Log.e(TAG,"on broadcast received:" + action);
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        Log.e(TAG,"on recieve found bluetooth");
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        String str= device.getName() + "----" + device.getAddress();
        if (listDevices.indexOf(str) == -1)// prevent repeating item adding
          listDevices.add(str); // get device's name and its mac
        adtDevices.notifyDataSetChanged();
      }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
        tv_btstate.setText(mcontext.getString(R.string.search_finish));
      }else if(BHSearchActivity.ACTION_BH_STATE.equals(action)){
        Log.e(TAG,"recieve bluetooth connect state");
        String state = intent.getExtras().getString("bh_state");
        if(state.equals("disconnect")){
          is_BH_conn = false;
        }else{
          BH_dev_info = state;
          is_BH_conn = true;
        }
      }else if(BHSearchActivity.ACTION_BH_CONNECTED.equals(action)){
        finish();
      }

    }
     
  }
}




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