Closed MichaelBiermann closed 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);
}
Can you publish the corrected app somewhere - just for Lego and Desire fans with too limited experience with Eclipse :-(
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:
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() {
// nxtBTsocketTEMPORARY = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);