Bonjour à tous,
J'ai une application Android avec laquelle j'effectue une connexion bluetooth avec un autre appareil et une communication est établie entre les deux.
Je poste cela ici car mon soucis est au niveau I/O.
J'essaie d'expliquer cela clairement :
J'utilise un Thread pour lire mon InputStream. Je me suis rendu compte en affichant le résultat que j'avais des anomalies dans le contenu récupéré.
Résultat attendu :Résultat constaté :1101=xx
1001=xx
2045=xx
...Ces anomalies sont assez aléatoires mais sont jamais identiques. Je suis partie sur la piste de solution suivante : lors de la lecture de mon InputStream j'ai un décalage...0101=xx
1011=xx
2045=xx
...
J'ai ainsi rajouté un reset() sur mon inpustream après le read() afin qu'il se repositionne correctement.
Résultat : Je dois appeler deux fois mon thread qui lit le inputstream car la première fois il me renvoit "1" et c'est tout. En l'appelant une deuxième fois il me renvoit alors la suite sans décallage :Je comprends pas pourquoi il faut que j'appelle alors deux fois mon thread suite à l'ajout de mon reset(). Des idées ?001=xx
1001=xx
2045=xx
La classe comportant ma lecture d'inputstream:
Merci !!!
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 package com.src.ecom.attest.bt; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.bluetooth.BluetoothSocket; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class BufferedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public String msgStatus =""; private int i; private int bytes; // Number of bytes. Handler handler; //Getter public String getMsgStatus() { return msgStatus; } //Setter public void setMsgStatus(String msgStatus) { this.msgStatus = msgStatus; } // Constructeur public BufferedThread(BluetoothSocket socket, Handler h) { mmSocket = socket; handler = h; InputStream tmpIn = null; OutputStream tmpOut = null; i=0; // Get the input and output streams, using temp objects because // member streams are final try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer = new byte[2048]; // Read 2K character at a time. bytes= 0; while(true) { try { if(mmInStream.available() > 0) { // Read from the InputStream. bytes = mmInStream.read(buffer,0,2048); // Send the obtained bytes to the MainActivity. handler.obtainMessage(InterfaceActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); mmInStream.reset(); } } catch(IOException e) { msgStatus = e.toString(); break; } } } /* Call this from the main activity to send data to the remote device */ public void write(byte[] bytes) { try { mmOutStream.write(bytes); msgStatus = "Message Envoyé"; } catch (IOException e) { msgStatus = e.toString(); } } /* Call this from the main activity to shutdown the connection */ public void cancel() { try { mmSocket.close(); msgStatus ="Déconnecté"; } catch (IOException e) { msgStatus = e.toString(); } } }
![]()
Partager