The Great
The Great

Reputation: 61

Android bluetooth socket connection not created

I am creating an application which can send data to a Bluetooth device. I used the following code to create and connect socket:

package com.example.bluetooth;

import java.io.IOException;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice bd = ba.getRemoteDevice("20:13:10:15:39:84");
        Toast.makeText(getApplicationContext(), bd.getName(), Toast.LENGTH_SHORT).show();
        BluetoothSocket bs = null;
        try{
            bs = bd.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }
        catch(IOException io){
            Toast.makeText(getApplicationContext(), "Socket Create : " + io.toString() , Toast.LENGTH_SHORT).show();
        }
        try{
            ba.cancelDiscovery();
            bs.connect();
        }
        catch(IOException io){
            Log.e("Socket Connect", io.toString());
            Toast.makeText(getApplicationContext(), "Socket Connect : " + io.toString() , Toast.LENGTH_LONG).show();
        }
    }

}

My problem is that socket is not being connected. The message displayed is "java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted)."

I am using android 4.2 Lenovo Device.

The Bluetooth Module used is HC-05 and microcontroller is Arduino-Uno.

I have have referred to similar posts but none could solve my problem.

Thanks in Advance

Upvotes: 1

Views: 10195

Answers (3)

droidd
droidd

Reputation: 1371

I have used this piece of code to make my connectivity to Bluetooth device(i.e. Bluetooth printer) stable. Now it connect 9.9 times out of 10. If still some error occurred i reset my Bluetooth programmatically again call this piece of code then it connect 10 times out of 10.

public boolean connectToPrinter(String printerName) throws IOException 
    {
        BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

        BluetoothDevice device = getPrinterByName(printerName);

        if (bluetoothSocket != null) 
        {
            bluetoothSocket.close();
        }


        try {

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

        } catch (Exception e) {
            e.printStackTrace();
        }


        if (bluetoothSocket == null)
            return false;

        bluetoothSocket.connect();

        return true;
    }

here is the code of getPrinterByName():

private BluetoothDevice getPrinterByName(String printerName) 
    {
        Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

        for (BluetoothDevice device : pairedDevices) 
        {

            Log.e("","device name: "+device.getName());

            if (device.getName() == null)
                continue;
            if (device.getName().contains(printerName)) 
            {
                remoteDevice = device;
                //              pairPrinter(printerName);
                return remoteDevice;
            }
        }
        return null;
    }

Upvotes: 4

The Great
The Great

Reputation: 61

I first restarted my tablet and then tried to connect Bluetooth Socket using my code. I just had to use:

public void onStop(){
    super.onStop();
    mSocket.close();
    mOutputStream.close();
 }

at the end.

It worked!

The problem was that I never tried to close socket at end.

Upvotes: 3

Brinda K
Brinda K

Reputation: 765

Pair the arduino bluetooth from your phone settings first. Then try to connect to the bluetooth from your app. Try below code:

    private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mBluetoothDevice;
private BluetoothSocket mBluetoothSocket;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    try{

        if(mBluetoothAdapter == null){
            Log.d("bluetooth:", "device does not support bluetooth");
        }
        if(!mBluetoothAdapter.isEnabled()){
            Intent enableBt = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            enableBt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(enableBt);
        }

    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

For connection :

   mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("xx:xx:xx:xx:xx:xx");
   try {
    mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
    mBluetoothSocket.connect();
    } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
    }

Upvotes: 0

Related Questions