L'application s'est arrêtée
Bonjour,
J'ai une application Java qui a pour but de manipuler le Bluetooth et de se connecter à un périphérique. J'ai un soucis dans mon code car l'application s'arrête brutalement lors de son exécution sur Android. J'ai aucune erreur après avoir nettoyer le projet ou lors de la création du *.apk.
A noter que cette erreur est apparue peu après avoir rajouter la fonction connect(). Malheureusement même en revenant en arrière, rien à faire l'application s'arrête à son lancement.
Mon code Java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| package com.src.ecom.attest.bt;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.*;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
public class InterfaceActivity extends Activity {
Button connection = null;
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothDevice device = null;// le périphérique (le module bluetooth)
private BluetoothSocket socket = null;
private InputStream receiveStream = null;// Canal de réception
private OutputStream sendStream = null;// Canal d'émission
private boolean flag = false;
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
connection = (Button) findViewById(R.id.ConnectionBouton);
connection.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
flag = Recherche();
blueAdapter.cancelDiscovery();
if(flag == true)
{
//connect();
connect();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_interface, menu);
return true;
}
public boolean Recherche()
{
boolean result = false;
// On récupère la liste des périphériques associés
Set<BluetoothDevice> setpairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
BluetoothDevice[] pairedDevices = (BluetoothDevice[]) setpairedDevices.toArray(new BluetoothDevice[setpairedDevices.size()]);
// On parcours la liste pour trouver notre module bluetooth
for(int i=0;i<pairedDevices.length;i++)
{
// On teste si ce périphérique contient le nom du module bluetooth connecté au microcontrôleur
if(pairedDevices[i].getName().contains("GT-N7000")) {
device = pairedDevices[i];
try {
// On récupère le socket de notre périphérique
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
receiveStream = socket.getInputStream();// Canal de réception (valide uniquement après la connexion)
sendStream = socket.getOutputStream();// Canal d'émission (valide uniquement après la connexion)
Toast.makeText(context, "Périphérique trouvé!", duration).show();
result = true;
} catch (IOException e) {
Toast.makeText(context, "Périphérique non trouvé!", duration).show();
e.printStackTrace();
}
break;
}
}
return result;
}
public void connect() {
new Thread() {
@Override public void run() {
try {
socket.connect();// Tentative de connexion
// Connexion réussie
Toast.makeText(context, "Connexion réussie", duration).show();
} catch (IOException e) {
// Echec de la connexion
Toast.makeText(context,"Connexion Echouée", duration).show();
e.printStackTrace();
}
}
}.start();
}
} |
XML de l'interface
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/ConnectionBouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:layout_marginLeft="16dp"
android:text="Connection" />
<Button
android:id="@+id/EnvoyerBouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ConnectionBouton"
android:layout_alignBottom="@+id/ConnectionBouton"
android:layout_alignParentRight="true"
android:layout_marginRight="27dp"
android:text="Envoyer" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ConnectionBouton"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Liste Périphérique"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/listViewResults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp" >
</ListView>
</RelativeLayout> |
Le manifeste :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.src.ecom.attest.bt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".InterfaceActivity"
android:label="@string/title_activity_interface" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |
Merci d'avance d'avoir lu ce topic :ccool: