Android Open Source - Bluetooth Main Activity






From Project

Back to project page Bluetooth.

License

The source code is released under:

GNU General Public License

If you think the Android project Bluetooth 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 heck.pinball.simpleghost.simpleghost;
//from  w w  w.  j a  v a2 s.c o m
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.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

//More imports


public class MainActivity extends Activity {

    private Button bluetoothOn, bluetoothOff, bluetoothVisible, bluetoothlist, L000, L007, bluetoothConnect, bluetoothDisconnect, bluetoothDiscover;
    private BluetoothAdapter mBluetoothAdapter;
    private Set<BluetoothDevice> pairedDevices;
    private ListView listViewDiscovery;
    private static final UUID BT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private UUID uuid = UUID.fromString("39f7b019-b0da-4a59-ad73-061d4603b514");
/*
    @Override
    public void run() {
    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothOn = (Button) findViewById(R.id.buttonBluetooth_1);
        bluetoothOff = (Button) findViewById(R.id.buttonBluetooth_0);
        bluetoothDiscover = (Button) findViewById(R.id.buttonDiscover);
        bluetoothVisible = (Button) findViewById(R.id.buttonDiscoverable);
        bluetoothConnect = (Button) findViewById(R.id.buttonConnect);
        //bluetoothConnect = (Button)findViewById(R.id.buttonConnect);
        bluetoothDisconnect = (Button) findViewById(R.id.buttonDisconnect);
        L000 = (Button) findViewById(R.id.buttonL000);
        L007 = (Button) findViewById(R.id.buttonL007);
        listViewDiscovery = (ListView) findViewById(R.id.listViewDiscovery);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public void bluetoothOn(View view) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
            Toast.makeText(getApplicationContext(), "Turned on"
                    , Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Already on",
                    Toast.LENGTH_LONG).show();
        }
    }

    public void bluetoothDiscover(View view) {
        pairedDevices = mBluetoothAdapter.getBondedDevices();

        ArrayList<String> list = new ArrayList<String>();
        for (BluetoothDevice device : pairedDevices)
            list.add(device.getName() + "\n" + device.getAddress());
        Toast.makeText(getApplicationContext(), "Showing Paired Devices",
                Toast.LENGTH_SHORT).show();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listViewDiscovery.setAdapter(adapter);

    }

    private void manageConnectedSocket(BluetoothSocket mmSocket) {
        Toast.makeText(getApplicationContext(), "Manage Connection",
                Toast.LENGTH_SHORT).show();
    }

    public void L007(View view) {
        Toast.makeText(getApplicationContext(), "[L007]",
                Toast.LENGTH_SHORT).show();
    }

    public void L000(View view) {
        Toast.makeText(getApplicationContext(), "[L000]",
                Toast.LENGTH_SHORT).show();
    }

    public void bluetoothConnection(View view) {
        //private final BluetoothSocket mmSocket;
        //private final BluetoothDevice mmDevice;
        //BluetoothConnectThread.start();
        //BluetoothConnectThread confectionTest = new BluetoothConnectThread(mBluetoothAdapter);
        Toast.makeText(getApplicationContext(), "bluetoothConnection()",
                Toast.LENGTH_SHORT).show();
    }

    public void bluetoothDisonnection(View view) {
        Toast.makeText(getApplicationContext(), "bluetoothDisconnection()",
                Toast.LENGTH_SHORT).show();
    }

    // Create a BroadcastReceiver for ACTION_FOUND
    public final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                //ArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                //MainActivity.this.listViewDiscovery.getAdapter().add(device.getName() + "\n" + device.getAddress());
               //zZZZZZZZZZZZZ ((ArrayAdapter) BluetoothDevice.this.listViewDiscovery.getAdapter()).add(device.getName() + "\n" + device.getAddress());
            }
        }
    };

    //???
    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    //registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    ///

    public void bluetoothOff(View view) {
        mBluetoothAdapter.disable();
        Toast.makeText(getApplicationContext(), "Turned off",
                Toast.LENGTH_LONG).show();
    }

    public void bluetoothDiscoverable(View view) {
        Intent getVisible = new Intent(BluetoothAdapter.
                ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(getVisible, 0);

    }

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


    private class BluetoothConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public BluetoothConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
            }
            mmSocket = tmp;
        }

        protected class SwitchMsg {
            private byte sw;
            private byte state;

            public SwitchMsg(byte sw, byte state) {
                this.sw = sw;
                this.state = state;
            }

            public byte getSw() {
                return sw;
            }

            public byte getState() {
                return state;
            }
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            mBluetoothAdapter.cancelDiscovery();

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }

            // Do work to manage the connection (in a separate thread)
            manageConnectedSocket(mmSocket);
        }
        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }
}
/*
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isDeviceConnected()) {
            showControls();
        } else {
            hideControls();
        }
    }
    */

  /*  //    protected abstract boolean isControllerConnected();
    protected boolean isDeviceConnected() {
        return false;
    }

    protected void enableDevice(boolean enable) {
        if (enable) {
            showDevice();
        } else {
            hideDevice();
        }
    }

    protected void hideDevice() {
        if(mBluetoothThread != null) {
            mBluetoothThread.interrupt();
            mBluetoothThread = null;
        }
    }

    protected void showDevice() {
        setContentView(R.layout.main);

        mGameSurface = (SurfaceView) findViewById(R.id.gameSurface);
        mGameThread = new GameThread(this, mGameSurface);
        mGameThread.start();
    }*/

      //  protected void handleSwitchMessage(SwitchMsg o) {
       /* byte sw = o.getSw();
        switch(sw) {
            case 1: //Button B
                if(o.getState() != 0 && mGameThread != null) {
                    mGameThread.togglePlayerColor();
                }
                break;
            case 0: //Button A
                if(o.getState() != 0 && mGameThread != null) {
                    boolean success = mGameThread.attemptPickup();
                    if(success) {
                        Toast.makeText(this, "Awesome Job!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "Sorry.  Try Again!", Toast.LENGTH_SHORT).show();
                    }
                    sendVibeControl(success);
                }
                break;
            default:
                break;ii
        }i*/




Java Source Code List

Android.Arduino.Bluetooth.AndroidArduino.java
Android.Arduino.Bluetooth.AndroidArduino.java
Android.Arduino.Bluetooth.BluetoothTest.java
Android.Arduino.Bluetooth.BluetoothTest.java
Android.Arduino.Bluetooth.BuildConfig.java
beta.ArduinoidBlue.ActivityMain.java
beta.delux.ghostscanner.ActivityMain.java
beta.delux.ghostscanner.FragmentAbout.java
beta.delux.ghostscanner.FragmentBluetooth.java
beta.delux.ghostscanner.FragmentLights.java
beta.delux.ghostscanner.FragmentMotors.java
beta.delux.ghostscanner.FragmentSolenoids.java
beta.delux.ghostscanner.FragmentSwitches.java
beta.delux.ghostscanner.HandlerBluetoothSPP.java
beta.delux.ghostscanner.HandlerBluetooth.java
beta.delux.ghostscanner.SimpleGhost.java
beta.delux.ghostscanner.ThreadUpdate_00.java
.blkuetoothconnect.java
.bluetoothExamplemainactivity.java
com.colecago.santa.AboutInfoActivity.java
com.colecago.santa.AboutInfoActivity.java
com.colecago.santa.BtHelperHandler.java
com.colecago.santa.BtHelperHandler.java
com.colecago.santa.BtSPPHelper.java
com.colecago.santa.BtSPPHelper.java
com.colecago.santa.BuildConfig.java
com.colecago.santa.DeviceListActivity.java
com.colecago.santa.DeviceListActivity.java
com.colecago.santa.MainActivity.java
com.colecago.santa.MainActivity.java
example.redux.bluetoothchat.BluetoothChatService.java
example.redux.bluetoothchat.BluetoothChatService.java
example.redux.bluetoothchat.BluetoothChat.java
example.redux.bluetoothchat.BluetoothChat.java
example.redux.bluetoothchat.DeviceListActivity.java
example.redux.bluetoothchat.DeviceListActivity.java
ghostscanner_00.ghostscanner.GhostScanner_00.java
ghostscanner_00.ghostscanner.MainActivity.java
heck.pinball.ghostscanner.AboutFragment.java
heck.pinball.ghostscanner.BluetoothFragment.java
heck.pinball.ghostscanner.BluetoothHandlerSPP.java
heck.pinball.ghostscanner.BluetoothHandler.java
heck.pinball.ghostscanner.BluetoothService.java
heck.pinball.ghostscanner.DeviceListActivity.java
heck.pinball.ghostscanner.LightsFragment.java
heck.pinball.ghostscanner.MainActivity.java
heck.pinball.ghostscanner.MotorsFragment.java
heck.pinball.ghostscanner.SolenoidsFragment.java
heck.pinball.ghostscanner.SwitchesFragment.java
heck.pinball.simpleghost.simpleghost.BluetoothActivity.java
heck.pinball.simpleghost.simpleghost.BluetoothActivity.java
heck.pinball.simpleghost.simpleghost.MainActivity.java
heck.pinball.simpleghost.simpleghost.MainActivity.java
.mainactivity.java