Esempio n. 1
0
        private bool WriteData(byte[] data, int timeout)
        {
            var buffer = CreateOutputBuffer();


            Array.Copy(data, 1, buffer, 0, data.Length - 1);

            if (__OutputBufferPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(__OutputBufferPtr);
            }


            __OutputBufferPtr = Marshal.AllocHGlobal(buffer.Length);
            Marshal.Copy(buffer, 0, __OutputBufferPtr, buffer.Length);



            if (timeout > 0)
            {
                if (!DeviceGCHandle.IsAllocated)
                {
                    DeviceGCHandle = GCHandle.Alloc(this);



                    // Schedule the device on the current run loop in case it isn't already scheduled
                    //Native.IOHIDDeviceScheduleWithRunLoop (__deviceHandle, Native.CFRunLoopGetCurrent (), Native.RunLoopModeDefault);
                }


                Native.IOHIDDeviceSetReportWithCallback(
                    __deviceHandle,
                    Native.IOHIDReportType.kIOHIDReportTypeOutput,
                    data[0],
                    __OutputBufferPtr,
                    buffer.Length,
                    timeout,
                    OutputReportCallback,
                    GCHandle.ToIntPtr(DeviceGCHandle));

                return(OutputAutoResetEvent.WaitOne(timeout));
            }
            else
            {
                try
                {
                    return(IOReturn.kIOReturnSuccess == Native.IOHIDDeviceSetReport(__deviceHandle,
                                                                                    Native.IOHIDReportType.kIOHIDReportTypeOutput,
                                                                                    data[0],
                                                                                    __OutputBufferPtr,
                                                                                    buffer.Length));
                }
                catch (Exception ex) {
                    UnityEngine.Debug.LogException(ex);
                    return(false);
                }
            }
        }