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)); }