Unity的Android Java USB串行通信插件未运行

时间:2018-09-25 01:23:52

标签: java android unity3d plugins usb

我创建了一个Java插件,该插件利用UsbManager设备与指定的串行设备进行通信。我正在使用Android Studio运行插件,并且可以写入适当的设备,但是我不了解如何将代码与Unity集成。我在构造函数中传递了Context,因此可以创建UsbManager,但是我不知道如何在Unity中做到这一点,或者不知道是否有另一种获取Context的方法。

将Context从Unity传递到我的插件的正确方法是什么?我也不确定我的功能是否在Unity中正常工作,因为我不知道清单文件中的USB是否也需要权限。

统一代码:

void Start()
{
        ajc = new AndroidJavaObject("com.bebopsensors.dg.USBController");
        int connected = ajc.Call<int>("startUSB");
}

Java代码:

public class USBController {

    private Context context;
    private static final String ACTION_USB_PERMISSION = "com.bebopsensors.dg.USB_PERMISSION";
    private final int BAUD_RATE = 19200;

    private int bytesRead;
    private byte[] readBuffer;
    private UsbManager usbManager;
    private UsbDeviceConnection connection;
    private UsbSerialDevice serial;
    private UsbDevice dg = null;

    public USBController(Context context){
        this.context = context;
    }

    public int startUSB(){
        //usbManager = (UsbManager) context.getSystemService(context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        UsbDevice dg = null;

        if(deviceList.size() == 0){
            return -2;
        }
        // 1st and only device
        dg = deviceList.values().iterator().next();

        if(dg != null){
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
            usbManager.requestPermission(dataglove, pi);
            UsbDeviceConnection connection = usbManager.openDevice(dataglove);

            UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(dg, connection);
            serial.open();
            serial.setBaudRate(BAUD_RATE);
            serial.setDataBits(UsbSerialInterface.DATA_BITS_8);
            serial.setStopBits(UsbSerialInterface.STOP_BITS_1);
            serial.setParity(UsbSerialInterface.PARITY_NONE);
            serial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
            serial.read(callback);
        } else {
            return -1;
        }
        return 0;
    }

    private UsbSerialInterface.UsbReadCallback callback = new UsbSerialInterface.UsbReadCallback() {
        @Override
        public void onReceivedData(byte[] data) {
            bytesRead = data.length;
            readBuffer = data;
        }
    };

1 个答案:

答案 0 :(得分:1)

  

将Context从Unity传递到我的插件的正确方法是什么?

C#端:

您的{ "name": "indecision-app", "version": "1.0.0", "description": "", "main": "index.js", "author": "", "license": "ISC", "scripts": { "serve": "live-server public/", "build": "webpack", "build-babel": "babel ./public/src/app.js --out-file=./public/scripts/app.js --presets=env,react --watch" }, "dependencies": { "babel-cli": "6.24.1", "babel-preset-env": "^1.5.2", "babel-preset-react": "6.24.1", "live-server": "^1.2.0", "webpack": "^4.19.1", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.8" } } 类具有一个以USBController作为参数的构造函数。在调用Context函数之前,获取Unity startUSB并将其发送到 创建Context实例时的构造函数。

获取Unity上下文:

USBController

在创建实例时将上下文发送到Java代码:

    AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

现在,您可以调用ajc = new AndroidJavaObject("com.bebopsensors.dg.USBController", unityContext); 函数了:

startUSB

Java Side:

现在,在int connected = ajc.Call<int>("startUSB"); 函数中,可以将startUSBContext一起使用。我注意到您对此发表了评论。请注意,getSystemService应该是context.USB_SERVICEContext.USB_SERVICE中的c应该大写。

Context
  

清单中不知道USB是否也需要权限   文件。

对此不太确定,但我相信调用您执行过的public int startUSB() { usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); .... .... } 函数就足够了。如果这不起作用,那么我建议您仅在Android和无Unity中测试Java程序,以查看它是否有效。如果可以,我建议的解决方案也应该可以。