I m trying to read data from ads1299 arduino module. I m able to connect device with my android application, but still get unreadable characters. I even tried decoding with UTF-8, US-ASCII, ISO-8859-1.
Here is my code
mmBuffer = new byte[1024];
int numBytes;
while (true) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
String str = new String(mmBuffer, 0, numBytes);
//String str = new String(mmBuffer, "UTF-8");
} catch (IOException e) {
Log.d(LOG_TAG, "Input stream was disconnected", e);
break;
}
}
Sample encoded characters currently I m getting
����������D#����������Ѫ���w����w���$W���������W.����⪪��뚪���/U���
Related
I am trying to send a photo from my Raspberry Pi using Python 3, to my Android Device. I am doing this over TCP with the Pi as the Client, and the Android Device as my Server. My goal is to send a photo file from Pi to my Android Device. My Android Device would then decode that photo data and then set it as the drawable for an ImageView in my App. Kindly note that I'm sending a 200kB image that's 640x480.
I have tried a set up where the Pi sends text to my Android Device via TCP, and I've had success with that.
What I did next was to attempt to send a photo from a Python3 client to a Python3 Server. In this case, I used the Pi still as my client, and I used my MacOS Laptop as the Server. This are the code that I ended up using.
Server - MAC OS
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 11111 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
f = open('torecv.jpg','wb')
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
print("Receiving...")
l = c.recv(1024)
while (l):
print("Receiving...")
f.write(l)
l = c.recv(1024)
f.close()
print("Done Receiving")
c.send(b'Thank you for connecting')
c.shutdown(socket.SHUT_RDWR)
c.close() # Close the connection
Client - Pi
import socket # Import socket module
import os
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
host = '192.168.254.194' # Get local machine name
port = 6001 # Reserve a port for your service.
CWD_PATH = os.getcwd()
PATH_TO_IMG_DIR = os.path.join(CWD_PATH, 'img_folder', 'test.jpg')
s.connect((host, port))
#s.send(b'Hello Server!')
f = open(PATH_TO_IMG_DIR,'rb')
print('Sending...')
l = f.read(1024)
while (l):
print('Sending...')
s.send(l)
l = f.read(1024)
f.close()
print("Done Sending")
s.shutdown(socket.SHUT_WR)
print(s.recv(1024))
s.close # Close the socket when done
Using this code, I was able to transfer the photo from my Pi to my MacOS laptop.
Now, I used the code here as reference in order to transfer my photo from my Pi to my Android Device. Now, this is my code:
Server - Android
class ServerThread implements Runnable {
public void run() {
Socket socket;
try {
Log.e(TAG, "starting the serverthread at port 6001");
serverSocket = new ServerSocket(6001);
} catch (IOException e) {
Log.e(TAG, "exception in creating server socket: ", e);
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
}
}
}
}
class CommunicationThread implements Runnable{
private Socket clientSocket;
private DataInputStream input;//private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
Log.e(TAG, "getting data from the input stream!");
//this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
InputStream in = this.clientSocket.getInputStream();
this.input = new DataInputStream(in);
} catch (IOException e) {
Log.e(TAG, "error in creating data input stream: ", e);
e.printStackTrace();
}
}
public void run() {
Log.e(TAG, "running the code!");
while (!Thread.currentThread().isInterrupted()) {
try {
Log.e(TAG, "parsing the input data stream!");
byte[] data;//String read = input.readLine();
int len= this.input.readInt();
if (len > 0) {
data = new byte[len];
this.input.readFully(data,0,data.length);
}
/*
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data;
int length = 0;
while ((length = this.input.read(data))!=-1) {
out.write(data,0,length);
}
data=out.toByteArray();
*/
Log.e(TAG, "Updating the UI through a thread!!");
// updateConversationHandler.post(new updateUIThread(data));
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "error in reading sent data! ", e);
e.printStackTrace();
}
}
}
}
And in order to use these classes, I had declared the following as global variables:
Thread serverThread = null;
Handler updateConversationHandler;
And in my onCreate(), I have the following:
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
The app would start, and the socket would be opened. However, when I attempt to send in the photo from my Pi, I hit an error at this block of code:
public void run() {
Log.e(TAG, "running the code!");
while (!Thread.currentThread().isInterrupted()) {
try {
Log.e(TAG, "parsing the input data stream!");
byte[] data;//String read = input.readLine();
int len= this.input.readInt();
if (len > 0) {
data = new byte[len];
this.input.readFully(data,0,data.length);
}
/*
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data;
int length = 0;
while ((length = this.input.read(data))!=-1) {
out.write(data,0,length);
}
data=out.toByteArray();
*/
Log.e(TAG, "Updating the UI through a thread!!");
// updateConversationHandler.post(new updateUIThread(data));
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "error in reading sent data! ", e);
e.printStackTrace();
}
}
}
class updateUIThread implements Runnable {
private byte[] byteArray;//private String msg;
public updateUIThread(byte[] array){ //public updateUIThread(String str) {
this.byteArray=array; //this.msg = str;
}
#Override
public void run() {
Log.e(TAG, "running the photo update!");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
ivBed.setImageBitmap(bitmap);//text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}
}
Originally, the line:
data = new byte[len];
was outside the if(len>0) condition. But what happened was, for some reason, the Pi sent in a negative value, and of course we don't want a negative value for the len variable. Of course, I hit an error when I tried to create the byte array data with a negative length. I then put that line in the if condition.
However, after I did, I hit on OOM Error in the same line data = new byte[len];
Process: agict.work.freelance.patientsensor, PID: 15224
java.lang.OutOfMemoryError: Failed to allocate a 1677608328 byte allocation with 6291456 free bytes and 254MB until OOM, max allowed footprint 7797480, growth limit 268435456
I have a hunch that in the 2nd error, I was trying to initialize the byte array with a value that was actually the image data already, hence, the OOM error.
However, if I just take in the first value and assign it as the len, there's a chance that I'd get a negative number and the code would hit the first error.
Would there be a chance that I have to tweak something in order to transfer a photo data from Python3 to Android? I have a feeling that there's a format mismatch of sorts that's happening.
Again, my goal is to send a photo file from Python3 to my Android Device via TCP. The Python3 will be given a file, and the Android Device will decode the input it gets, and once decoded, use that data as the drawable for an ImageView.
According to me that error is caused by the size of the bitmap that you are trying to set for the ImageView. Checking sizes before use and/or scaling the bitmap may solve the issue. I've encountered such problems in the past before I start using third party libraries to load images.
You can take a look at this article from Android Developers and I think it can help:
https://developer.android.com/topic/performance/graphics/load-bitmap#java
I got it. You're supposed to use a ByteArrayOutputStream Object.
class ServerImageThread implements Runnable{
ServerSocket ss;
Socket s;
DataInputStream dis;
byte[] data;
#Override
public void run(){
try{
ss = new ServerSocket(6001);
while(true){
s = ss.accept();
InputStream in = s.getInputStream();
dis = new DataInputStream(in);
// read from the stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] content = new byte[1024];
int bytesRead = -1;
while( (bytesRead = in.read(content)) != -1 ) {
baos.write( content, 0, bytesRead );
} // while
Log.e(TAG, "made it through!");
Log.e(TAG, "baos size is = " + baos.size());
File file = new File(
Environment.getExternalStorageDirectory(), "test.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
baos.writeTo(fos);
Log.e(TAG, "managed to write baos to fos");
} catch(IOException ioe) {
// Handle exception here
Log.e(TAG, "baos IOException = ", ioe);
ioe.printStackTrace();
} finally {
Log.e(TAG, "closing fos");
fos.close();
}
}
}
catch(IOException e) {
Log.e(TAG, "exception in creating server socket: ", e);
e.printStackTrace();
}
}
}
You can call this via:
Thread myThread = new Thread(new ServerImageThread());
myThread.start();
I am working on a project that involves communication between an Android Uno and an Android phone. The phone sends a request signal "*" that once received, the Arduino sends random integers in a loop. Right now, the Android device is receiving the message but it is showing up as boxed question marks, and not receiving all of the messages. Any ideas? Thank you so much!
Arduino code:
#include <SoftwareSerial.h>
const int RX_PIN = 0;
const int TX_PIN = 1;
SoftwareSerial bluetooth(RX_PIN, TX_PIN);
char commandChar;
void setup (){
bluetooth.begin (9600);
Serial.begin(38400);
}
void loop () {
if(bluetooth.available()){
commandChar = bluetooth.read();
switch(commandChar){
case '*':
Serial.println("Got the request code");
for(int i = 0; i < 10; i++){
bluetooth.print(random(21));
}
break;
}
}
}
Android code:
public void run() {
initializeConnection();
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer);//read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
Log.e("Received Message: ", readMessage);
} catch (IOException e) {
break;
}
}
}
public void initializeConnection() {
try {
PrintWriter out;
out = new PrintWriter(mmOutStream, true);
out.println("*");
out.flush();
}catch (NullPointerException NPE) {
}
}
Console output:
08-13 19:02:46.546 4019-4128/? E/Received Message:: �
08-13 19:02:46.596 4019-4128/? E/Received Message:: ����
Ah I think I spot the problem. Random numbers are being sent from the arduino to the app, and the app is logging these bytes as ascii literals. Instead of sending random numbers, try sending well-formed ascii (visual characters).
You can send the hex bytes [0x68,0x65,0x6c,0x6c,0x6f] for "hello", or use SoftwareSerialPrint's built-in HEX option.
So change it to this, see if that works.
bluetooth.print(random(21), HEX);
Edit:
Let's try this on the app side instead. This will convert the received bytes into a hexadecimal string representation so we can see it in ascii properly.
bytes = mmInStream.read(buffer);//read bytes from input buffer
StringBuilder sb = new StringBuilder(bytes * 2);
for(byte b: buffer)
sb.append(String.format("%02x", b));
Log.e("Received Message: ", sb.toString());
I am trying to write an app that will log the output of an arduino Due to a text file on the phone or tablet. The output rate is 1kHz. I have based my app on the Blueserial code (https://github.com/plastygrove/BlueSerial). The bluetooth connection gets established properly with the arduino bluetooth module, the commands are sent and received properly and everything seems to work just fine. However, the file that I am saving the data to is missing blocks of data, usually around 200ms worth every so often (I have a millisecond timestamp included in my data), resulting in corrupted data. I have been trying to figure out the source of the problem and I think it might be related to the gc but at this point I am at a loss. This is the code that writes my data to the file:
private class ReadInput implements Runnable {
private boolean bStop = false;
private Thread t;
public ReadInput() {
t = new Thread(this, "Input Thread");
t.start();
}
public boolean isRunning() {
return t.isAlive();
}
public void run() {
InputStream inputStream;
try {
inputStream = mBTSocket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
while (!bStop) {
byte[] buffer = new byte[250];
int bytes = 0;
if (bis.available() > 0) {
bytes = bis.read(buffer);
strInput = new String(buffer, 0, bytes);
sb.append(strInput);
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
pw.print(strInput); // print buffer to the file buffer
pw.flush(); // flush buffer and force write to media
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear the string
pw.print(strInput); // write buffer to file buffer
pw.flush(); // force writing to file
pw.close(); // close print writer
try {
f.close(); // close file output stream
} catch (IOException e) {
e.printStackTrace();
}
}
sb.delete(0, sb.length()); strInput = "";
}
//Thread.sleep(100);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop() {
bStop = true;
}
}
and this is my file outputsteam and printwriter declarations:
String strInput = null;
static PrintWriter pw = null;
static FileOutputStream f = null;
private StringBuilder sb = new StringBuilder();
The data I am sending is formatted as so:
24.330,-58,5,119,460\n
24.331,-86,25,-105,460\n
24.332,66,41,-145,460\n
24.333,90,-23,-85,4622,-7,119,460\n
24.524,6,-95,107,461\n
24.525,10,-7,-173,461\n
24.526,-22,33,103,461\n
and in this example you can see where it skipped some data. Thank you for helping out!
There appears to be code missing or you have some extra logic.
For example the StringBuilder doesn't appear to be doing anything,
bStop is never set/cleared. But you are always printing out the incoming data
via strInput.
The end of line handling also looks off, specifically this:
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length());
You extract the string and then delete the whole buffer sb.delete(0,sb.length())
same at the bottom of the loop.
I am creating a app where videos are played in T.v by connecting the android device to T.v through HDMI cable.I want detect if T.V is turned off using the HDMI cable.I also tried a method mentioned in this link but its not working.
How to check the HDMI device connection status in Android?
Get the data file from the location /sys/class/display/display0.hdmi/connect.If the data in the file is 0 hdmi is not connected if its 1 its connected.Try this method.
try
{
File file = new File("/sys/class/display/display0.hdmi/connect")
InputStream in = new FileInputStream(file);
byte[] re = new byte[32768];
int read = 0;
while ( (read = in.read(re, 0, 32768)) != -1)
{
String string="Empty";
string = new String(re, 0, read);
Log.v("String_whilecondition","string="+string);
result = string;
}
in.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
From my Android phone, I'm trying to read (using Bluetooth) incomming strings from an external GPS device. I've followed mainly the BluetoothChat example and everything seems to work as expected so far. My reading thread is executing and I can see variable bytes packets incoming when looping with the following code:
Log.d(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true)
{
try
{
bytes = mmInStream.read(buffer);
// Test...
String strReadBuf = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothHandler.MessageType.READ,
bytes, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
sendErrorMessage(R.string.bt_connection_lost);
break;
}
}
The strings I'm supposed to read are text strings (NMEA format) but I'm reading only 0 and -32 bytes in my buffer array. Any idea why I'm getting this?
Log.d(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true)
{
try
{
bytes = mmInStream.read(buffer);
// Test...
//String strReadBuf = new String(buffer, 0, bytes);
//I've changed for
String strReadBuf = new String(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothHandler.MessageType.READ,
bytes, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
sendErrorMessage(R.string.bt_connection_lost);
break;
}
}
I used the String constructor String(byte[]), where byte[] is the buffer deprecating the byte[] size. I've used many times and that works for me even if the buffer size changes over time.