receive Bytes from pc in android device via usb - java

I want to know how to receive like 100 bytes of data in my android device, I send from my program:
public void sendVoucherInformationToPhone(String data) {
this.targetDevice.openPort();
this.targetDevice.writeBytes(data.getBytes(), data.getBytes().length);
this.targetDevice.closePort();
}
//the library used in the program is JSerialComm
Screenshot of the program:
I can detect the device itself, But as I said, I have no Idea how to receive the bytes on the other side, so is there any help you can offer me?
(I did read the doc in the official android website but didn't get it, so if any example code or even pseudocode would be so helpful)
thank you

Related

Java usb4java reading from usb device on windows 10 platform

I am trying to read a message from RFID reader connected via USB to windows 10pro machine with usb4java library.
I have managed to claim the interface, opened pipe and registered listener for the data, however the listener is never triggered. The reader acts as keyboard and whatever it reads ends up in active application, such as IDE i have open, instead of in listener.
UsbInterface usbInterface = activeInteface(device);
// there is only one endpoint in the list
UsbEndpoint endpoint = (UsbEndpoint)usbInterface.getUsbEndpoints().get(0);
UsbPipe pipe = endpoint.getUsbPipe();
try {
usbInterface.claim();
// true
System.out.println("claimed usb interface: " + usbInterface.isClaimed());
pipe.open();
// true
System.out.println("pipe open: " + pipe.isOpen());
pipe.addUsbPipeListener(new MessageListener());
// true
System.out.println("pipe active: " + pipe.isActive());
// keep main thread alive, async call should be done from another thread i guess
Thread.sleep(15000);
}
catch (Exception any) {System.out.println(any);}
}
And the listener:
private static class MessageListener implements UsbPipeListener {
#Override
public void errorEventOccurred(UsbPipeErrorEvent event) {
System.out.println(event.toString() + " , " +event.getUsbException());
}
#Override
public void dataEventOccurred(UsbPipeDataEvent event) {
// this code block never triggers
System.out.println("listener ...);
int actualLength = event.getActualLength();
System.out.println("length: " + actualLength);
byte[] data = event.getData();
System.out.println("data length " + data.length);
}
}
i have also tried synchronous read instead of asynchronous in the block above, like this:
byte[] buffer = new data[8];
// this fails on its own, don't even need to read something with RFID reader
int received = pipe.syncSubmit(buffer);
fails with:
USB error1: Transfer error on interrupt endpoint: Input/Output error
There is some windows specific property that library supports: org.usb4java.javax.useUSBDK = true
but this fails when i try to set it with an exception.
I have 0 experience with USB devices so not sure how to proceed from here. Is there something wrong with the code, do i need USBDK or device does not support libUSB driver ? Sadly this is not my device and i don't have access to documentation of the device so cannot be sure if it is device driver issue.
I know that this is 2 years old, but i've had similar issue and this was one of the first questions that i ran into looking for solution, which took me hours.
So, basically, windows doesn't let to read/write keyboard devices directly, to do so, you have to override it's driver (That's why you're getting Input/Output error, and it's written in the hid4java's FAQ).
First way to override device driver is described in libusb wiki.
As far as i know you would have to install a new driver every time you connect the device to a new USB port, which is why i recommend you to read further.
Second way is what you've already mentioned, which is using UsbDk (Usb Drivers Development Kit for Windows). It makes the device accessible for you by detaching the kernel driver and reattaching it back after you're done playing with it.
In order to use it, you need to do two things:
Set the org.usb4java.javax.useUSBDK = true in you javax.usb.properties file as stated in the manual (this can also be done manually in low-level usb4java, see OPTION_USE_USBDK and setOption(Context, int)).
Download and install UsbDk on your system (simplest way is to download x64 or x86 version msi installer which has GUI and is fully automated), which is sadly not in the manual (maybe it's obvious for some people, but took me amount of time that i am not proud of to realize).
Im guessing that the lack of second step is why OP has been getting an exception.
Hope that this will help someone, knowing all this two days ago would save me a lot of headache.
RFID readers operate in keyboard emulation mode by default.
You can normally get a tool from the manufacturer's website to configure the RFID reader.
This will allow you to change the reader to HID mode.
This should resolve your issues.
Sorry for the late response but I hope it helps others.

DJI mavic pro : Receiving corrupted video when using remote controller

I am currently developing an Android application using your SDK. This application is supposed to connect to a Mavic Pro drone in order to receive its video stream and other relevant data.
This application works well when the phone is connected to the drone via WiFi, however, the video stream is corrupted when the phone is connected via the remote controller.
To receive the video frames, I use the following code :
// Callback fired when receiving a new frame of 'size' bytes
VideoFeeder.getInstance().getPrimaryVideoFeed().setCallback((bytes, size) -> {
if (codecManager != null) {
// Shows the video in a "SurfaceTexture" on the phone
codecManager.sendDataToDecoder(bytes, size, UsbAccessoryService.VideoStreamSource.Camera.getIndex());
DroneVideoFrame videoFrame = new DroneVideoFrame(bytes, size, getVideoWidth(), getVideoHeight());
}
});
As mentioned above, this snippet works perfectly when connecting to the drone via WiFi. To be exact, each frame contains ~2000 bytes of data, the video is in 1280x720p in 24fps. The resulting video quality is perfect.
However, when using the remote controller, the data I get is completely different. While the "size" variable tells me that the received frame weighs ~2000 bytes, the frame itself (contained in the variable named "bytes") weighs more than 30 kilobytes. Moreover, this 30 Kb frame seems to be corrupted as it mostly contains what I recognize as buffer bytes (a long sequence of 0's).
Also, the functions "getVideoWidth" and "getVideoHeight" return respectively "9px" and "16px", which is obviously wrong. Moreover thoses function return correct values when using the drone WiFi.
What I have tried :
Update the firmware
Update DJI Go 4 application.
Truncate the buffer bytes (sequences of '0') seen in the frame. This results in a video full of artifacts as seen in the following image
System information :
Drone : DJI Mavic Pro, firmware up to date as of 09 July 2018
Phone : Panasonic FZ-N1 "Toughpad" - Android version : 6.0.1
Would you have any idea what causes that corruption ?
You have to rule out the problem one by one
(1) Can try downgrade one version? it would be a firmware issue. DJI is known to have this sort of problem. Latest doesn't mean safest. For consumer product review, you can refer to here https://forum.dji.com/thread-120739-1-1.html.
If you are a DJI partner, you can call them to confirm on firmware. Today we just had some firmware issue with M200 and PSDK. And we msgs them and they quickly replied. We have to change the firmware to enable gimbal power control for DJI PSDK.
(2) Change a RF spectrum, say from 15 to 18. To make sure it is not because of some frequency hopping or intentional jamming from other people
(3) If changing firmware version and RF doesn't help, try to borrow a 2nd set and run the same code to rule out the hardware issue e.g broken rf link.
(4) If you borrowed a drone and they all have the same problem, then it could be somewhere in your code, there is a bug.
Thats all i can think so far. Ill add if I remember something else

Send APDU commands to USIM/SIM card in android

I was already worked with smart cards and I am familiar with APDU commands (that are defined in ISO/IEC 7816 and Global Platform specifications).
Now I want to know if there is any way to send an APDU command to my USIM/SIM card that is inserted to my mobile phone? (Samsung A3 with Android v4.4.4 kitkat installed.)
I already searched in the Google and I found some related topics and tools named SIM Toolkit Application and Seek for Android. But I don't really understand what are these? Are these items two applications that I must install on my mobile phone? or are those two tools that was installed on the USIM/SIM card already and receive commands from the mobile phone?
What is the difference between Proactive commands , APDU commands and AT commands?
Should I learn android to develop SIM card applications or I just need Java Card specifications and ETSI standards?
Thanks in advance.
There can be two different types of applets present on your SIM card.
Common applets
Common applets written in plain JavaCard. This is the type of applet you are used to from the world of common smart cards. It has the process method and smart card is the passive subject in the communication: your app sends APDU commands and the card responses.
You can communicate with these applets using a special set of Android libraries called SEEK for Android. Have a look at this tutorial to learn how to create such a phone application.
Starting on API level 21 there is also a way to communicate to SIM using Telephony Manager. However, there is one huge obstacle: your app needs MODIFY_PHONE_STATE permission, which can be granted only to system apps. A reqular, non-system app isn't allowed to use it.
SIM Toolkit Applets
A SIM card is much more than just a common smart card and writing an applet for a SIM card can be much more complicated than for a common smart card if you want to use all the possibilities the SIM card offers. I recommend you to read this paper - it is someone's bachelor thesis, but it is the best overview for a beginner I have found all over the Internet. I also recommend this video from the DefConn conference.
The role of the applet loaded on the SIM card is different: the applet is no longer a passive entity. The phone asks your applet regularly: "Is there anything new I can do for you?" and your applet can reply: "Yes, send this SMS, please" or "Tell me what time it is" etc. Moreover, your applet can become a listener of some events: incoming call, received SMS, time interval elapsed etc. Yes, the SIM card seems to be passive from the technical point of view, but its role is in fact an active one: it is the SIM card who sends commands to the phone.
These commands are called "proactive commands" or SIM Application Toolkit commands. Structure is the same - CLA INS P1 P2 LC data LE; the meaning is different.
You can send them from your applet using classes in a special JavaCard package called sim.toolkit.
(SIM Application Toolkit is a standard that specifies the proactive commands in the same way Global Platform specifies the applet's lifecycle.)
Example of SIM Toolkit applet:
import sim.toolkit.ToolkitInterface;
import sim.toolkit.ToolkitRegistry;
...
import javacard.framework.ISOException;
public class STKTest extends Applet implements ToolkitInterface {
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new STKTest().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
//this method handles standard APDU commands
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
apdu.setIncomingAndReceive();
final byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x00:
//do something
break;
}
}
//this method handles the SIM Toolkit commands
public void processToolkit(byte event) throws ToolkitException {
switch (event) {
case ToolkitConstants.EVENT_TIMER_EXPIRATION:
//do something
break;
}
}
}
Yes, you should learn Android - you will need it to use the SEEK library. Your question is very broad, please ask me for any details, if you want.
Starting on API level 22 (Android 5.1) there is another Option called "Carrier Privileges". It allows non-system apps to send APDUs to the SIM card using Android TelephonyManager. See:
https://developer.android.com/reference/android/telephony/TelephonyManager.html#hasCarrierPrivileges()
For example mobile network operator (MNO) Apps that are distributed on Google Play can use this.
But again it's not open for everybody. In this case you need to be granted access by the SIM. The Access Rules on the SIM are managed by the MNO who issued it. See also:
http://source.android.com/devices/tech/config/uicc.html

Sending Push Notification to device using parse4j in java

I am using parse4j library for server side coding and on client side I have iOS device. Now I want to send the push notification from my web browser page I developed in JAVA in which I am using parse4j library to communicate with iOS device through Parse cloud. I am using gwt for coding the server side.
public void sendPushtoIOS() {
Parse.initialize("appId", "restApiId");
ParsePush parsePushObj = new ParsePush();
parsePushObj.sendInBackground("hello from server",null);
}
I am trying to send the notification with the above code, but nothing happens and iOS device doesn't receive any notification. Please could someone guide the code I written is correct or not, If not, how can I send the notification then?
try {
String rawJSON = "{\"aps\":{\"alert\":\""+"your message"+"\"},\"alerts\":{\"others\":\""+others+"\"}}";
PushNotificationPayload payload=PushNotificationPayload.fromJSON(rawJSON);
Push.payload(payload, "path of certificate.p12", password,false, appleDeviceToken);
}
catch (Exception e) {
}
As far as I understand correct, you are using the Parse4j library to send Push Notification. Did you control the current version of parse4j if it can send Push notification? Or it is pending enhancement? One suggestion; in order to send push notification write a cloud code and trigger this cloud code from parse4j. Then cloud code will send the Push notification.
Hope this helps,
Regards.
This library works fine. You can check pushes in parse dashboard, most likely you collected push requests. The only problem was that library works with channels. Maybe you do not set channels.
If you want to work with conditions, add this lines of code in library
JSONObject local = new JSONObject();
local.put("deviceType", "android");
data.put("where", local);
and remove
data.put("channel", "");
data.put("channels", new JSONArray(this.channelSet));
all of these changes must be done in method getJSONData() in class ParsePush
instead of android you can set your devices

Video streaming in android by parcelFileDescriptor

I am succeed to record video through Mediarecorder on SD card
but i want to send this video to a server without writing to SD card.
I search it and i found the parcelFileDescriptor is the way to send
video to TCP socket
but i don't know how to receive it on server side please explain it.
here is my client side code
socket = new Socket("hostname", portnumber);
ParcelFileDescriptor pfd =ParcelFileDescriptor.fromSocket(socket);
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mPreview = new Preview(VideoRecorder.this,recorder);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(mPreview);
I want to receive it on server side and play it to create areal time video transer.
knowing that
"The MediaRecorder records either in 3GPP or in MP4 format. This file format consists of atoms, where each atom starts with its size. There are different kinds of atoms in a file, mdat atoms store the actual raw frames of the encoded video and audio. In the Cupcake version Android starts writing out an mdat atom with the encoded frames, but it has to leave the size of the atom empty for obvious reasons. When writing to a seekable file descriptor, it can simply fill in the blanks after the recording, but of course socket file descriptors are not seekable. So the received stream will have to be fixed up after the recording is finished, or the raw video / audio frames have to be processed by the server".
I want a server(may be Android handset or PC) side code.
if there is another way please help me......
Thanks
In order to stream from android or pc you need to implement protocol over which the stream is carried over and server. There are several of them like HSL, RTPS etc (more http://en.wikipedia.org/wiki/Streaming_media). It is not a trivial problem, and there are only very few successful streaming service from android.
You can check how to implement and steaming service on android here: https://github.com/fyhertz/libstreaming
The library is broken for Android 5, but works for 4.4.x

Categories