get Rfcomm Socket - Android android.bluetooth

Android examples for android.bluetooth:Bluetooth

Description

get Rfcomm Socket

Demo Code

import java.lang.reflect.Method;
import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Build;

public class Main {

  static final UUID BLUETOOTH_SPP_PROFILE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  static synchronized BluetoothSocket getRfcommSocket(BluetoothDevice device, int numberOfRetries) {
    BluetoothSocket socket = null;/*from w  ww  .  ja  v a 2s  .c om*/
    numberOfRetries = numberOfRetries % 3;
    switch (numberOfRetries) {
    case 0:
      try {
        if (Build.VERSION.SDK_INT < 10) {
          socket = getInsecureRfcommSocketByReflection(device);
        } else {
          socket = device.createInsecureRfcommSocketToServiceRecord(BLUETOOTH_SPP_PROFILE);
        }
        break;
      } catch (Exception e) {
      }

    case 1:
      try {
        socket = device.createRfcommSocketToServiceRecord(BLUETOOTH_SPP_PROFILE);
        break;
      } catch (Exception e) {
      }

    case 2:
      try {
        socket = getRfcommSocketByReflection(device);
      } catch (Exception e) {
      }
      break;
    }
    return socket;
  }

  static synchronized BluetoothSocket getRfcommSocket(BluetoothDevice device) {
    return getRfcommSocket(device, 0);
  }

  private static synchronized BluetoothSocket getInsecureRfcommSocketByReflection(BluetoothDevice device)
      throws Exception {
    if (device == null)
      return null;
    Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });

    return (BluetoothSocket) m.invoke(device, 1);
  }

  private static synchronized BluetoothSocket getRfcommSocketByReflection(BluetoothDevice device) throws Exception {
    if (device == null)
      return null;
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });

    return (BluetoothSocket) m.invoke(device, 1);
  }

}

Related Tutorials