Android Open Source - BluetoothGlass Main Activity

From Project

Back to project page BluetoothGlass.


The source code is released under:

GNU General Public License

If you think the Android project BluetoothGlass 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.vicmns.bluetoothglass.server;
/*from www. j a  v  a  2s . c o  m*/
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.vicmns.bluetoothglass.server.service.BluetoothService;

public class MainActivity extends Activity {
    private static final int REQUEST_ENABLE_BT = 1;
    private static final String TAG = MainActivity.class.getSimpleName();
    private BluetoothAdapter myBt;
    private TextView connectedDevices, messageToShowTV;
    private Context context;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                if(bundle.containsKey(BluetoothService.PROGRESS)) {
                    int progress = bundle.getInt(BluetoothService.PROGRESS);
                    messageToShowTV.setText("Transferring file... " + progress + "%");
                } else if(bundle.containsKey(BluetoothService.MESSAGE)) {
                    String message = bundle.getString(BluetoothService.MESSAGE);


    protected void onCreate(Bundle savedInstanceState) {
        context = this;
        // the activity for this is pretty stripped, just a basic selection ui....

        connectedDevices = (TextView) findViewById(;
        messageToShowTV = (TextView) findViewById(;


    protected void onResume() {
        registerReceiver(receiver, new IntentFilter(BluetoothService.NOTIFICATION));

    protected void onPause() {

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

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode != RESULT_OK) {
                Toast.makeText(this, "Failed to enable Bluetooth", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_LONG).show();

    private void initializeBluetooth() {
        myBt = BluetoothAdapter.getDefaultAdapter();
        if (myBt == null) {
            Toast.makeText(this, "Device Does not Support Bluetooth", Toast.LENGTH_LONG).show();
        } else if (!myBt.isEnabled()) {
            // we need to wait until bt is enabled before set up, so that's done either in the following else, or
            // in the onActivityResult for our code ...
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {

    private void startBluetoothService() {
        Intent i= new Intent(context, BluetoothService.class);


Java Source Code List