Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.os.Build;
import android.util.Log;

public class Main {
    private static final String SERIAL_SERVICE_UUID = "00001101-0000-1000-8000-00805f9b34fb";

    public static BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
        UUID uuid = UUID.fromString(SERIAL_SERVICE_UUID);
        BluetoothSocket socket = null;
        if (Build.VERSION.SDK_INT >= 10) {
            try {
                final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord",
                        new Class[] { UUID.class });
                System.out.println("Performing invoke");
                socket = (BluetoothSocket) m.invoke(device, uuid);
                System.out.println("Invoke done");
                System.out.println(socket);
            } catch (Exception e) {
                Log.e("error", "Could not create Insecure RFComm Connection", e);
            }
        } else {
            //TODO, support older versions with secure connection
        }
        return socket;
    }
}