Greetings. I have been using Serial PORT API to Transmit Data to Serial Port and the following code to read and write file to serial port.
File file = new File(musicpath + "file.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c,counter=0;
while ((c = br.read()) != -1) {
String s = new StringBuilder().append("").append((char)c).toString();
MainActivity.sendDataChar(s);
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
I have been transmitting 8 BIT MONO WAVE FILES # 22Khz from SD CARD using RS485 and I do have hardware to playback the transmitted data using DAC. (BOTH TRANSMITTER and RECEIVER uses ARM7 - LPC2148).
Now my idea is to have the 8 BIT WAVE FILE stored in SDCARD of Android Device and transmit the same at specified frequency using timer.
I wanted to know how to do this. I tried the above method but I do understand that the transmission frequency does not match as I needed. (PORT OPENED AT 230400 baud rate).
With the above code, a 155 KB File should take approximately 6-7 seconds to finish up the transmission character by character. But it really takes about 15-20 seconds and I dont have the audio output at the receiver end. Just the noise.
Kindly advise me and give some ideas on how to do it in realtime just like the way I managed to do it in ARM7.
Thanks a Lot
Related
I getting issue, printing through bluetooth on thermal printer from pdf file become text view.
Print Pdf file via Bluetooth Printer Android I was tried these example but didn't what I expected.
this is my current code
code file source:
String checkout = "checkout";
String fpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+ checkout + ".pdf";
code to printing
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
System.out.println("ERROR!");
}
byte[] bytesPDF = bos.toByteArray();
byte[] printformat = { 27, 33, 0 }; //try adding this print format
mService.write(printformat);
mService.write(bytesPDF);
I hope able to print pdf file by thermal bluetooth printer. Please help me. Thankyou.
The issue is very clear. As we can see that the printed receipt has formatting syntaxes with it. Which is used to format text and images in a PDF file. So, the printer through which you are trying to print doesn't support printing a PDF file. So, if possible you should provide the file in a compatible format such as a text file.
To know more about formatting text in a Bluetooth printer, you can have a look at this post here. Let me know whether this solves your problem or not.
The way how Thermal printer works is
Open socket connection to printer
Send the encoded data that the printer understands
Close the connection
So, the question here boils down to what's the format of the data to be sent so that the printer is able to understand it and print accordingly. It depends on the manufacturer of the Printer. The encodings are either well documented, packed into an SDK/driver for use or are open source standard encoding for ESC/POS generic printers.
At the end, what you need to do to print a PDF file is -
Convert PDF file to Bitmap[] of pages.
Encode the pages one by one by the command for printing bitmap as provided by the manufacturer.
Pass this encoded string data to the printer.
For Example, do look at the generic ESC/POS implementation in the following GitHub Repo
https://github.com/DantSu/ESCPOS-ThermalPrinter-Android
PrinterTextParserImg.bitmapToHexadecimalString()
I have multicast UDP video stream and android box.
I wrote application on xamarin for android box
that plays this video stream.
Now, I want to write code for the application, that would record video stream to file on the android box and play video from the file when user will want. It will be "TimeShift" and PVR (Personal Video Recorder) function for my application.
I looked at a lot of examples, but there is showed only record video stream from the camera.
If anyone has example code for my case, please share with me.
I tried to use various elements to connect and write data (UdpClient, HttpClient, also UdpSocket from plugin rda.SocketsForPCL). Received an error of connection and error protocol type or protocol is not supported.
Is there a way to connect and save data from udp stream (video)?
string host;
string port;
IPAddress ip_multi = IPAddress.Parse(host);
IPEndPoint ipEndpoint = new IPEndPoint(ip_multi, Convert.ToInt32(port));
Socket clientSocket;
try
{
clientSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);
int Length;
Byte[] b = new Byte[Length];
clientSocket.Connect(ipEndpoint);
clientSocket.Receive(b);
//clientSocket.BeginReceive(b, 0, Length, SocketFlags.Multicast, receiveCallback, clientSocket);
clientSocket.Close();
}
catch (IOException ex)
{
Log.Debug("esception","error!"+ex.ToString());
}
My requirement is to print a file from an android device without using any cloud based service.
I have been able to achieve it using "Raw" print protocol i.e by simply sending the file to printer's IP address at Port 9100. Here is the code snippet for that:
client = new Socket(ip,port); //Port is 9100
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
The problem with "Raw" printing protocol is that there is no way to get the status back from the printer.
So, I recently read about IPP and LDR using which we can get the status back from printer.
I have tried to find a way to implement them using android but had no success. I have already went through this answer but had no success in finding my solution.
It will be really helpful if someone can guide me on how to implement IPP or LDR in android.
Thanks in advance!
General usage of IPP:
Once a print job has been submitted the printer returns a job-id
Use the Get-Job-Attributes-Operation in order to get the current job-state
Wait until the attribute job-state equals to 9 (means 'completed')
There are other final job-states you should check for: aborted or canceled
For prototyping you could use the ipptool (native for desktop usage):
# ipptool -t -d job=482 ipp://192.168.2.113/ipp job.ipp
{
OPERATION Get-Job-Attributes
GROUP operation-attributes-tag
ATTR charset attributes-charset utf-8
ATTR language attributes-natural-language en
ATTR uri printer-uri $uri
ATTR integer job-id $job
}
Update 5/2020
I have published a kotlin implementation of the ipp protocol.
https://github.com/gmuth/ipp-client-kotlin
Once submitted you can wait for the print job to terminate: job.waitForTermination()
I am working n in the meanwhile learning to code in android
I came across this mediastore which returns all the details about all the mp3 files stored on the card(Internal & External).
But this method is very very slow
I guess that's what has been implemented in the default music application
No wonder it sometimes fails to find out all the files...
I was thinking of implementing a faster search algorithm for this purpose, But am confused with the initial requirements of these algorithms
1: I thought of implementing the Binary search method (Divide n Conquer) to find files, but then the algorithm requires information about the number of files to be scanned.How do I get that?
2: I thought of implementing separate threads for each divided cluster.But then will it really work?
Plz help me in this!
the last question : How in the world does poweramp find out all the files so quickly,
My android has about 200 songs on the card, but this app only takes some seconds to get them all!!
Really puzzled!!
Try this code:
go to Download and DCIM folder there you can use this command to get all files and if you want to filter files then here is the link
Process process = null;
try {
process = Runtime.getRuntime().exec("ls -l");
} catch (IOException ez) {
ez.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println("line: "+line.toString());
total.append(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Command Output: "+total.toString());
In my application i want send a file or text over Bluetooth to another Bluetooth device(receiver device may be android,Nokia,LG,etc..). I want to send a file whenever the sever returns the data. For example am checking weather if the climate level falls below any particular given value. It automatically, need to send data over Bluetooth to the receiver device. It wont allow the user to send.
How can I implement it using android Bluetooth API?
And also I need to transfer any file via Bluetooth by converting it to byte array. I have gone through Bluetooth chat example. In that they have given the buffer size of 1024. If the file size more than 1024 byte means how should I transfer. Whether I have to sent each 1024 byte every time and have to merge it at the receiver side or else any other better way is available?
Thanks in advance.
As far as I understand you're asking three questions.
How to send a file whenever a server returns data: You basically open a connection to the server (e.g. http, but might also be any other TCP or UDP-based protocol). Then you listen for incoming data; once you receive data, you trigger whatever action you want. These are some relevant calls for a starting point when your server is not using http (untested, consult the docs for details and alternatives):
Socket s = new Socket('your.server.com', 47000);
s.connect();
SocketChannel c = s.getChannel();
ByteBuffer buffer = new ByteBuffer(1);
c.read(buffer); // blocks until bytes are available
How to initiate a Bluetooth connection automatically: Obtain the target device's BluetoothDevice object, then connect to it - as in the BluetoothChat demo.
How to send a file / more than 1024 bytes via Bluetooth: Yes you have to split your data into blocks on the sending side and reassemble them on the receiving side (mind to send the filesize before the actual data, so the receiver knows when the file is complete). You can also use reasonably larger byte buffers. I'd recommand using a maximum block size of 64 Kb: This allows you to resend blocks without too much (time) cost and doesn't consume too much memory.
As a starter regarding 3., something like this could the core of the sending side (untested and without error handling, just to give the idea):
// Send the file size
OutputStream out = socket.getOutputStream();
ByteBuffer header = ByteBuffer.allocate(8);
header.putLong(file.length();
out.write(header.array());
// Send the file in chunks
byte buffer[1024];
InputStream in = new BufferedInputStream(new FileInputStream(file));
int length = in.read(buffer);
while (length > 0) {
out.write(buffer, 0, length);
if (length < 1024) break;
length = in.read(buffer, 0, sizeof(buffer));
};
... and the receiving side:
// Receive and unmarshal the file size
InputStream in = socket.getInputStream();
ByteBuffer header = ByteBuffer.allocate(8);
byte buffer[1024];
in.read(buffer, 0, 8);
header.put(buffer);
long filesize = header.getLong();
long receivedBytes = 0;
// Receive the file
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
int length = in.read(buffer);
while ((receivedBytes < filesize) && (length > 0)){
out.write(buffer, 0, length);
receivedBytes += length;
length = in.read(buffer);
}
if (receivedBytes != filesize) ... // Assure the transfer was successful