Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.PollClient;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.ViewFlipper;
- import java.io.*;
- import java.util.UUID;
- /**
- * Created with IntelliJ IDEA.
- * User: Sean
- * Date: 9/7/13
- * Time: 2:49 PM
- * To change this template use File | Settings | File Templates.
- */
- public class Connection extends Activity {
- private static final String DEBUG = "DEBUG_LOG";
- private static final int REQUEST_ENABLE_BT = 1;
- private static final int GET_MESSAGE = 0;
- private static final int SEND_MESSAGE = 1;
- private int CUR_STATE;
- private BluetoothAdapter btAdapter = null;
- private BluetoothSocket btSocket = null;
- private Intent intent;
- private InputStream inStream = null;
- private BufferedReader bReader = null;
- private OutputStream outputStream = null;
- private PrintWriter printWriter = null;
- private EditText macAddress;
- private Button macButton;
- private Button connectButton;
- // Well known SPP UUID
- private static final UUID MY_UUID =
- UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- // // Insert your server's MAC address
- private static String address = "78:DD:08:A3:F3:3F";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.connect);
- CUR_STATE = GET_MESSAGE;
- Log.d(DEBUG,"\n...Connect In onCreate()...");
- btAdapter = BluetoothAdapter.getDefaultAdapter();
- CheckBTState();
- macAddress = (EditText) findViewById(R.id.editText);
- macAddress.setText(address);
- macButton = (Button) findViewById(R.id.saveMACButton);
- macButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- address = macAddress.getText().toString();
- }
- });
- connectButton = (Button) findViewById(R.id.connectButton);
- connectButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try{
- connectClient();
- } catch (IOException e){
- e.printStackTrace();
- }
- }
- });
- }
- private void CheckBTState() {
- // Check for Bluetooth support and then check to make sure it is turned on
- // Emulator doesn't support Bluetooth and will return null
- if(btAdapter==null) {
- AlertBox("Fatal Error", "Bluetooth Not supported. Aborting.");
- } else {
- if (btAdapter.isEnabled()) {
- Log.d(DEBUG, "\n...Bluetooth is enabled...");
- } else {
- //Prompt user to turn on Bluetooth
- Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- }
- }
- public void AlertBox( String title, String message ){
- new AlertDialog.Builder(this)
- .setTitle( title )
- .setMessage( message + " Press OK to exit." )
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface arg0, int arg1) {
- finish();
- }
- }).show();
- }
- private void connectClient() throws IOException {
- //Get the String JSON Message and store it for the poll activity
- Log.d(DEBUG, "\n...Attempting client connect...");
- // Hard coded connection for now
- // Set up a pointer to the remote node using it's address.
- BluetoothDevice device = btAdapter.getRemoteDevice(address);
- // Two things are needed to make a connection:
- // A MAC address, which we got above.
- // A Service ID or UUID. In this case we are using the
- // UUID for SPP.
- try {
- btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
- }
- // Discovery is resource intensive. Make sure it isn't going on
- // when you attempt to connect and pass your message.
- btAdapter.cancelDiscovery();
- // Establish the connection. This will block until it connects.
- try {
- btSocket.connect();
- Log.d(DEBUG, "\n...Connection established and data link opened...");
- } catch (IOException e) {
- try {
- btSocket.close();
- } catch (IOException e2) {
- AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
- }
- }
- getMessage();
- intent = new Intent(Connection.this, PollTaker.class);
- startActivity(intent);
- }
- private void getMessage() throws IOException{
- Log.d(DEBUG, "\n Getting Message");
- try{
- inStream = btSocket.getInputStream();
- bReader = new BufferedReader(new InputStreamReader(inStream));
- String message = bReader.readLine();
- CUR_STATE = SEND_MESSAGE;
- //Get the String JSON Message and store it for the poll activity
- } catch (IOException e){
- AlertBox("Fatal Error", "In onResume() and input stream creation failed:" + e.getMessage() + ".");
- }
- }
- private void sendMessage(){
- Log.d(DEBUG, "\n Sending Message");
- try {
- outputStream = btSocket.getOutputStream();
- printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
- //Get JSON Response object, push
- CUR_STATE = GET_MESSAGE;
- } catch (IOException e) {
- AlertBox("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement