Makes a call to createSocket() upon success of creating the BluetoothDevice. - Android Bluetooth

Android examples for Bluetooth:Bluetooth Device

Description

Makes a call to createSocket() upon success of creating the BluetoothDevice.

Demo Code



//package com.java2s;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

public class Main {
  public static final int SETUP_SUCCESSFULL = 1;
  public static final int PAIR_DEVICE_REQUEST = 10;
  public static final int CONNECTION_FAILURE = 11;
  private static final String DEVICE_NAME = "Open Sesame v1.0";
  public static BluetoothAdapter mBlueToothAdapter;
  public static BluetoothSocket mBluetoothSocket = null;
  public static BluetoothDevice mBluetoothDevice;
  public static OutputStream mOutputStream = null;

  public static int setup() {
    mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothDevice == null && mBlueToothAdapter.isEnabled()) {
      try {/*from w  ww. java  2 s .c  o  m*/
        mBluetoothDevice = getDeviceByName(mBlueToothAdapter, DEVICE_NAME);
        if (mBluetoothDevice != null) {
          if (createSocketOK())
            ;
          return SETUP_SUCCESSFULL;
        } else {
          return PAIR_DEVICE_REQUEST;
        }
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return CONNECTION_FAILURE;
      }
    }
    return 0;
  }

  private static BluetoothDevice getDeviceByName(BluetoothAdapter adapter, String name) {
    for (BluetoothDevice device : getBondedDevices(adapter)) {
      String thisDevice = device.getName();
      if (name.matches(thisDevice)) {
        return device;
      }
    }
    return null;
  }

  public static boolean createSocketOK() {
    try {
      if (mBluetoothDevice != null) {
        Method createRfcommSocket = mBluetoothDevice.getClass().getMethod("createRfcommSocket", int.class);
        mBluetoothSocket = (BluetoothSocket) createRfcommSocket.invoke(mBluetoothDevice, 1);
        return true;
      } else {
        teardown();
        setup();
      }
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
      e.printStackTrace();
    }
    return false;
  }

  private static Set<BluetoothDevice> getBondedDevices(BluetoothAdapter adapter) {
    Set<BluetoothDevice> results = adapter.getBondedDevices();
    if (results == null) {
      results = new HashSet<BluetoothDevice>();
    }
    return results;
  }

  public static void teardown() {
    try {
      if (mBluetoothSocket != null) {
        mBluetoothSocket.close();
      }
      if (mOutputStream != null) {
        mOutputStream.flush();
        mOutputStream.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Related Tutorials