EV3 Lejos Bluetooth send and receive data - java

I have some problems with the connection between my EV3 and my Android-Device.
I am able to receive Data with my EV3, but I can't send.
I have signed up with my Github account if you like to see the App.
Here is my Code:
public class Bluetooth {
public static int speed = 100;
public static BTConnector connector;
public static NXTConnection connection;
public static void main(String[] args) throws IOException {
openConnection();
Thread moveWithBluetoothThread = new Thread(new moveWithBluetooth());
moveWithBluetoothThread.start();
}
public static void openConnection() {
connector = new BTConnector();
LCD.drawString("Waiting for Connecrion", 3, 1);
connection = connector.waitForConnection(0, NXTConnection.RAW);
LCD.clear();
LCD.drawString("Connected", 3, 5);
}
}
class moveWithBluetooth implements Runnable {
#Override
public void run() {
while (true) {
InputStream is = Bluetooth.connection.openInputStream();
BufferedReader dis = new BufferedReader(new InputStreamReader(is), 1);
OutputStream os = Bluetooth.connection.openOutputStream();
BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(os), 1);
try {
byte[] b;
for (int i = 0; i < 100; i++) {
String n = dis.readLine();
System.out.println(n);
b = n.getBytes();
Bluetooth.connection.write(b, b.length);
Thread.sleep(10);
dos.write(n);
dos.flush();
}
dis.close();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

I found out, that there was no Problem with my EV3. That means, you can use this code. If you are having the same Problem, here is my solution:
The Problem was the Library I use in my Application. At the "Tssues"-Tab of Github was my Problem. The library waits until there is a complete Transmission, but the EV3 sends a permanent Stream, so I just had to add \r\n at the end of my Transmission. My final Code looked like that:
byte[] b = (n + "\r\n").getBytes();
Bluetooth.connection.write(b, b.length);

Related

json file not arriving complete when using sockets

I have a stream of video. And every frame I need to send a small json file with data from that frame, speed it´s crucial. Whats the best way to do this?
My Server is something like this. Waits for a json file and then has to send that json file to a python application.
public class ServerClass {
public static void main(String[] args) {
Marcoserver mimarco=new Marcoserver();
}
}
class Marcoserver implements Runnable {
public Marcoserver(){
Thread miHilo = new Thread(this);
miHilo.start();
}
public void run() {
try {
ServerSocket server = new ServerSocket(7777);
while (true) {
Socket miSocket = server.accept();
BufferedReader entrada = new BufferedReader(new InputStreamReader(miSocket.getInputStream(), "UTF8"));
String mensaje = entrada.readLine();
JSONObject obj;
obj = new JSONObject(mensaje);
System.out.println(obj);
ConectorSocket cntor = new ConectorSocket("localhost", 6363);
cntor.Conectar();
cntor.Send(mensaje);
cntor.Close();
miSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ConectorSocket{
private String host;
private int port;
Socket sockSend;
public ConnectClass(String hst, int prt ) {
this.host = hst;
this.port = prt;
}
public void Conectar() {
this.sockSend = new Socket(this.host, this.port);
}
public void Send(String mensaje) {
DataOutputStream flujo_salida = new DataOutputStream(sockSend.getOutputStream());
flujo_salida.writeBytes(mensaje);
flujo_salida.close();
}
public boolean Close() {
this.sockSend.close();
}
}
This is the python app simplified:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 6666))
serversocket.listen()
while True:
connection, address = serversocket.accept()
buf = connection.recv(2048)
if len(buf) > 0:
print(buf.decode())
My problem is that the python app prints incomplete information such as:
{"keyword"
{"keyword": [[14, 1, -1]]}
{"keyword": [[14,
instead of:
{"keyword":[]}
{"keyword":[[14,1,-1]]}
{"keyword":[[14,2,-1]]}
There's nothing wrong with your Java code, but your Python code is not good:
while True:
connection, address = serversocket.accept()
buf = connection.recv(2048)
if len(buf) > 0:
print(buf.decode())
That will print just the first received TCP packet for each connection.
You need to continue calling recv until it returns 0:
while True:
connection, address = serversocket.accept()
msg = []
while True:
buf = connection.recv(65536)
if (len(buf) == 0):
break
msg.append(buf)
print(''.join(msg))
connection.close()
You also need to close each connection.

Channel.transmit(new CommandAPDU(array)) retrieve another value when clicking the button again

I have implemented Android App - Server side application. The Android app communicate with the server to get authtenticated by the smart card. When I click a button in the App a TCP connection is built and messages are being exchanged until the protocol ends so far so good. Currently I am facing problem when I click the button in the app again. The same process is beig passed through
but the retrieved data from the smart card are different.
In the SmartCard class --> forwardMessage():
--> First button clicking in the Android App --> first clientSocket:
I am getting this byte [0, -92, 2, 12, 2, 0, 2] array as in the screenshot and when calling channel.transmit(new CommandAPDU(array)); I am getting the right response [-112,0]
--> Second button clicking in the Android App --> second clientSocket (without run the server application neu)
I am getting this byte [0, -92, 2, 12, 2, 0, 2] array as in the screenshot and when calling channel.transmit(new CommandAPDU(array)); I am getting the right response [106,-126]
This result [106,-126] should be like the one of the first clientSocket also [-112,0]. As a result of this retrieved data [106,-126] from the smart card the protocol is not being executed to the end.
I have tried to call the disconnect() of the Card class inside the disconnect() of the SmartCard but I am
I appreciate any help!
the first clientsocket data
the second clientsocket data
Android App --> Server --> Card Reader
Server class
public class Server {
private final static int RECEIVE_BUFFER_LENGTH = 512;
public final static int MESSAGE_MAXIMUM_LENGTH = 256;
private final static int MESSAGE_HEADER_LENGTH = 8;
private static InputStream bufferedInputStream = null;
private static OutputStream outputStream = null;
private static SmartCard smartCard = null;
public static void main(String args[]) throws Exception {
ServerSocket serverSocket = new ServerSocket(27015);
System.out.println("SS construction of server socket");
System.out.println("SS listenting for incoming connection on port 27015");
while (true) {
Socket clientSocket = serverSocket.accept();
Server.bufferedInputStream = new BufferedInputStream(clientSocket.getInputStream());
Server.outputStream = clientSocket.getOutputStream();
Server.smartCard = new SmartCard();
Server.handleConnection();
Server.bufferedInputStream.close();
Server.outputStream.close();
Server.smartCard = null;
clientSocket.close();
System.out.println("Smart card instance was deleted ");
System.out.println("--------------------- Finished ---------------------------");
}
}
private static void handleConnection() throws IOException {
ByteBuffer receiveBuffer = ByteBuffer.allocate(Server.RECEIVE_BUFFER_LENGTH);
int readBytes = 0;
while (true) {
readBytes = Server.bufferedInputStream.read(receiveBuffer.array(), receiveBuffer.position(),
receiveBuffer.remaining());
System.out.println("readBytes: " + readBytes);
if (readBytes < 0) {
break;
}
//Here I am reading the received bytes and communicating with the Smart card.
}
}
}
}
SmartCard
public class SmartCard {
public Card card;
private String protocol;
public void connect(String preferredProtocol) {
Card cardTemp = null;
this.protocol = preferredProtocol;
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
CardTerminal terminal = terminals.get(0);
System.out.println("Reader name: " + terminal.getName());
if (preferredProtocol.equals(ProtocolType.SC_T0.getProtocolName())) {
cardTemp = terminal.connect(preferredProtocol);
} else if (preferredProtocol.equals(ProtocolType.SC_T1.getProtocolName())) {
cardTemp = terminal.connect(preferredProtocol);
}
System.out.println("SC connect --> SCARD Protocol " + preferredProtocol);
this.card = cardTemp;
} catch (CardException e) {
e.printStackTrace();
}
}
private boolean isConnect() {
if (this.card != null) {
return true;
} else {
return false;
}
}
public void disconnect() throws CardException {
if (this.isConnect()) {
this.card = null;
//this.card.disconnect(false);
System.out.println("SC disconnect()");
}
}
private void reconnect(String preferredProtocol) {
if (!this.isConnect()) {
this.connect(preferredProtocol);
}
}
public byte[] requestATR() {
ATR atr = this.card.getATR();
if (atr.getBytes().length > 0xFF) {
throw new IllegalArgumentException(
"Package too big, not supported with protocol -> Answer To Test byte array is too big!");
}
return atr.getBytes();
}
public byte[] forwardMessage(byte[] array) throws CardException {
try {
if (!this.isConnect()) {
this.reconnect(this.protocol);
System.out.println("SC reconnect()");
}
Cg2AapiServer.printData(array, array.length, SmartCard.OPERATOR, Cg2AapiServer.RECEIVE);
CardChannel channel = this.card.getBasicChannel();
System.out.println("data from the client socket: " + Arrays.toString(array));
ResponseAPDU responseAPDU = channel.transmit(new CommandAPDU(array));
byte[] byteArray = responseAPDU.getBytes();
System.out.println("retrieved data from the smart card: " + Arrays.toString(byteArray));
if (responseAPDU.getBytes().length > 0xFF) {
throw new IllegalArgumentException("Package too big, not supported with protocol.");
}
Cg2AapiServer.printData(responseAPDU.getBytes(), responseAPDU.getBytes().length, SmartCard.OPERATOR,
Cg2AapiServer.TRANSMIT);
return responseAPDU.getBytes();
} catch (CardException e) {
e.printStackTrace();
}
return null;
}
}
To get it work I had to add the this.card.disconnect(false); method to the disconnect() method in the smart Card like this:
public void disconnect() throws CardException {
if (this.isConnect()) {
// this.card = null;
this.card.disconnect(false);
System.out.println("SC disconnect()");
}
}

Why isn't this multithreaded code faster?

This is my java code. Before, it calls BatchGenerateResult sequentially which is a lengthy process, but I want to try some multithreading and have each one of them run at the same time. However when I test it, the new time is the same as the old time. I expected the new time to be faster. Does anyone know whats wrong?
public class PlutoMake {
public static String classDir;
public static void main(String[] args) throws JSONException, IOException,
InterruptedException {
// determine path to the class file, I will use it as current directory
String classDirFile = PlutoMake.class.getResource("PlutoMake.class")
.getPath();
classDir = classDirFile.substring(0, classDirFile.lastIndexOf("/") + 1);
// get the input arguments
final String logoPath;
final String filename;
if (args.length < 2) {
logoPath = classDir + "tests/android.png";
filename = "result.png";
} else {
logoPath = args[0];
filename = args[1];
}
// make sure the logo image exists
File logofile = new File(logoPath);
if (!logofile.exists() || logofile.isDirectory()) {
System.exit(1);
}
// get the master.js file
String text = readFile(classDir + "master.js");
JSONArray files = new JSONArray(text);
ExecutorService es = Executors.newCachedThreadPool();
// loop through all active templates
int len = files.length();
for (int i = 0; i < len; i += 1) {
final JSONObject template = files.getJSONObject(i);
if (template.getBoolean("active")) {
es.execute(new Runnable() {
#Override
public void run() {
try {
BatchGenerateResult(logoPath, template.getString("template"),
template.getString("mapping"),
template.getString("metadata"), template.getString("result")
+ filename, template.getString("filter"),
template.getString("mask"), template.getInt("x"),
template.getInt("y"), template.getInt("w"),
template.getInt("h"));
} catch (IOException | JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
es.shutdown();
boolean finshed = es.awaitTermination(2, TimeUnit.MINUTES);
}
private static void BatchGenerateResult(String logoPath, String templatePath,
String mappingPath, String metadataPath, String resultPath,
String filter, String maskPath, int x, int y, int w, int h)
throws IOException, JSONException {
ColorFilter filterobj = null;
if (filter.equals("none")) {
filterobj = new NoFilter();
} else if (filter.equals("darken")) {
filterobj = new Darken();
} else if (filter.equals("vividlight")) {
filterobj = new VividLight();
} else {
System.exit(1);
}
String text = readFile(classDir + metadataPath);
JSONObject metadata = new JSONObject(text);
Map<Point, Point> mapping = MyJSON.ReadMapping(classDir + mappingPath);
BufferedImage warpedimage = Exporter.GenerateWarpedLogo(logoPath, maskPath,
mapping, metadata.getInt("width"), metadata.getInt("height"));
// ImageIO.write(warpedimage, "png", new FileOutputStream(classDir +
// "warpedlogo.png"));
Exporter.StampLogo(templatePath, resultPath, x, y, w, h, warpedimage,
filterobj);
warpedimage.flush();
}
private static String readFile(String path) throws IOException {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String text = new String(data, "UTF-8");
return text;
}
}
It looks like, for all practical purposes the following code should be the only one which can improve performance by using multithreading.
BufferedImage warpedimage = Exporter.GenerateWarpedLogo(logoPath, maskPath,
mapping, metadata.getInt("width"), metadata.getInt("height"));
// ImageIO.write(warpedimage, "png", new FileOutputStream(classDir +
// "warpedlogo.png"));
Exporter.StampLogo(templatePath, resultPath, x, y, w, h, warpedimage,
filterobj);
The rest of it major IO - I doubt how much performance improvement you can achieve there.
Do a profile and check how long each one of the methods is executing. Depending on that you should be able to understand.
Hi sorry not able add to comment part as just joined..
would suggest to first go for dummy method any check whether it works at your end then add your business logic...
if the sample works then you might need to check your "template" class
here's the sample.. check the timestamp
package example;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorStaticExample {
public static void main(String[] args){
ExecutorService ex = Executors.newCachedThreadPool();
for (int i=0;i<10;i++){
ex.execute(new Runnable(){
#Override
public void run() {
helloStatic();
System.out.println(System.currentTimeMillis());
}
});
}
}
static void helloStatic(){
System.out.println("hello form static");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

An IDM like app in java

I'm writing an small application like IDM in java.
But this has has many Exceptions.
This is the code of Downloader class which implements runnable and I want use it for multithreading.
public class Downloader implements Runnable{
private DataInputStream inputStream;
private byte[][] fileData;
private int index;
private int size;
public Downloader(DataInputStream inputStream, byte[][] fileData, int index, int size) {
this.inputStream = inputStream;
this.fileData = fileData;
this.index = index;
this.size = size;
}
public synchronized void run() {
try{
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}
and this is my main class
public class Main {
public static void main(String[] args) {
String s;
//Scanner input = new Scanner(System.in);
//System.out.print("Enter file destination : ");
//s = input.nextLine();
s = "http://video.varzesh3.com/video/clip1/92/uclip/fun/gaf_6_borhani.mp4";
URL url;
URLConnection connection;
DataInputStream inputStream;
FileOutputStream outStream;
byte[][] fileData;
try{
url = new URL(s);
connection = url.openConnection();
inputStream = new DataInputStream(connection.getInputStream());
fileData = new byte[8][connection.getContentLength() / 4];
int size = connection.getContentLength() / 4;
Runnable d0 = new Downloader(inputStream, fileData, 0, size);
Runnable d1 = new Downloader(inputStream, fileData, 1, size);
Runnable d2 = new Downloader(inputStream, fileData, 2, size);
Runnable d3 = new Downloader(inputStream, fileData, 3, size);
Thread thread0 = new Thread(d0);
Thread thread1 = new Thread(d1);
Thread thread2 = new Thread(d2);
Thread thread3 = new Thread(d3);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
inputStream.close();
String path = "C:\\Users\\NetTest\\Desktop\\test.mp4";
outStream = new FileOutputStream(new File(path));
outStream.write(fileData[0]);
/*outStream.write(fileData[1]);
outStream.write(fileData[2]);
outStream.write(fileData[3]);
outStream.write(fileData[4]);
outStream.write(fileData[5]);
outStream.write(fileData[6]);
outStream.write(fileData[7]);*/
outStream.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}
but when I run it this happens
It works: 0
null
null
null
null
What should I do now?
There are 2 Problems in your code.
At the current state you close() the InputStream from which all Thread try to read directly after you started them (-> while they are running). To solve this Problem you can call the join() Method of the Thread class. In your case your'd have to call it for all 4 Threads to make sure they are finished.
If I understand it correctly you want to seperate the download File into 4 parts downloading at the same time.
To do this you need 4 independent InputStreams. (Currently you are using ONE [See also: Java Object Copying])
So to change this your code would look something like this:
public class Downloader implements Runnable{
private byte[][] fileData;
private int index;
private int size;
private URL url;
public Downloader(URL url, byte[][] fileData, int index, int size) {
this.fileData = fileData;
this.index = index;
this.size = size;
this.url = url;
}
public synchronized void run() {
try{
URLConnection connection = url.openConnection();
DataInputStream inputStream = new DataInputStream(connection.getInputStream());
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

reading bytes to Netty server

public class ServerPipelineFactory implements ChannelPipelineFactory {
#Override
public ChannelPipeline getPipeline() throws Exception {
PacketFrameDecoder decoder = new PacketFrameDecoder();
PacketFrameEncoder encoder = new PacketFrameEncoder();
return Channels.pipeline(decoder, encoder, new Handler());
}
}
and my decoder
public class PacketFrameDecoder extends
ReplayingDecoder<PacketFrameDecoder.DecoderState> {
public enum DecoderState {
READ_CONTENT;
}
private int length;
public PacketFrameDecoder() {
super(DecoderState.READ_CONTENT);
}
#Override
protected Object decode(ChannelHandlerContext chc, Channel chnl,
ChannelBuffer cb, DecoderState state) throws Exception {
switch (state) {
case READ_CONTENT:
for (int i = 0; i < cb.capacity(); i ++) {
byte b = cb.getByte(i);
System.out.println((char) b);
}
return null;
default:
throw new Error("Shouldn't reach here.");
}
}
}
and I send messages
Socket fromserver = new Socket("localhost", 7283);
PrintWriter out = new PrintWriter(fromserver.getOutputStream(), true);
int data = 12;
out.write(data);
out.flush();
out.close();
fromserver.close();
but when I get bytes- I have cb.capacity() = 256
and message "?0?0" System.out.println((char) b);
please help
Using capacity is wrong a it is the "max" amount of bytes in the buffer. Also starting at position 0 is wrong as the readerIndex could be on an other position. Please read the apidocs of ChannelBuffer which all of this explains in great detail.

Categories

Resources