Bonjour, j'ai un équipement Arduino qui m'envoie des données chaque seconde par bluetooth. J'aimerais recupérer ces données en temps réel sur
mon téléphone Android.Pour simuler cela , j'ai créé une application android qui m'envoie les données en temps réel et une autre sur le téléphone qui récupérer ces données en temps réel.J'ai un probleme avec l'application qui m'envoit les données, voici le code ecrit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 public class Test extends Activity {
 
    public static final String CARDIO_UUID = "00001101-0000-1000-8000-00805F9B34FB";
 
	private UUID cardioUuid = UUID.fromString(CARDIO_UUID);
 
	public final String RET = "\n";
 
	private BluetoothSocket clientSocket = null;
 
	CardioWriteThread writeOnSocket;
	CardioConnectThread connectThread;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
 
        Log.d("DEBUG","ICI1");
        connectThread = new CardioConnectThread();
        connectThread.start();
    }
 
 
    class CardioConnectThread extends Thread {
		public CardioConnectThread() {
			BluetoothSocket temp = null;
 
			try {
				Thread.sleep(2000);
				Log.d("DEBUG","ICI2");
				BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	    		if(adapter != null){
	    			if(adapter.isEnabled()) {
	    				Set<BluetoothDevice> visibleDevices = adapter.getBondedDevices();
	    				if(visibleDevices.size() > 0) {
	    					BluetoothDevice[] devices = (BluetoothDevice[]) visibleDevices.toArray();
	    					BluetoothDevice device = devices[0];
	    					//ParcelUuid[] uuids = device.getUuids();
	    					Log.d("DEBUG","ICI3");
	    					temp = device.createRfcommSocketToServiceRecord(cardioUuid/*uuids[0].getUuid()*/);
	    				}
	    			}
	    		}
	    		else {
	    			Log.d("DEBUG","ICI3");
	    			Log.d("Error", "Bluetooth adaptater not found !!");
	    		}
			}
			catch(IOException e) {
				Log.e("Error Socket","Socket listen() failed!");
			}
			catch(InterruptedException e){}
 
			clientSocket = temp;
		}
		@Override
		public void run() {
			while(true) {
				try {
					clientSocket.connect();
				}
				catch(IOException e) {
					Log.e("Error","couldn't serverSocket.accept() !");
					break;
				}
				// lorsque la connection etait accepte
				if(clientSocket != null) {
					Log.d("DEBUG","ICI4");
					writeOnSocket = new CardioWriteThread();
					writeOnSocket.start();
				}
			}
		}
 
		public void cancel() {
			try {
				clientSocket.close();
			}
			catch(IOException e){}
		}
	}
 
    class CardioWriteThread extends Thread {
 
		private final OutputStream outStream;
		private boolean continueWriting = true;
 
		public CardioWriteThread() {
			OutputStream tmp = null;
			try {
				tmp = clientSocket.getOutputStream();
			}
			catch(IOException e){}
			outStream = tmp;
		}
 
	    public void write(String s,OutputStream outStream) throws IOException {
		    outStream.write(s.getBytes());
		}
 
		private int getRandom() {
			double high = 3;
			double low = 0.5;
			double value = Math.random() * (high - low) + low;
			return (int)Math.round(value);
		}
 
 
		@Override
		public void run() {
 
			while(continueWriting) {
 
				try {	
  			           write(Integer.toHexString(getRandom())+RET+RET,outStream);
  			           Log.d("DEBUG","ICI5");
  			           try {
					          Thread.sleep(1000);
				       } catch (InterruptedException e) {
					      Log.e("Error","Interrupted thread !!");
				       }
  			   }
  			   catch(NullPointerException e){
  			    	Log.e("Error","can't find Bluetooth Adapter !!"); 
  			   }
  			   catch(IOException e){
  			    	Log.e("Error","Input-Output Errors");
  			   }
 
			}
		}
 
		public void cancel() {
			try {
				outStream.close();
			}
			catch(IOException e){	
			}
			catch(NullPointerException e){}
 
			continueWriting = false;
		}
    }
 
    @Override
    public void onDestroy() {
 
		try {
			connectThread.cancel();
			writeOnSocket.cancel();
		}catch(Exception e) {
			Log.e("Canceling","Can't cancel connection !!");
		}
		super.onDestroy();
    }
}