Bonjour, je suis nouveau dans la programmation Android et j'essaye de recevoir des données du client avec des sockets. Quand j'utilise .readline(), ma fenêtre de texte ne se met à jour que lorsque je ferme le programme client en python, alors j'utilise .read() et l'application se ferme.
voici le code :
class Mythread implements Runnable{
Handler h = new Handler();
@Override
public void run() {
Socket s;
ServerSocket ss;
InputStreamReader ist;
BufferedReader bufferedReader;
try {
ss = new ServerSocket(21600);
s = ss.accept();
ist = new InputStreamReader(s.getInputStream());
bufferedReader = new BufferedReader(ist);
while (true){
final String message = bufferedReader.readLine();
Log.d("message", "message: " + message);
h.post(new Runnable() {
@Override
public void run() {
ballscollect.setText(message);
}
});
}
}catch (IOException e)
{
e.printStackTrace();
}
}
}
Ceci est mon code client python où chaque fois que l'interrupteur est cliqué, il envoie une chaîne de sortie à l'application :
import socket
from nanpy import (ArduinoApi, SerialManager)
import time
import thread
import Adafruit_CharLCD as LCD
import math
lcd_rs = 26
lcd_en = 19
lcd_d4 = 13
lcd_d5 = 6
lcd_d6 = 5
lcd_d7 = 11
lcd_backlight = 4
lcd_columns = 16
lcd_rows = 2
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
lcd.message ('Balls Collected')
ledpin = 13
buttonpin = 12
buttonpin1 = 2
buttonpin2 = 3
buttonstate1 = 1
buttonstate2 = 1
buttonstate3 = 1
connection = SerialManager()
a = ArduinoApi(connection = connection)
a.pinMode(ledpin, a.OUTPUT)
a.pinMode(buttonpin, a.INPUT)
a.pinMode(buttonpin1, a.INPUT)
a.pinMode(buttonpin2, a.INPUT)
host = '192.168.1.7'
port = 21600
sock = socket.socket()
sock.connect((host,port))
def count():
add = 0
while True:
buttonstate1 = a.digitalRead(buttonpin)
buttonstate2 = a.digitalRead(buttonpin1)
buttonstate3 = a.digitalRead(buttonpin2)
if buttonstate1:
a.digitalWrite(ledpin, a.HIGH)
else:
a.digitalWrite(ledpin, a.LOW)
add += 1
s = str(add)
lcd.message ('\n'+s)
sock.send(s) #this is the output string that is sent to the app
print ('sent')
time.sleep (.5)
if buttonstate2:
a.digitalWrite(ledpin, a.HIGH)
else:
a.digitalWrite(ledpin, a.LOW)
add += 1
s = str(add)
sock.send(s) #this is the output string that is sent to the app
lcd.message ('\n'+s)
print ('sent')
time.sleep (.5)
if buttonstate3:
a.digitalWrite(ledpin, a.HIGH)
else:
a.digitalWrite(ledpin, a.LOW)
add += 1
s = str(add)
lcd.message ('\n'+s)
sock.send(s) #this is the output string that is sent to the app
print ('sent')
time.sleep (.5)
sock.close()
thread.start_new_thread(count, ())
J'ai une erreur dans le logcat
01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null