NXT / LEGO-MINDSTORMS-MINDdroid

LEGO MINDSTORMS Android Apps
http://mindstorms.com
156 stars 73 forks source link

MINDroid Bluetooth connect exceptions "Ooops ..." #1

Closed MichaelBiermann closed 13 years ago

MichaelBiermann commented 13 years ago

I have downloaded MINDroid from Android Market and was able to pair HTC Desire and NXT. But pairing was only possible when searching HTC Desire on NXT. After pairing I tried to use the app but always got message "Ooops... NXT turned off ...".

I created my own app using some sample code from magazine "Android 360" and ran into the same issue.

The issue is explained on http://code.google.com/p/android/issues/detail?id=5427.

An workaround is described here:

  http://stackoverflow.com/questions/2660968/ 

I have applied it and "connect" worked fine, but I am not sure, if the solution works in general.

Regards Michael


This works fine for me (see line where nxtBTsocketTEMPORARY is filled):

private void createNXTconnection() {

    try {

        BluetoothSocket nxtBTsocketTEMPORARY;

        BluetoothDevice nxtDevice = null;

        nxtDevice = btAdapter.getRemoteDevice(mMACaddress);

        if (nxtDevice == null) {

            sendToast(myMINDdroid.getResources().getString(R.string.no_paired_nxt));

            sendState(STATE_CONNECTERROR);

            return;

        }

        try{

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

        nxtBTsocketTEMPORARY = (BluetoothSocket)m.invoke(nxtDevice, Integer.valueOf(1));            

// nxtBTsocketTEMPORARY = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);

        }

        catch (NoSuchMethodException e){   

            sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

            sendState(STATE_CONNECTERROR);

        return;

        }

        catch (InvocationTargetException e){            

            sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

            sendState(STATE_CONNECTERROR);

        return;

        }

        catch (IllegalAccessException e){            

            sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

            sendState(STATE_CONNECTERROR);

        return;

        }

        nxtBTsocketTEMPORARY.connect();

        nxtBTsocket = nxtBTsocketTEMPORARY;

        nxtDin = new DataInputStream(nxtBTsocket.getInputStream());

        nxtDos = new DataOutputStream(nxtBTsocket.getOutputStream());

        connected = true;

    } catch (IOException e) {

        //Log.d("BTCommunicator", "error createNXTConnection()", e);

        if (myMINDdroid.pairing) {

            sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

            sendState(STATE_CONNECTERROR);

        } else {

            sendState(STATE_CONNECTERROR);

        }

        return;

    }

    sendState(STATE_CONNECTED);

}
MichaelBiermann commented 13 years ago

The exception can be caught (IOException) and the solution can be applied to the catch block only.

The code below works fine for HTC Desire (Abdroid 2.2).

Moreover I have compared the two instances of nxtBTsocketTEMPORARY.

Regards Michael


private static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


nxtBTsocketTEMPORARY = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);

nxtBTsocketTEMPORARY BluetoothSocket (id=830104047616)
mAddress "00:16:53:0A:3D:A0" (id=830104002304)
mAuth true
mClosed false
mDevice BluetoothDevice (id=830104040368)
mEncrypt true
mInputStream BluetoothInputStream (id=830104048544) mLock ReentrantReadWriteLock (id=830104049080)
mOutputStream BluetoothOutputStream (id=830104048728)
mPort -1
mSdp BluetoothSocket$SdpHelper (id=830104048328)
mSocketData 2647808 mType 1

nxtBTsocketTEMPORARY.connect(); -> java.io.IOException: Unable to start Service Discovery

Method m = nxtDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); nxtBTsocketTEMPORARY = (BluetoothSocket)m.invoke(nxtDevice, Integer.valueOf(1));

nxtBTsocketTEMPORARY BluetoothSocket (id=830104070168)
mAddress "00:16:53:0A:3D:A0" (id=830104002304)
mAuth true
mClosed false
mDevice BluetoothDevice (id=830104040368)
mEncrypt true
mInputStream BluetoothInputStream (id=830104070232) mLock ReentrantReadWriteLock (id=830104070264)
mOutputStream BluetoothOutputStream (id=830104070248)
mPort 1
mSdp null
mSocketData 2823048 mType 1

nxtBTsocketTEMPORARY.connect(); -> works fine


private void createNXTconnection() {

    try {

        BluetoothSocket nxtBTsocketTEMPORARY;

        BluetoothDevice nxtDevice = null;

        nxtDevice = btAdapter.getRemoteDevice(mMACaddress);

        if (nxtDevice == null) {

            sendToast(myMINDdroid.getResources().getString(R.string.no_paired_nxt));

            sendState(STATE_CONNECTERROR);

            return;

        }

        nxtBTsocketTEMPORARY = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);

        try{

          nxtBTsocketTEMPORARY.connect();

        }  

        catch (IOException e){            

            try{

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

               nxtBTsocketTEMPORARY = (BluetoothSocket)m.invoke(nxtDevice, Integer.valueOf(1));            

               nxtBTsocketTEMPORARY.connect();

            }

            catch (NoSuchMethodException e1){   

                sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

                sendState(STATE_CONNECTERROR);

            return;

            }

            catch (InvocationTargetException e1){            

                sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

                sendState(STATE_CONNECTERROR);

            return;

            }

            catch (IllegalAccessException e1){            

                sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

                sendState(STATE_CONNECTERROR);

            return;

            }

        }

        nxtBTsocket = nxtBTsocketTEMPORARY;

        nxtDin = new DataInputStream(nxtBTsocket.getInputStream());

        nxtDos = new DataOutputStream(nxtBTsocket.getOutputStream());

        connected = true;

    } catch (IOException e) {

        //Log.d("BTCommunicator", "error createNXTConnection()", e);

        if (myMINDdroid.pairing) {

            sendToast(myMINDdroid.getResources().getString(R.string.pairing_message));

            sendState(STATE_CONNECTERROR);

        } else {

            sendState(STATE_CONNECTERROR);

        }

        return;

    }

    sendState(STATE_CONNECTED);

}
vvagr commented 13 years ago

Can you publish the corrected app somewhere - just for Lego and Desire fans with too limited experience with Eclipse :-(