Gets hidden method createRfcommSocket() from BluetoothDevice then calls it to return the BluetoothSocket needed to connect to. - Android Bluetooth

Android examples for Bluetooth:Bluetooth Device

Description

Gets hidden method createRfcommSocket() from BluetoothDevice then calls it to return the BluetoothSocket needed to connect to.

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 boolean createSocketOK() {
    try {/*from ww  w.j a  va 2s. c o  m*/
      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;
  }

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

  public static int setup() {
    mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothDevice == null && mBlueToothAdapter.isEnabled()) {
      try {
        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;
  }

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

Related Tutorials