2015-09-03 2 views
0

Je développe une application qui peut se connecter à un module Bluetooth (HC06 dans mon cas). Dans cette application, je veux envoyer une commande pour allumer 2 leds sur ma carte MSP430. J'ai réussi à me connecter au module bluetooth et à dire une commande ("on" par exemple). après une commande, l'application a des erreurs et des sorties. Note: si je dis "on" je veux envoyer la lettre "A", "off" lettre "B" et ainsi de suite.Erreur de fermeture de socket après une commande dans l'application de reconnaissance vocale et Bluetooth

Je semble avoir un problème avec la fermeture du socket, et c'est pourquoi je ne peux pas dire une seconde commande. Voici mon code:

public class VoiceRecognitionDemo extends Activity 
{ 

private static final int REQUEST_CODE = 1234; 
private static final String TAG = "SuperAPPLICATIE"; 
private BluetoothAdapter btAdapter = null; 
private BluetoothSocket btSocket = null; 
private OutputStream outStream = null; 
private static final UUID MY_UUID = 
     UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
// Insert your bluetooth devices MAC address 
private static final String address = "98:D3:31:30:3C:A5"; 

/** 
* Called with the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.voice_recog); 

    Button speakButton = (Button) findViewById(R.id.speakButton); 

    //wordsList = (ListView) findViewById(R.id.list); 

    // Disable button if no recognition service is present 
    PackageManager pm = getPackageManager(); 
    List<ResolveInfo> activities = pm.queryIntentActivities(
      new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (activities.size() == 0) 
    { 
     speakButton.setEnabled(false); 
     speakButton.setText("Recognizer not present"); 
    } 
} 

/** 
* Handle the action of the button being clicked 
*/ 
public void speakButtonClicked(View v) 
{ 
    startVoiceRecognitionActivity(); 

} 

/** 
* Fire an intent to start the voice recognition activity. 
*/ 
private void startVoiceRecognitionActivity() 
{ 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo..."); 
    startActivityForResult(intent, REQUEST_CODE); 
} 


public void onResume() { 
    super.onResume(); 

    Log.d(TAG, "...In onResume - Attempting client connect..."); 

    // Set up a pointer to the remote node using it's address. 
    BluetoothDevice device = btAdapter.getRemoteDevice(address); 

    // Two things are needed to make a connection: 
    // A MAC address, which we got above. 
    // A Service ID or UUID. In this case we are using the 
    //  UUID for SPP. 
    try { 
     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); 
    } 

    // Discovery is resource intensive. Make sure it isn't going on 
    // when you attempt to connect and pass your message. 
    btAdapter.cancelDiscovery(); 

    // Establish the connection. This will block until it connects. 
    Log.d(TAG, "...Connecting to Remote..."); 
    try { 
     btSocket.connect(); 
     Log.d(TAG, "...Connection established and data link opened..."); 
    } catch (IOException e) { 
     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
      errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
     } 
    } 

    // Create a data stream so we can talk to server. 
    Log.d(TAG, "...Creating Socket..."); 

    try { 
     outStream = btSocket.getOutputStream(); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + "."); 
    } 
} 

@Override 
public void onPause() { 
    super.onPause(); 

    Log.d(TAG, "...In onPause()..."); 

    if (outStream != null) { 
     try { 
      outStream.flush(); 
     } catch (IOException e) { 
      errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + "."); 
     } 
    } 

    try  { 
     btSocket.close(); 
    } catch (IOException e2) { 
     errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); 
    } 
} 

/** 
* Handle the results from the voice recognition activity. 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) 
    { 
     // Populate the wordsList with the String values the recognition engine thought it heard 
     ArrayList<String> matches = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 
     //wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
     //  matches)); 
     String resulttext = matches.get(0); 
     // wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
     // Integer.parseInt(matches.get(0)))); 

     Toast toast = Toast.makeText(getApplicationContext(), resulttext, Toast.LENGTH_LONG); 
     toast.show(); 
     String letter; 
     switch (resulttext.toLowerCase()) { 
      case "on": 
       letter = "A"; 
       break; 
      case "off": 
       letter = "B"; 
       break; 
      case "flash": 
       letter = "F"; 
       break; 
      default: 
       letter = "X"; 
       break; 
     } 
     sendData(letter.toUpperCase()); 

    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
private void sendData(String message) { 
    byte[] msgBuffer = message.getBytes(); 

    Log.d(TAG, "...Sending data: " + message + "..."); 
    Log.d(TAG, "...In onResume - Attempting client connect..."); 

    // Set up a pointer to the remote node using it's address. 
    BluetoothDevice device = btAdapter.getRemoteDevice(address); 

    // Two things are needed to make a connection: 
    // A MAC address, which we got above. 
    // A Service ID or UUID. In this case we are using the 
    //  UUID for SPP. 
    try { 
     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); 
    } 

    // Discovery is resource intensive. Make sure it isn't going on 
    // when you attempt to connect and pass your message. 
    btAdapter.cancelDiscovery(); 

    // Establish the connection. This will block until it connects. 
    Log.d(TAG, "...Connecting to Remote..."); 
    try { 
     btSocket.connect(); 
     Log.d(TAG, "...Connection established and data link opened..."); 
    } catch (IOException e) { 
     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
      errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
     } 
    } 

    // Create a data stream so we can talk to server. 
    Log.d(TAG, "...Creating Socket..."); 

    try { 
     outStream = btSocket.getOutputStream(); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + "."); 
    } 
    try { 
     outStream.write(msgBuffer); 
    } catch (IOException e) { 
     String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); 
     if (address.equals("00:00:00:00:00:00")) 
      msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code"; 
     msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; 

     errorExit("Fatal Error", msg); 
    } 
    Log.d(TAG, "...In onPause()..."); 

    if (outStream != null) { 
     try { 
      outStream.flush(); 
     } catch (IOException e) { 
      errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + "."); 
     } 
    } 

    try  { 
     btSocket.close(); 
    } catch (IOException e2) { 
     errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); 
    } 
} 

private void errorExit(String title, String message){ 
    Toast msg = Toast.makeText(getBaseContext(), 
      title + " - " + message, Toast.LENGTH_SHORT); 
    msg.show(); 
    finish(); 
} 

}

Répondre

2

sockets ne se contentent pas de se fermer spontanément. SocketException: Socket closed signifie que fermé le socket, puis a continué à utiliser son. C'est un bug dans votre code.

Votre code est très mal structuré. Toutes ces chaînes de try/catch bloque. Le code qui dépend du succès d'un bloc try antérieur doit se trouver dans ce bloc. N'écris pas de code comme ça.