Bluetooth Connector for Android

Hi, I am trying to create an Android application that will connect to the Blue SMiRF Bluetooth dongle to which I want to send data. I read the developer pages and looked at several different examples, but I am currently having problems creating a socket connection. Part of the Bluetooth code pretty much refers to the example I could find. When I try to connect to a Bluetooth dongle, the application receives power because there is some error that I am not handling correctly. However, I also tried to use the application to connect to another PC, and for some reason the connection would not be established correctly, although I already connected to the device through the Bluetooth settings before I even started the application. I posted some of the most important codes below, where I thinkwhat my problem may be. Any help would be greatly appreciated, please let me know if I write any additional code.
protected void connect(BluetoothDevice device) {
    //BluetoothSocket socket = null;
    try {
        //Create a Socket connection: need the server UUID number of registered

        socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));



        socket.connect();
        Log.d("EF-BTBee", ">>Client connectted");

        InputStream inputStream = socket.getInputStream();                                                      
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });


        new Thread() {
            public void run() {
                    while(true)
                    {   
                    try {
                        Log.d("EF-BTBee", ">>Send data thread!");
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(new byte[] { (byte) 0xa2, 0, 7, 16, 0, 4, 0 });
                    } catch (IOException e) {
                        Log.e("EF-BTBee", "", e);
                    }
                    }
            };
        }.start();

    } catch (IOException e) {
        Log.e("EF-BTBee", "", e);
    } finally {
        if (socket != null) {
            try {
                Log.d("EF-BTBee", ">>Client Close");
                socket.close(); 
                finish();
                return ;
            } catch (IOException e) {
                Log.e("EF-BTBee", "", e);
            }
        }
    }
}`

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

        socket = (BluetoothSocket) m.invoke(device, 1);

"socket =" .

+4
3

,

if(device.getBondState()==device.BOND_BONDED){
    Log.d(TAG,device.getName());
    //BluetoothSocket mSocket=null;
    try {
        mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.d(TAG,"socket not created");
        e1.printStackTrace();
    }
    try{
        mSocket.connect();
    }
    catch(IOException e){
        try {
            mSocket.close();
            Log.d(TAG,"Cannot connect");
        } catch (IOException e1) {
            Log.d(TAG,"Socket not closed");
            e1.printStackTrace();
        }
    }

MY_UUID

private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
+2

(!) UUID, , UUID , Bluetooth. .

, , createRfcommSocketToServiceRecord createInsecureRfcommSocketToServiceRecord "createRfcommSocket" "CreateInsecureRfcommSocket".

: BTWiz, . BTWiz API Bluetooth IO.

, () ion . , , .

www.mobileedge.co.il

0

All Articles