http://answers.unity3d.com/questions...using-jni.html
Настрочил вызовы jni:
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class USB_Serial : IDisposable {
private AndroidJavaObject usb;
private AndroidJavaObject dr;
private AndroidJavaClass driver = new AndroidJavaClass("com.hoho.android.usbserial.driver.UsbSerialDriver");
private AndroidJavaClass prober = new AndroidJavaClass("com.hoho.android.usbserial.driver.UsbSerialProber");
private USB_Serial(){
using(AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
using(AndroidJavaObject activity = ajc.GetStatic<AndroidJavaObject>("currentActivity")){
usb = activity.Call<AndroidJavaClass>("getSystemService",new object[] {"usb"});
dr = prober.Call<AndroidJavaClass>("acquire",new object[]{usb});
}
}
}
private bool dropen = false;
public void Open(int speed,int dataBits,int flow,int parity){
if(dr!=null){
dr.Call("open",new object[0]);
try{
dr.Call("setParameters",new object[]{speed,dataBits,flow,parity});
dropen=true;
}
catch{
}
}
}
public int Read(){
int read_c = -1;
if(dropen){
}
return read_c;
}
public void Dispose()
{
usb.Dispose();
dr.Dispose();
}
public static readonly USB_Serial Port = new USB_Serial();
}
Сей возникла проблем с:
/**
* Reads as many bytes as possible into the destination buffer.
*
* @param dest the destination byte buffer
* @param timeoutMillis the timeout for reading
* @return the actual number of bytes read
* @throws IOException if an error occurred during reading
*/
public int read(final byte[] dest, final int timeoutMillis) throws IOException;
/**
* Writes as many bytes as possible from the source buffer.
*
* @param src the source byte buffer
* @param timeoutMillis the timeout for writing
* @return the actual number of bytes written
* @throws IOException if an error occurred during writing
*/
public int write(final byte[] src, final int timeoutMillis) throws IOException;
/**
* Sets various serial port parameters.
*
* @param baudRate baud rate as an integer, for example {@code 115200}.
* @param dataBits one of {@link #DATABITS_5}, {@link #DATABITS_6},
* {@link #DATABITS_7}, or {@link #DATABITS_8}.
* @param stopBits one of {@link #STOPBITS_1}, {@link #STOPBITS_1_5}, or
* {@link #STOPBITS_2}.
* @param parity one of {@link #PARITY_NONE}, {@link #PARITY_ODD},
* {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or
* {@link #PARITY_SPACE}.
* @throws IOException on error setting the port parameters
*/
public void setParameters(
int baudRate, int dataBits, int stopBits, int parity) throws IOException;
А именно с read, так как наверно прямо сделать так:
public int read(ref byte[] data, int ms) {
var len = dr.Call<int>("read",new object[]{data,ms});
return len;
}
НЕЛЬЗЯ.?
Так как тогда?
Сам класс тут.
UPD:
Вечно показывает подключенное устройство.
Не отправляет и не принимает .