0

Getting error while Connecting Bluetooth

getting error when open Bluetooth Device

I am calling Methods Like this

findBT();
try {
    Thread.currentThread();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    openBT();
}

my code for finding pared Bluetooth device

void findBT () {
    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter == null) {
            Toast.makeText(context, "  No bluetooth adapter available  ", Toast.LENGTH_LONG).show();
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
                .getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {

                // MP300 is the name of the bluetooth printer device
                if (device.getName().equals("AB-340M")) {
                    mmDevice = device;
                    break;
                }
            }
        }
        //Toast.makeText(context, "Found", Toast.LENGTH_LONG).show();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

MY java code for Open Bluetooth Device

/*
 * Tries to open a connection to the bluetooth printer device
 */
void openBT ()throws IOException {
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        mmOutputStream = mmSocket.getOutputStream();
        mmInputStream = mmSocket.getInputStream();

        beginListenForData();

        //Toast.makeText(context, "OPEN", Toast.LENGTH_LONG).show();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Getting Error

07-08 13:22:44.710: W/BluetoothAdapter(9120): getBluetoothService() called with no BluetoothManagerCallback
07-08 13:22:44.937: W/System.err(9120): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1016)
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:973)
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:662)
07-08 13:22:44.953: W/System.err(9120):     at com.clppl.project.SendChallan.openBT(SendChallan.java:343)
07-08 13:22:44.953: W/System.err(9120):     at com.clppl.project.SendChallan$ApproveAdapter$2.onClick(SendChallan.java:229)
07-08 13:22:44.953: W/System.err(9120):     at android.view.View.performClick(View.java:4452)
07-08 13:22:44.953: W/System.err(9120):     at android.widget.Button.performClick(Button.java:148)
07-08 13:22:44.953: W/System.err(9120):     at android.view.View$PerformClick.run(View.java:18428)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Handler.handleCallback(Handler.java:725)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Looper.loop(Looper.java:176)
07-08 13:22:44.953: W/System.err(9120):     at android.app.ActivityThread.main(ActivityThread.java:5365)
07-08 13:22:44.953: W/System.err(9120):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:22:44.960: W/System.err(9120):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 13:22:44.960: W/System.err(9120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 13:22:44.960: W/System.err(9120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 13:22:44.960: W/System.err(9120):     at dalvik.system.NativeStart.main(Native Method)

Please Help Me how i can fix this problem

Thanks IN Advance

user3809459
  • 61
  • 1
  • 1
  • 5

1 Answers1

0

I am not sure but check your device Bluetooth version is it version 4.0 (Low Energy). Google has released Bluetooth LE API in Android 4.3

Android
  • 82
  • 1
  • 10