unity3d - Android UsbHost Problems with Samsung Galaxy Tab S 10.5 and Unity -
when application opened, , usbdevice attached, application able communicate device, , doesnt. error's receive differ every time. constant is, works expected on samsung galaxy phone, nexus 7, nexus 10. if application launches device beingness connected, works expected, of time.
manifest set so
<uses-feature android:name="android.hardware.usb.host" /> <uses-feature android:name="android.hardware.usb.usbinterface" /> <activity android:label="@string/app_name" android:screenorientation="fullsensor" android:launchmode="singletask" android:configchanges="mcc|mnc|locale|touchscreen|keyboard|keyboardhidden|navigation|orientation|screenlayout|uimode|screensize|smallestscreensize|fontscale" android:name="com.realart.beatmaker.unityplayernativeactivity"> <intent-filter > <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <intent-filter > <action android:name="android.hardware.usb.action.usb_device_attached" /> </intent-filter> <!-- usb connect list --> <meta-data android:name="android.hardware.usb.action.usb_device_attached" android:resource="@xml/device_filter" /> <intent-filter> <action android:name="android.intent.action.view" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.browsable" /> </intent-filter> <meta-data android:name="unityplayer.unityactivity" android:value="true" /> <meta-data android:name="unityplayer.forwardnativeeventstodalvik" android:value="false" /> </activity>
in oncreate, check see if application launched intent, by:
// device woke activity usbdevice connecteddevice = (usbdevice)getintent() .getparcelableextra(usbmanager.extra_device); if (connecteddevice != null) { usbmanager manager = (usbmanager)getsystemservice(context.usb_service); usbdevice device = connecteddevice; writedeviceinfo(device); initdatatransferthread(manager, device); }
if application opened, , because launchmode
singletask
, i've overridden onnewintent
method so
@override public void onnewintent(intent intent) { final string device_connected = "android.hardware.usb.action.usb_device_attached"; string action = intent.getaction(); if (action.equals(device_connected)) { log.d(tag, "device connected"); final usbmanager manager = (usbmanager)getsystemservice(context.usb_service); seek { final usbdevice connecteddevice = (usbdevice)intent .getparcelableextra(usbmanager.extra_device); if (intent.getbooleanextra(usbmanager.extra_permission_granted, false)) { if (connecteddevice != null) { writedeviceinfo(connecteddevice); initdatatransferthread(manager, connecteddevice); } else { log.d(tag, "device null"); } } else { log.d(tag, "permission denied"); log.d(tag, "asking permission , trying again..."); manager.requestpermission(connecteddevice, permissionintent); } } grab (exception e) { log.d(tag, "exception throw: " + e.tostring()); e.printstacktrace(); } } }
and hits permission denied else block in method, goes permission check broadcast receiver have set so:
// attach events attachreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { log.d(tag, "permissions receiver hit"); string action = intent.getaction(); log.d(tag, "action requested: " + action); usbmanager manager = (usbmanager)getsystemservice(context.usb_service); if (intent.getaction().equals("com.realart.beatmaker.usb_permission")) { // thread still running? if (arduino != null) { if (arduino.pipeline != null) { message msg = message.obtain(); msg.what = change.usb_disconnected; arduino.pipeline.sendmessage(msg); } arduino = null; } synchronized (this) { log.d(tag, "checking permissions user..."); usbdevice device = (usbdevice)intent.getparcelableextra(usbmanager.extra_device); boolean permissiongranted = intent.getbooleanextra(usbmanager.extra_permission_granted, false); log.d(tag, "permissions granted: " + permissiongranted); if (permissiongranted) { if (device != null) { writedeviceinfo(device); initdatatransferthread(manager, device); } else { log.d(tag, "device null"); } } else { log.d(tag, "permission denied user"); } } } } }; permissionintent = pendingintent.getbroadcast(this, 0, new intent("com.realart.beatmaker.usb_permission"), 0); intentfilter attachfilter = new intentfilter("com.realart.beatmaker.usb_permission"); registerreceiver(attachreceiver, attachfilter);
that process doesn't seem causing errors, , device info writes out every time. sometimes, lists 2 interfaces, 0. weird, , dunno why that.
this how initializing device:
private boolean initserialbus() { seek { // open device connection = usbmanager.opendevice(arduino); if (connection == null) { log.d(tag, "opening device failed"); homecoming false; } // serial interface usbinterface = arduino.getinterface(1); boolean interfaceclaimed = connection.claiminterface(usbinterface, force_claim); log.d(tag, "usb interface claimed: " + interfaceclaimed); if (!interfaceclaimed) { log.d(tag, "trying 1 time again kernel driver"); interfaceclaimed = connection.claiminterface(usbinterface, false); if (!interfaceclaimed) { log.d(tag, "unable claim interface."); homecoming false; } } // arduino usb serial converter setup log.d(tag, "data transfter setup"); // line state int linestate = connection.controltransfer(0x21, 0x22, 0, 0, null, 0, 0); if (linestate < 0) { log.d(tag, "line state command transfer failed."); log.d(tag, "return value: " + linestate); releaseusbresources(); arduinostate.onarduinoerror(new exception("control frame transfer error")); homecoming false; } log.d(tag, "line state command frame: ok return: " + linestate); // line encoding (9600 baud) final byte[] lineencoding = { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }; int lineencode = connection.controltransfer(0x21, 0x20, 0, 0, lineencoding, 7, 0); if (lineencode < 0) { log.d(tag, "line encoding command transfer failed."); log.d(tag, "return value: " + lineencode); releaseusbresources(); arduinostate.onarduinoerror(new exception("control frame transfer error")); homecoming false; } log.d(tag, "line encoding command frame: ok return: " + lineencode); // i/o endpoints (int = 0; < usbinterface.getendpointcount(); i++) { usbendpoint endpoint = usbinterface.getendpoint(i); if (endpoint.gettype() == usbconstants.usb_endpoint_xfer_bulk) { if (endpoint.getdirection() == usbconstants.usb_dir_in) { arduinoin = usbinterface.getendpoint(i); } else { arduinoout = usbinterface.getendpoint(i); } } } // ensure found endpoints if (arduinoin == null || arduinoout == null) { releaseusbresources(); arduinostate.onarduinoerror(new exception("no usb endpoints found")); homecoming false; } log.d(tag, "i/o enpoints found"); arduinoconnected = true; } grab (exception e) { e.printstacktrace(); } homecoming arduinoconnected; }
this usual logcat output:
v/usbhostmanager( 2733): usb host uevent: {subsystem=host_notify, state=add, devname=usb_otg, devpath=/devices/virtual/host_notify/usb_otg, seqnum=18860, action=change} d/usbhostmanager( 2733): turnonlcd :: d/usbhostnotification( 2733): setusbobservernotification : cancel id = 998563545, device = usbdevices d/usbhostnotification( 2733): setusbobservernotification : notify id = 998563545, device = usbdevices, title = usb connector connected. d/usbhostnotification( 2733): send timeout : current 1413981920804, vailed = -2999, displayed = 1413981920803 d/usbhostnotification( 2733): setusbobservernotification : cancel id = 998563545, device = usbdevices d/usbhostmanager( 2733): usbdeviceadded : device :: /dev/bus/usb/002/077 [2341h:003dh] [02h,00h,00h] (cdc control) d/usbhostmanager( 2733): usbdeviceadded : interface :: /dev/bus/usb/002/077 [2341h:003dh] [02h,02h,01h] (cdc control) d/usbhostmanager( 2733): usbdeviceadded : interface :: /dev/bus/usb/002/077 [2341h:003dh] [0ah,00h,00h] (cdc data) d/usbsettingsmanager( 2733): deviceattached: /dev/bus/usb/002/077 def bundle com.realart.beatmaker d/usbsettingsmanager( 2733): deviceattached, send intent intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 (has extras) } d/usbsettingsmanager( 2733): deviceattached, phone call resolveactivity started, intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 (has extras) } d/usbsettingsmanager( 2733): resolveactivity : matches count = 1, defaultpackage = com.realart.beatmaker d/usbsettingsmanager( 2733): resolveactivity : defaultri = resolveinfo{425e9ad0 com.realart.beatmaker/.unityplayernativeactivity m=0x108000} d/usbsettingsmanager( 2733): grantdevicepermission: usbdevice[mname=/dev/bus/usb/002/077,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@42d33850] 10212 d/usbsettingsmanager( 2733): grantdevicepermission: mdevicepermissionmap set usbdevice[mname=/dev/bus/usb/002/077,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@42d33850] {} d/usbsettingsmanager( 2733): resolveactivity : permissionsgranted resolveinfo{425e9ad0 com.realart.beatmaker/.unityplayernativeactivity m=0x108000} d/usbsettingsmanager( 2733): deviceattached, phone call resolveactivity ended, intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 cmp=com.realart.beatmaker/.unityplayernativeactivity (has extras) } d/unity (30871): device connected d/unity (30871): permission denied d/unity (30871): asking permission , trying again... d/usbsettingsmanager( 2733): requestpermission:/dev/bus/usb/002/077 ,pi pendingintent{42169a08: pendingintentrecord{42c42748 com.realart.beatmaker broadcastintent}} d/usbsettingsmanager( 2733): request permission device vendorid: 9025, productid: 61, package: com.realart.beatmaker d/usbsettingsmanager( 2733): haspermission++ d/usbsettingsmanager( 2733): haspermission: usbdevice[mname=/dev/bus/usb/002/077,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.os.parcelable;@4360cc38]for {10212=true} d/usbsettingsmanager( 2733): requestpermission:/dev/bus/usb/002/077 has permissions d/unity (30871): permissions receiver nail d/unity (30871): action requested: com.realart.beatmaker.usb_permission d/unity (30871): checking permissions user... d/unity (30871): permissions granted: true d/unity (30871): device name: /dev/bus/usb/002/077 d/unity (30871): vender id: 9025 d/unity (30871): product id: 61 d/unity (30871): class: class android.hardware.usb.usbdevice d/unity (30871): sub-class: 0 d/unity (30871): protocol: 0 d/unity (30871): num interfaces: 2 d/unity (30871): device hash: 263427216 d/arduino (30871): usb interface claimed: false d/arduino (30871): trying 1 time again kernel driver d/arduino (30871): unable claim interface. d/usbsettingsmanager( 2733): checkpermission: /dev/bus/usb/002/077 d/usbsettingsmanager( 2733): haspermission++ d/usbsettingsmanager( 2733): haspermission: usbdevice[mname=/dev/bus/usb/002/077,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@42d33850]for {10212=true}
there seems noticeable timeout after next lines printed in logcat:
d/usbhostnotification( 2733): send timeout : current 1413981920804, vailed = -2999, displayed = 1413981920803 d/usbhostnotification( 2733): setusbobservernotification : cancel id = 998563545, device = usbdevices
if rest of application continues fast enough, problem doesn't happen, if hangs on step few seconds, fails every time. but, not in same spot. when trying claim interface, when sending command transfers, , no failure message, no info beingness received.
when application launched, logcat output looks this:
--------- origin of /dev/log/system d/usbhostnotification( 2733): setusbobservernotification : cancel id = 998563545, device = usbdevices --------- origin of /dev/log/main v/usbhostmanager( 2733): usb host uevent: {subsystem=host_notify, state=add, devname=usb_otg, devpath=/devices/virtual/host_notify/usb_otg, seqnum=19659, action=change} d/usbhostmanager( 2733): turnonlcd :: d/usbhostnotification( 2733): setusbobservernotification : cancel id = 998563545, device = usbdevices d/usbhostnotification( 2733): setusbobservernotification : notify id = 998563545, device = usbdevices, title = usb connector connected. d/usbhostnotification( 2733): send timeout : current 1413982261198, vailed = -2991, displayed = 1413982261189 d/usbhostmanager( 2733): usbdeviceadded : device :: /dev/bus/usb/002/081 [2341h:003dh] [02h,00h,00h] (cdc control) d/usbhostmanager( 2733): usbdeviceadded : interface :: /dev/bus/usb/002/081 [2341h:003dh] [02h,02h,01h] (cdc control) d/usbhostmanager( 2733): usbdeviceadded : interface :: /dev/bus/usb/002/081 [2341h:003dh] [0ah,00h,00h] (cdc data) d/usbsettingsmanager( 2733): deviceattached: /dev/bus/usb/002/081 def bundle com.realart.beatmaker d/usbsettingsmanager( 2733): deviceattached, send intent intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 (has extras) } d/usbsettingsmanager( 2733): deviceattached, phone call resolveactivity started, intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 (has extras) } d/usbsettingsmanager( 2733): resolveactivity : matches count = 1, defaultpackage = com.realart.beatmaker d/usbsettingsmanager( 2733): resolveactivity : defaultri = resolveinfo{42458358 com.realart.beatmaker/.unityplayernativeactivity m=0x108000} d/usbsettingsmanager( 2733): grantdevicepermission: usbdevice[mname=/dev/bus/usb/002/081,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@4259eeb8] 10212 d/usbsettingsmanager( 2733): grantdevicepermission: mdevicepermissionmap set usbdevice[mname=/dev/bus/usb/002/081,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@4259eeb8] {} d/usbsettingsmanager( 2733): resolveactivity : permissionsgranted resolveinfo{42458358 com.realart.beatmaker/.unityplayernativeactivity m=0x108000} d/usbsettingsmanager( 2733): deviceattached, phone call resolveactivity ended, intent { act=android.hardware.usb.action.usb_device_attached flg=0x10000000 cmp=com.realart.beatmaker/.unityplayernativeactivity (has extras) } d/usbhostmanager( 2733): onuevent(device) :: action = add, devtype = usb_interface, device = null, product = 2341/3d/1, type = 2/0/0, interface = 2/2/1, devpath = /devices/platform/exynos-dwc3.0/exynos-xhci.0/usb2/2-1/2-1:1.0 d/usbhostmanager( 2733): onuevent(device) :: action = add, devtype = usb_interface, device = null, product = 2341/3d/1, type = 2/0/0, interface = 10/0/0, devpath = /devices/platform/exynos-dwc3.0/exynos-xhci.0/usb2/2-1/2-1:1.1 d/unity (31983): device name: /dev/bus/usb/002/081 d/unity (31983): vender id: 9025 d/unity (31983): product id: 61 d/unity (31983): class: class android.hardware.usb.usbdevice d/unity (31983): sub-class: 0 d/unity (31983): protocol: 0 d/unity (31983): num interfaces: 2 d/unity (31983): device hash: 263427241 d/arduino (31983): usb interface claimed: true d/arduino (31983): info transfter setup d/arduino (31983): line state command frame: ok return: 0 d/arduino (31983): line encoding command frame: ok return: 7 d/arduino (31983): i/o enpoints found d/arduino (31983): arduino bus initialized d/usbsettingsmanager( 2733): checkpermission: /dev/bus/usb/002/081 d/usbsettingsmanager( 2733): haspermission++ d/usbsettingsmanager( 2733): haspermission: usbdevice[mname=/dev/bus/usb/002/081,mvendorid=9025,mproductid=61,mclass=2,msubclass=0,mprotocol=0,minterfaces=[landroid.hardware.usb.usbinterface;@4259eeb8]for {10212=true} d/arduino (31983): receiver thread started d/unity (31983): onarduinoready d/arduino (31983): resetarduino v/unity (31983): sending message unity = reset v/unity (31983): sending message unity = onarduinoready
when application started, device connected, , onnewintent
method hit, permissions denied block nail every time, prompt asking permissions never shown?
does have thought why behavior unusual tablet?
model: sm-t800
android version: 4.4.2
kernel version: 3.4.39-2010469
build number: kot49h.t800xxu1anfb
in case hits same problem, main section of app created unity , race status between spawned thread reacted intent, , unity on grabbing usbinterface. on boot, because unity wasn't initialized yet, spawned thread able capture interface 99% of time, after unity spawned up, untiy through jni able claim interface first. i'm not exclusively sure why claiming sensor on tablet, , not other ones, has exynos 5 octa 5420
chipset , how unity decides sensor.
android unity3d usb samsung-mobile usb-hostcontroller
No comments:
Post a Comment