get Bluetooth Rfcomm Socket - Android Bluetooth

Android examples for Bluetooth:Bluetooth Data Transfer

Description

get Bluetooth Rfcomm Socket

Demo Code

/*/*from  w  ww.ja  va  2  s.co  m*/
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * version 3 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * Please contact Integreight, Inc. at info@integreight.com or post on our
 * support forums www.1sheeld.com/forum if you need additional information
 * or have any questions.
 */
//package com.java2s;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothSocket;
import android.os.Build;
import java.lang.reflect.Method;
import java.util.UUID;

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;
        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