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 mormic.bluetest2;
import android.bluetooth.BluetoothSocket;
import android.support.v7.app.ActionBarActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends ActionBarActivity implements OnItemClickListener {
BluetoothSocket mmSocket = null;
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = ba.getBondedDevices();
BluetoothDevice [] tbDevices = (BluetoothDevice[]) devices.toArray(new BluetoothDevice[devices.size()]);
OutputStream sendStream = null;
Button btlst, btEnvoi;
ListView lv;
TextView msgSelect;
EditText cmd;
ArrayList<String> listItems = new ArrayList<String>();
public String lettre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
btlst = (Button)findViewById(R.id.btListe);
btEnvoi = (Button) findViewById(R.id.envoi);
lv = (ListView)findViewById(R.id.liste);
msgSelect = (TextView)findViewById(R.id.select);
cmd = (EditText)findViewById(R.id.commande);
//Est-ce que le Bluetooth est supporté
if (ba == null)
Toast.makeText(context, "Pas de Bluetooth", Toast.LENGTH_SHORT).show();
else {
//Si oui est-ce que le Bluetooth est en fonction
if (!ba.isEnabled()) {
Toast.makeText(context, "Le Bluetooth est présent mais n'est pas activé", Toast.LENGTH_LONG).show();
//Rend le Bluetooth actif
Intent active = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(active, 0);
}
else
Toast.makeText(context,ba.getName() + "\n" + ba.getAddress(),Toast.LENGTH_LONG).show();
}
}
//affiche une liste des équipements apairés
public void afficheListe(View v){
for (BluetoothDevice bd : devices) {
listItems.add(bd.getName() + "\n" + bd.getAddress());
}
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
//sélectionne un équipement
public void onItemClick (AdapterView<?>adapter, View v, int position, long id) {
// Affichage des données de l'équipement sélectionné dans un textview
msgSelect.setText("Position " + Integer.toString(position) + "\n" + listItems.get(position));
//Crée le socket
try {
mmSocket = tbDevices[position].createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
e.printStackTrace();
}
connect();
}
public void connect() {
new Thread() {
@Override public void run() {
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
}
}.start();
}
public void envoiMessage (View v) {
lettre = this.cmd.getText().toString();
try {
sendStream.write(lettre.getBytes());
sendStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager