0

I am trying to configure a neonode sensor by doing a controlTransfer over USB. My transfer keeps failing even though I have given my android device(a hikey 960 board) permisssion to the USB and the connection appears to be successful from my debug logs. I am using Unity and modifying the UnityPlayerActivity.

In the UnityPlayer activity I have:

public UsbManager getUSBManager(){
    return (UsbManager)getSystemService(Context.USB_SERVICE);
}
public UsbDevice getUSBManagerDevices(){
    UsbManager Manager =  (UsbManager)getSystemService(Context.USB_SERVICE);
    UsbDevice Device;
    HashMap<String, UsbDevice> deviceList = Manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    int i = 0;
    while(deviceIterator.hasNext()) {
        System.out.print("inside while");
        UsbDevice device = deviceIterator.next();
        if(i!=0) {
            Device = device;
            if(!Manager.hasPermission(device)) {
                String ACTION_USB_PERMISSION ="com.Immersion.LRA2n1";
                PendingIntent intent = PendingIntent.getBroadcast(this, 0,
                        new Intent(ACTION_USB_PERMISSION), 0);
                Manager.requestPermission(device, intent);
            }
        }
        i++;
    }
    return Device;
}

In Unity/C# I have:

private static bool ReversedTouchScreen;
private AndroidJavaObject Device;
private AndroidJavaObject USBManager;

 private static AndroidJavaObject currentActivity
 {
     get
     {
         return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
     }

 }
 public void SetDevice()
 {
    USBManager = currentActivity.Call<AndroidJavaObject>("getUSBManager");
    AndroidJavaObject[] UsbDevices = currentActivity.Call<AndroidJavaObject[]>("getUSBManagerDevices");
    device = UsbDevices[0];
    Debug.Log(UsbDevices.Length);  
 }

public bool ReverseTouchScreen()
{
    if (USBManager.Call<bool>("hasPermission", device))
    {
        Debug.Log("Permission granted");
        AndroidJavaObject USBConnection1 = USBManager.Call<AndroidJavaObject>("openDevice", device);
        Debug.Log("opening device");
        byte[] message = new byte[257];
        message[0] = 0xEE;
        message[1] = 0x0E;
        message[2] = 0x40;
        message[3] = 0x02;
        message[4] = 0x02;
        message[5] = 0x00;
        message[6] = 0x73;
        message[7] = 0x08;
        message[8] = 0xA2;
        message[9] = 0x06;
        message[10] = 0x84;
        message[11] = 0x01;
        message[12] = 0xFF;
        message[13] = 0x85;
        message[14] = 0x01;
        message[15] = 0xFF;
        if (USBConnection1 != null)
        {
          if(!USBConnection1.Call<bool>("claimInterface",device.Call<AndroidJavaObject>("getInterface",0),true))
            {
                Debug.Log("failed to claim interface");
                return false;
            }
            int requestType = 0x00 | (0x01 << 5) | 0x01;
            int RequestTransfer1 = USBConnection1.Call<int>("controlTransfer", requestType, 0x09, 0x0301, 0x0000, message, message.Length, 0);
            Debug.Log("Transfer complete");
            Debug.Log("Request transfer resulted in : " + RequestTransfer1);
        }
        else
        {
            Debug.Log("Unable to open device");
        }
        return true;
    }
    return false;
}

void Start () {
    SetDevice();
}

// Update is called once per frame
void Update () {
    if (!ReversedTouchScreen)
    {
        ReversedTouchScreen = ReverseTouchScreen();
    }
}

Any ideas as to what my problem may be? I have never done a controlTransfer before and am new to android programming so maybe I'm missing something obvious. Any and all help is appreciated.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.