Getting data from an RS232 Port using Android Studio (Java) - java

Trying to get the weight from the weighing scale through a port connected to a USB that's connected to my android phone. The data is coming through but I don't know how to convert it properly. This is the code I'm currently using to convert the data:
try {
String data = new String(arg0, "UTF-8");
if (mHandler != null)
mHandler.obtainMessage(MESSAGE_FROM_SERIAL_PORT, data).sendToTarget();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
This is what I'm currently getting:
The number 14700t was supposed to show on the textview.

Related

Get data usage for Wi-Fi and cellular network in Android java for Android versions above pie

I would like to know how to determine the data usage for the specific application above Android version pie programmatically. I found from Android 10 onwards subscriber Id is not accessible to third party applications. Hence I'm not able to fetch data usage using networkStatsManager. But I found there are applications in Play store which provide the solution . I would like to know the implementation for the same.
I'm not sure on this but they actually use the /proc/net/dev file generated by the phone to get the data .
An example to do that without permissions would be to use a function like this
public static long[] getNetworkUsageKb() {
BufferedReader reader;
String line;
String[] values;
long[] totalBytes = new long[2];//rx,tx
try {
reader = new BufferedReader(new FileReader("/proc/net/dev"));
while ((line = reader.readLine()) != null) {
if (line.contains("eth") || line.contains("wlan")){
values = line.trim().split("\\s+");
totalBytes[0] +=Long.parseLong(values[1]);//rx
totalBytes[1] +=Long.parseLong(values[9]);//tx
}
}
reader.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//transfer to kb
totalBytes[0] = totalBytes[0] / 1024;
totalBytes[1] = totalBytes[1] / 1024;
return totalBytes;
}
SO Question on data in proc-net-dev

Write bytes from audio live stream to file using php

My goal is to record live audio stream then save it on my website programmaticaly.
I already do this job using android app that runs on android physical phone or android emulator .. the app reads stream bytes then save it in a file then upload it to my website .. can i do this job directly using cpanel php?
Can i get bytes from live audio stream and open new file using php then write this bytes inside it and finally save this file?
Thanks in advance and execuse me if this question wrong or not logic.
Code in Android
try {
if (inputStream != null) {
if ((output = inputStream.read(bytes, 0, bytes.length)) != -1) {
buffer.write(bytes, 0, output);
}
}
} catch (IOException e) {
e.printStackTrace();
}

Writing huge json object to file

So i have a huge JSONObject that I need to write to a file, right now my code work perfectly on 90% of the devices, the problem is on low memory devices such as Amazon Fire TV the app crashes with an error "java.lang.OutOfMemoryError".
I wonder is there another more memory friendly way to write that json object to file?
That's my code:
try{
Writer output = null;
if(jsonFile.isDirectory()){
jsonFile.delete();
}
if(!jsonFile.exists()){
jsonFile.createNewFile();
}
output = new BufferedWriter(new FileWriter(jsonFile));
output.write(mainObject.toString());
output.close();
} catch (Exception e) {
e.printStackTrace();
}

What is the easiest way to save a String on a server

I created a game and now I want to add a global highscore. I want to save the highscore on my server. I think the easiest way is to overwrite a textfile on my server that stores the scores and the names of the top players. How can I do this? The game is not running on my server! It is running on the client side.
Here is an example of writing a string to a file using the java.nio.file.Files class:
try {
String hightscore = "MyString";
Files.write(new File("D:/temp/file.txt").toPath(), hightscore.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
You can save it as a flat file like flavio.donze did or you can use a database.
So there is usually no relevance if you want to save it one the server or somewhere else. The path decides the location.
If you want to upload the scores from a client to a server, you can use multiple solutions.
F.e. adding per RMI or Webservice call
You can hire a simple hosting PHP/MySql and save the score in a database.
try {
URL url = new URL("http://exemple.com/saveScore.php");
InputStream is = url.openStream();
Scanner scanner = new Scanner(is);
scanner.useDelimiter("\\A");
String response = scanner.hasNext() ? scanner.next() : null;
if (response == "ok") {
System.out.println("Saved!");
}
}
catch (IOException e) {
e.printStackTrace();
}
In saveScore.php, after you save, just print ok.
<?php
// DB OPERATIONS
echo "ok";
exit;

how to convert rfid tag data (askii) to string in JAVA

I'm using the (id12 innovations) RFID read with a Raspberry Pi.
Using the PI4J java library and it's serial example, I'm able to read some data like (5002CF13C6) i'm not sure what this data is! it suppose to get this number (0002948115).
here's is my code:
// create an instance of the serial communications class
final Serial serial = SerialFactory.createInstance();
// create and register the serial data listener
serial.addListener(new SerialDataListener() {
#Override
public void dataReceived(SerialDataEvent event) {
//-----------
System.out.print("\n" + event.getData());
//-----------
}
});
try {
// open the default serial port provided on the GPIO header
serial.open("/dev/ttyAMA0", 9600);
// continuous loop to keep the program running until the user terminates the program
for (;;) {
try {
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
try {
// wait 1 second before continuing
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Rfid.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (SerialPortException ex) {
System.out.println("e: RFID setup failed : " + ex.getMessage());
}
what should i do to event.getData() in order to be able to read the real data?
event.getData(), is returning to you exactly what the id12 chip is saying on the serial port. The data is a 10 character string representation of a hexadecimal number, followed by a 2 character checksum.
The behavior is specified in the id12 datasheet, which can be quickly found on google, or here: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/ID/ID-2LA,%20ID-12LA,%20ID-20LA(2013-4-10).pdf . In the linked PDF, it is page 4.
If you would like some help parsing this data in java, please supply some actual read data, and the corresponding expected values belonging to that read data.

Categories