0

I have setup a bluetooth connection and all works fine unless the target receiver is turned off. findBT() is set to detect the paired device name but always sets isBTConnected=true even when the device is turned off. pairedDevices.size() should also be 0 since I dont have any other devices within range and this should also set isBTConnected =false. I am at a loss of where to proceed to detect NO Bluetooth available.

        void findBT() {
    mmDevice = null;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        msbox("BlueTooth ", "This device does not support bluetooth");
        isBTConnected = false;

    } else {

        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) {
            // if(device.getName().equals("RN42-45A9"))
            if (device.getName().equals("RN42-45A9")) {
                Log.d("TAG", "BT true");
                mmDevice = device;
                isBTConnected = true;


            } else {

                isBTConnected = false;
                Log.d("TAG", "BT false");
                msbox("BlueTooth ", "You must pair device");
            }

        }
    } else {
        isBTConnected = false;
        msbox("BlueTooth ",
                "You must pair this device with the Mobil-Link receiver before you can run application. Go to settings> Bluetooth> Pairing or see intruction manual for "
                        + "for pairing device");
    }
}

void openBT() throws IOException {
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard

    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();

}

I changed the openBT()

        void openBT() throws IOException {

    if (mmDevice==null){
        isBTConnected = false;
        Log.d("TAG", "BT NULL");
        return;
    }


    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard

    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();

}

Logcat

03-22 22:02:35.563: D/TAG(11971): CON  true
03-22 22:02:35.563: W/BluetoothAdapter(11971): getBluetoothService() called with no BluetoothManagerCallback
03-22 22:02:35.563: D/BluetoothSocket(11971): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[41]}
03-22 22:02:40.719: W/System.err(11971): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:504)
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:515)
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:319)
03-22 22:02:40.729: W/System.err(11971):    at com.example.simplergb.SimpleRGB_Main.openBT(SimpleRGB_Main.java:3075)
03-22 22:02:40.729: W/System.err(11971):    at com.example.simplergb.SimpleRGB_Main.onCreate(SimpleRGB_Main.java:262)
03-22 22:02:40.729: W/System.err(11971):    at android.app.Activity.performCreate(Activity.java:5133)
03-22 22:02:40.729: W/System.err(11971):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-22 22:02:40.729: W/System.err(11971):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 22:02:40.729: W/System.err(11971):    at android.os.Looper.loop(Looper.java:137)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 22:02:40.729: W/System.err(11971):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 22:02:40.729: W/System.err(11971):    at java.lang.reflect.Method.invoke(Method.java:525)
03-22 22:02:40.729: W/System.err(11971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 22:02:40.729: W/System.err(11971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 22:02:40.729: W/System.err(11971):    at dalvik.system.NativeStart.main(Native Method)
03-22 22:02:40.819: D/AndroidRuntime(11971): Shutting down VM
03-22 22:02:40.819: W/dalvikvm(11971): threadid=1: thread exiting with uncaught exception (group=0x41a95700)
03-22 22:02:40.829: E/AndroidRuntime(11971): FATAL EXCEPTION: main
03-22 22:02:40.829: E/AndroidRuntime(11971): java.lang.RuntimeException: Unable to resume activity {com.example.simplergb/com.example.simplergb.SimpleRGB_Main}: java.lang.NullPointerException
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.os.Looper.loop(Looper.java:137)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at java.lang.reflect.Method.invoke(Method.java:525)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at dalvik.system.NativeStart.main(Native Method)
03-22 22:02:40.829: E/AndroidRuntime(11971): Caused by: java.lang.NullPointerException
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.example.simplergb.SimpleRGB_Main.onResume(SimpleRGB_Main.java:1364)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.Activity.performResume(Activity.java:5211)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
03-22 22:02:40.829: E/AndroidRuntime(11971):    ... 12 more
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
Bobby
  • 659
  • 2
  • 13
  • 26

1 Answers1

0

Just cancel the operation if the mBluetoothAdapter is null; I've added return:

isBTConnected = false;
return; // just cancel the operation 

    void findBT() {
    mmDevice = null;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        msbox("BlueTooth ", "This device does not support bluetooth");
        isBTConnected = false;
        return; // just cancel the operation 
    } else {

        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) {
            // if(device.getName().equals("RN42-45A9"))
            if (device.getName().equals("RN42-45A9")) {
                Log.d("TAG", "BT true");
                mmDevice = device;
                isBTConnected = true;


            } else {

                isBTConnected = false;
                Log.d("TAG", "BT false");
                msbox("BlueTooth ", "You must pair device");
            }

        }
    } else {
        isBTConnected = false;
        msbox("BlueTooth ",
                "You must pair this device with the Mobil-Link receiver before you can run application. Go to settings> Bluetooth> Pairing or see intruction manual for "
                        + "for pairing device");
    }
}

void openBT() throws IOException {

    //cancel if device is null
    if(mmDevice==null){
     retrurn;
    }

    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard

    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();

}
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • Still a crash. The findBT() seems to look only at the android device and not at the target receiver. is there a way to test for connection inside the openBT() ? – Bobby Mar 23 '14 at 01:59
  • check ` //cancel if device is null if(mmDevice==null){ retrurn; }` – Mohammad Ersan Mar 23 '14 at 02:02
  • still crashes, mmDevice will not be null it gets the name from paired devices from the android device. I need to detect if the receiver is valid. Not sure how to test for that. I made changes as per your suggestion see edits. – Bobby Mar 23 '14 at 02:18
  • See attached logcat. I was not sure how to format for easy reading – Bobby Mar 23 '14 at 23:47
  • Anyone have any ideas on the crash. – Bobby Mar 29 '14 at 18:31
  • @Bobby because you are not connected to the other device (check your error `Read Time Out`) – Mohammad Ersan Mar 29 '14 at 18:45
  • If device is not detected the mmDvice should == null correct ? This is the check I do on openBT(). – Bobby Mar 29 '14 at 20:35
  • maybe the problem the 2 devices are not paired – Mohammad Ersan Mar 30 '14 at 00:04
  • Devices are paired in the findbt(); and confirmed in device settings – Bobby Mar 30 '14 at 00:45
  • Found some other post that help with the discovery issue. http://stackoverflow.com/questions/10560319/bluetooth-device-discovery-in-android-startdiscovery. Thank you MoshErsan your comments assisted in searching for the discovery option. – Bobby Mar 31 '14 at 22:31