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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| package com.example.sevice_bluetooth;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BtClass extends Activity{
private final BluetoothAdapter blueAdapter;
// private final MyBroadcastReceiver blueReceiver;
protected Set<BluetoothDevice> mArrayAdapter;
Set<BluetoothDevice> setpairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
BluetoothDevice[] pairedDevices = (BluetoothDevice[]) setpairedDevices.toArray(new BluetoothDevice[setpairedDevices.size()]);
private BluetoothSocket socket = null;
private BluetoothDevice device = null;// le périphérique (le module bluetooth)
private InputStream receiveStream = null;// Canal de réception
private OutputStream sendStream = null;// Canal d'émission
boolean stopWorker = false;
String Message;
public BtClass()
{
blueAdapter = BluetoothAdapter.getDefaultAdapter();
}
public String GetEtat()
{
String returnedString = "";
if (blueAdapter.isEnabled()) {
returnedString = "Bluetooth enable";
}
else
{
returnedString = "Bluetooth disable";
}
return returnedString;
}
public String ActivateBlue()
{
String returnedString = "";
if (!blueAdapter.isEnabled()) {
try {
blueAdapter.enable();
returnedString = "Le Bluetooth est activé";
}
catch(Exception e)
{
returnedString = "Erreur : " + e.toString();
}
}
return returnedString;
}
public String DisactivateBlue(){
String returnedString = "";
if (blueAdapter.isEnabled()) {
try {
blueAdapter.disable();
returnedString = "Le Bluetooth est desactivé";
}
catch(Exception e)
{
returnedString = "Erreur : " + e.toString();
}
}
return returnedString;
}
public Set<BluetoothDevice> KnownDevices()
{
Set<BluetoothDevice> pairedDevices = blueAdapter.getBondedDevices();
return pairedDevices;
}
public void ScanDevices(Context context)
{
try{
Toast.makeText(BtClass.this, "demarage de la decouverte ", Toast.LENGTH_SHORT).show();
//blueAdapter.startDiscovery();
}catch (Exception e) {
Toast.makeText(BtClass.this, "erreur : " + e, Toast.LENGTH_SHORT).show();
}
//return pairedDevices;
}
protected void onDestroy() {
super.onDestroy();
blueAdapter.cancelDiscovery();
// unregisterReceiver(blueReceiver);
}
public boolean ConnectDevice(ListView liste, int myItemInt, TextView reception)
{
reception.append("\n wait...");
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
Log.d("debug", "Boucle des paires devices");
String selectedFromList =(String) (liste.getItemAtPosition(myItemInt));
if(pairedDevices[i].getName().contains(selectedFromList)) {
Log.d("debug", selectedFromList+" find");
device = pairedDevices[i];
try {
String ADR=device.getAddress();
// On récupère le socket de notre périphérique
Log.d("debug","adresse device : "+ ADR);
BluetoothDevice device = blueAdapter.getRemoteDevice(ADR);
Log.d("debug", "Connecting to ... " + device);
reception.setText("Connecting to ... " + device);
blueAdapter.cancelDiscovery();
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
Log.d("debug", "peut etre");
reception.append("\n wait...");
socket.connect();
Log.d("debug", "connecté !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
reception.setText("Device Connecte \n Wait... ");
beginListenForData(reception);
Log.d("debug", "receiverThread.start();");
return true;
} catch (IOException e) {
e.printStackTrace();
Log.d("debug", "connection fail");
reception.setText("Fail Connection");
return false;
}
}
}
return stopWorker;
}
public void DisconnectionDevice()
{
}
public List<String> PutInList(ListView liste)
{
Log.d("debug", "rentré");
Log.d("debug", "Tableau de device");
List<String> BlueDevicesList=new ArrayList<String>();
Log.d("debug", "rentre dans le for");
for(int i=0;i<pairedDevices.length;i++)
{
Log.d("debug", "IN");
BlueDevicesList.add(pairedDevices[i].getName());
}
Log.d("debug", "OUT");
return BlueDevicesList;
}
public void writeData(String data) {
try {
sendStream = socket.getOutputStream();
} catch (IOException e) {
Log.d("debug", "Bug BEFORE Sending stuff", e);
}
String message = data;
byte[] msgBuffer = message.getBytes();
try {
sendStream.write(msgBuffer);
} catch (IOException e) {
Log.d("debug", "Bug while sending stuff", e);
}
}
public void writeData(int number) {
try {
sendStream = socket.getOutputStream();
} catch (IOException e) {
Log.d("debug", "Bug BEFORE Sending stuff", e);
}
int message = number;
byte msgBuffer =(new Integer(message)).byteValue();
try {
sendStream.write(msgBuffer);
} catch (IOException e) {
Log.d("debug", "Bug while sending stuff", e);
}
}
public void beginListenForData(final TextView reception) {
try {
Log.d("debug", "creation de receivestream");
receiveStream = socket.getInputStream();
} catch (IOException e) {
}
Thread workerThread = new Thread(new Runnable()
{
public void run()
{
Log.d("debug", "run de threadWorker");
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
//Log.d("debug","post try");
try
{
//Log.d("debug","try");
if(receiveStream.available() > 0)
{
Log.d("debug", "Available OK");
byte buffer[] = new byte[100];
// On lit les données, k représente le nombre de bytes lu
int k = receiveStream.read(buffer, 0, 100);
Log.d("debug", "Lecture Bugger : OK");
if(k > 0) {
// On convertit les données en String
Log.d("debug", "Conversion donnée");
byte rawdata[] = new byte[k];
Log.d("debug", "Nouveau tableau de Byte");
for(int i=0;i<k;i++){
Log.d("debug", "Boucle for : check");
rawdata[i] = buffer[i];
Log.d("debug", "Rawdata = buffer");
}
String data = new String(rawdata);
Log.d("debug", "new string");
Message=data;
//a faire dans le Thread UI
runOnUiThread(new Runnable() {
public void run() {
reception.setText(" "+Message);
}
}
);
Log.d("debug", data);
}
}
}
catch (IOException ex)
{
Log.d("debug", "marche pas");
stopWorker = true;
}
}
}
});
workerThread.start();
}
} |
Partager