Android Open Source - blue-tool-box Main Activity






From Project

Back to project page blue-tool-box.

License

The source code is released under:

Apache License

If you think the Android project blue-tool-box 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.eperu.bluemessenger;
//from   w w w.j a va 2 s  .  c  om
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

  // private TextView listBtDevicesAlreadyPaired;

  private LinearLayout listBtDevicesAlreadyPaired;

  private LinearLayout listBtDevicesFound;

  private ProgressBar progressBar1;

  private BroadcastReceiver mReceiver = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_main);
    this.listBtDevicesAlreadyPaired = (LinearLayout) this.findViewById(R.id.listBtDevicesAlreadyPaired);
    this.listBtDevicesFound = (LinearLayout) this.findViewById(R.id.listBtDevicesFound);
    this.progressBar1 = (ProgressBar) this.findViewById(R.id.progressBar1);
    this.progressBar1.setVisibility(View.GONE);

    // search for other devices
    this.initializeButtsearchDevices();

    // Don't forget to unregister during onDestroy
    // Register the BroadcastReceiver
    this.mReceiver = BluetoothManager.getInstance(this).getReceiver(this);
    /**
     * FIXME regarder a : http://b2cloud.com.au/tutorial/listening-to-bluetooth-connections/
     */
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    this.registerReceiver(this.mReceiver, filter);
    BluetoothManager.getInstance(this).startDiscovery();

  }

  /**
   * Gestion du menu principal
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item != null) {
      if (item.getItemId() == R.id.action_refresh) {
        Log.v("System.out", "refresh ");
        BluetoothManager.getInstance(this).startDiscovery();
      }
      return true;
    }
    return false;
  }

  private void initializeButtsearchDevices() {
    Set<BluetoothDevice> devicesAlreadyPaired = BluetoothManager.getInstance(this).getDevicesAlreadyPaired();

    if (devicesAlreadyPaired != null && devicesAlreadyPaired.size() > 0) {
      for (final BluetoothDevice bd : devicesAlreadyPaired) {
        Button button = new Button(this);
        button.setText(bd.getName());
        this.listBtDevicesAlreadyPaired.addView(button);
        button.setClickable(true);
        button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = null;
            if (BluetoothDevice.DEVICE_TYPE_LE == bd.getType()) {
              intent = new Intent(MainActivity.this, BluetoothLEDeviceDetailsActivity.class);
            } else {
              intent = new Intent(MainActivity.this, BluetoothDeviceDetailsActivity.class);
            }
            intent.putExtra("device", bd);
            MainActivity.this.startActivity(intent);
          }
        });
        // button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
      }
    }
  }

  /**
   * Ajoute  la liste des devices trouves le device trouv.
   * 
   * @param device
   */
  public void refreshDevicesFound(final BluetoothDevice device) {
    this.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Button button = new Button(MainActivity.this);
        MainActivity.this.listBtDevicesFound.addView(button);
        String text = device.getName() + "-" + device.getAddress();
        if (BluetoothDevice.DEVICE_TYPE_LE == device.getType()) {
          text += " BLE";
        }
        button.setText(text);
        button.setClickable(true);
        button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = null;
            if (BluetoothDevice.DEVICE_TYPE_LE == device.getType()) {
              intent = new Intent(MainActivity.this, BluetoothLEDeviceDetailsActivity.class);
            } else {
              intent = new Intent(MainActivity.this, BluetoothDeviceDetailsActivity.class);
            }
            intent.putExtra("device", device);
            BluetoothManager.getInstance(MainActivity.this).stopScan();
            MainActivity.this.startActivity(intent);

          }
        });
      }
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    this.unregisterReceiver(this.mReceiver);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    this.getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

}




Java Source Code List

com.eperu.bluemessenger.BluetoothDeviceDetailsActivity.java
com.eperu.bluemessenger.BluetoothLEDeviceDetailsActivity.java
com.eperu.bluemessenger.BluetoothManager.java
com.eperu.bluemessenger.MainActivity.java