コード例 #1
0
        public override HIDReport ReadDefault()
        {
            if (IsOpen == false)
            {
                OpenDevice();
            }



            //do this only once
            if (!DeviceGCHandle.IsAllocated)
            {
                try {
                    DeviceGCHandle = GCHandle.Alloc(this);

                    // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include
                    // the device identifier as the context variable, so we can identify it and figure out the device later.


                    Native.IOHIDDeviceRegisterInputValueCallback(__deviceHandle, DeviceValueReceived, GCHandle.ToIntPtr(DeviceGCHandle));
                    //_registeredToInputReport=true;

                    Native.IOHIDDeviceScheduleWithRunLoop(__deviceHandle, RunLoop, Native.RunLoopModeDefault);
                    //_scheduleWithRunLoop=true;
                } catch (Exception e) {
                    UnityEngine.Debug.LogException(e);
                }
            }

            return(__lastHIDReport);
        }
コード例 #2
0
        void RemoveDevice(IOHIDDeviceRef deviceRef)
        {
            if (deviceRef == IntPtr.Zero)
            {
                Debug.LogWarning("IOHIDeviceRef equal to IntPtr.Zero");
                return;
            }


            int product_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductIDKey)))).ToInteger();



            int vendor_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDVendorIDKey)))).ToInteger();


            int    location  = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)))).ToInteger();
            string transport = (new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDTransportKey)))).ToString();

            string path = String.Format("{0:s}_{1,4:X}_{2,4:X}_{3:X}",
                                        transport, vendor_id, product_id, location);          //"%s_%04hx_%04hx_%x"


            lock (syncRoot) {
                if (!__Generics.ContainsKey(path))
                {
                    return;
                }

                HIDDevice hidDevice = __Generics [path];


                Generics.Remove(path);

                Debug.Log("Device " + hidDevice.index + " PID:" + hidDevice.PID + " VID:" + hidDevice.VID + " Disconnected");
            }


            Debug.Log("Try to unshedule,unregister and close device");
            Native.IOHIDDeviceUnscheduleFromRunLoop(deviceRef, RunLoop, InputLoopMode);
            Native.IOHIDDeviceRegisterInputValueCallback(deviceRef, IntPtr.Zero, IntPtr.Zero);
            Native.IOHIDDeviceRegisterRemovalCallback(deviceRef, null, IntPtr.Zero);

            Native.IOHIDDeviceClose(deviceRef, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone);



            this.DeviceDisconnectEvent(this, new DeviceEventArgs <string>(path));
        }