hid调试 hid
authorliangzhenying <867174124@qq.com>
Wed, 21 Aug 2024 04:09:18 +0000 (12:09 +0800)
committerliangzhenying <867174124@qq.com>
Wed, 21 Aug 2024 04:09:18 +0000 (12:09 +0800)
MouseSdk/MouseSdk.xcodeproj/project.pbxproj
MouseSdk/MouseSdk/HidManager.m
MouseSdk/MouseSdk/Info.plist
MouseSdk/MouseSdk/MouseSdk.entitlements
MouseSdk/MouseSdk/usbHid.h [new file with mode: 0644]
MouseSdk/MouseSdk/usbHid.m [new file with mode: 0644]

index c02e0db..fd8f697 100644 (file)
@@ -16,6 +16,7 @@
                180E7D1B2BD9005D008558E8 /* liblzma.5.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 18CB080B2BA401D0005F5854 /* liblzma.5.tbd */; };
                182B548D2BB7EC9B009F53B6 /* DeviceManger.m in Sources */ = {isa = PBXBuildFile; fileRef = 182B548C2BB7EC9B009F53B6 /* DeviceManger.m */; };
                1867E5A92BB2706F008FC5FB /* HidManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 1867E5A82BB2706F008FC5FB /* HidManager.m */; };
+               18986DCC2C75974F00CFF59B /* usbHid.m in Sources */ = {isa = PBXBuildFile; fileRef = 18986DCA2C75974F00CFF59B /* usbHid.m */; };
                18BF1DCF2B81AB7C00B8D90F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1DCE2B81AB7C00B8D90F /* AppDelegate.m */; };
                18BF1DD22B81AB7C00B8D90F /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1DD12B81AB7C00B8D90F /* ViewController.m */; };
                18BF1DD42B81AB7F00B8D90F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18BF1DD32B81AB7F00B8D90F /* Assets.xcassets */; };
@@ -68,6 +69,8 @@
                182B548C2BB7EC9B009F53B6 /* DeviceManger.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DeviceManger.m; sourceTree = "<group>"; };
                1867E5A72BB2706F008FC5FB /* HidManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HidManager.h; sourceTree = "<group>"; };
                1867E5A82BB2706F008FC5FB /* HidManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HidManager.m; sourceTree = "<group>"; };
+               18986DCA2C75974F00CFF59B /* usbHid.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = usbHid.m; sourceTree = "<group>"; };
+               18986DCB2C75974F00CFF59B /* usbHid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = usbHid.h; sourceTree = "<group>"; };
                18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MouseSdk.app; sourceTree = BUILT_PRODUCTS_DIR; };
                18BF1DCD2B81AB7C00B8D90F /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
                18BF1DCE2B81AB7C00B8D90F /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -76,7 +79,6 @@
                18BF1DD32B81AB7F00B8D90F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
                18BF1DD62B81AB7F00B8D90F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
                18BF1DD82B81AB7F00B8D90F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
-               18BF1DDA2B81AB7F00B8D90F /* MouseSdk.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MouseSdk.entitlements; sourceTree = "<group>"; };
                18BF1DDF2B81AB8000B8D90F /* MouseSdkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MouseSdkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
                18BF1DE32B81AB8000B8D90F /* MouseSdkTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkTests.m; sourceTree = "<group>"; };
                18BF1DE92B81AB8000B8D90F /* MouseSdkUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MouseSdkUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
                                18BF1E212B83066200B8D90F /* BleManager.m */,
                                1867E5A72BB2706F008FC5FB /* HidManager.h */,
                                1867E5A82BB2706F008FC5FB /* HidManager.m */,
+                               18986DCB2C75974F00CFF59B /* usbHid.h */,
+                               18986DCA2C75974F00CFF59B /* usbHid.m */,
                                182B548B2BB7EC9B009F53B6 /* DeviceManger.h */,
                                182B548C2BB7EC9B009F53B6 /* DeviceManger.m */,
                                18BF1DCD2B81AB7C00B8D90F /* AppDelegate.h */,
                                18BF1DD32B81AB7F00B8D90F /* Assets.xcassets */,
                                18BF1DD52B81AB7F00B8D90F /* Main.storyboard */,
                                18BF1DD82B81AB7F00B8D90F /* main.m */,
-                               18BF1DDA2B81AB7F00B8D90F /* MouseSdk.entitlements */,
                                18BF1E032B81E18700B8D90F /* MouseSdk-Bridging-Header.h */,
                        );
                        path = MouseSdk;
                        isa = PBXSourcesBuildPhase;
                        buildActionMask = 2147483647;
                        files = (
+                               18986DCC2C75974F00CFF59B /* usbHid.m in Sources */,
                                18F3235D2B8ED78300CABA2E /* ScanCell.m in Sources */,
                                18BF1DD22B81AB7C00B8D90F /* ViewController.m in Sources */,
                                18BF1DD92B81AB7F00B8D90F /* main.m in Sources */,
                                ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
                                CLANG_ENABLE_MODULES = YES;
                                CODE_SIGN_ENTITLEMENTS = MouseSdk/MouseSdk.entitlements;
+                               "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
                                CODE_SIGN_STYLE = Automatic;
                                COMBINE_HIDPI_IMAGES = YES;
                                CURRENT_PROJECT_VERSION = 1;
+                               DEVELOPMENT_TEAM = M33X987285;
                                FRAMEWORK_SEARCH_PATHS = "$(inherited)";
                                GENERATE_INFOPLIST_FILE = YES;
                                HEADER_SEARCH_PATHS = (
                                ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
                                CLANG_ENABLE_MODULES = YES;
                                CODE_SIGN_ENTITLEMENTS = MouseSdk/MouseSdk.entitlements;
+                               "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
                                CODE_SIGN_STYLE = Automatic;
                                COMBINE_HIDPI_IMAGES = YES;
                                CURRENT_PROJECT_VERSION = 1;
+                               DEVELOPMENT_TEAM = M33X987285;
                                FRAMEWORK_SEARCH_PATHS = "$(inherited)";
                                GENERATE_INFOPLIST_FILE = YES;
                                HEADER_SEARCH_PATHS = (
index b0667ac..3408cd4 100644 (file)
@@ -8,7 +8,6 @@
 #import "HidManager.h"
 #include <IOKit/hid/IOHIDLib.h>
 #include <IOKit/hid/IOHIDKeys.h>
-#import "Config.h"
 
 //供应商id
 const long vendorID = 0x10D6;
@@ -19,7 +18,7 @@ const long productID = 0xB008;
 //const long vendorID = 0x6675;
 //const long productID = 0x8601;
 
-@interface HidManager ()<AiMouseDelegate>
+@interface HidManager ()
 
 @property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理
 @property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道
@@ -82,12 +81,9 @@ const long productID = 0xB008;
 /// 匹配到设备回调
 static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
     [[HidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]];
-    uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
-    uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
 //    if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
         //设备信息
         [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
-        [[HidManager shared] connectDevice];
         //注册消息回调
         unsigned char InputBuffer[128];
         IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
@@ -120,21 +116,21 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
 /// 设备移除回调
 static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
     [[HidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]];
-    uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
-    uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
-
-    if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
-        [HidManager shared].deviceInfoCha = nil;
-//        [HidManager shared].aiCha = nil;
-        [[AiMouse shared] didDisconnect];
-    }
+//    uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
+//    uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
+//
+//    if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
+//        [HidManager shared].deviceInfoCha = nil;
+////        [HidManager shared].aiCha = nil;
+//        [[AiMouse shared] didDisconnect];
+//    }
 }
 
 static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){
 //    [[HidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[AiMouseTools dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]];
     
-    NSInteger len = get_data_len(report, reportLength);
-    NSData *data = [[NSData alloc] initWithBytes:report length:len];
+//    NSInteger len = get_data_len(report, 64);
+    NSData *data = [[NSData alloc] initWithBytes:report length:64];
     [[HidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理后 %@",data]];
     
 //    uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey));
@@ -142,138 +138,16 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
 //
 //    if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
 //        [[AiMouse shared] sendDeviceDataToSdk:data];
-//        
+//
 //    }
 //    else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
 //        [[AiMouse shared] sendAiDataToSdk:data];
 //    }
 }
 
-#pragma mark - AiDelegate
-/// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备
-/// - Parameter data: 数据
--(void)aiMouseSendDeviceInfoData:(NSData *)data{
-    [self sendData:data toDevice:self.deviceInfoCha];
-}
 
-/// 鼠标按键指令回调
-/// - Parameter data: 数据。
-/// 前  单击0x06, 按下0x13(开始录音)抬起0x14(结束录音)
-/// 中  单击0x03, 按下0x11(开始录音)抬起0x12(结束录音)
-/// 后  第一次单击0x01, 第二次单击0x02(结束录音) , 按下0x09(开始录音)抬起0x10(结束录音)
-/// 其他未定义
-- (void)aiMouseSendCmdData:(NSData *)data{
-    uint8 cmd;
-    [data getBytes:&cmd length:1];
-    
-    if(cmd == 0x13 || cmd == 0x11 || cmd == 0x01 ||cmd == 0x09){
-        [self showLog:@"开始录音"];
-        self.recordData = [[NSMutableData alloc] init];
-    }
-    else if(cmd == 0x14 || cmd == 0x12 || cmd == 0x02 ||cmd == 0x10){
-        [self showLog:@"结束录音"];
-        [self finishRecored:self.recordData];
-    }
-}
-/// 录音回调
-/// - Parameter data: pcm格式语音数据
-- (void)aiMouseSendRecordData:(NSData *)data{
-    [self.recordData appendData:data];
-}
-/// 输出日志
-- (void)aiMouseAddLog:(NSString *)log {
-//    [self showLog:log];
-}
-
--(void)hidDataError{
-    [self close];
-    [self open];
-}
-
-// 连接状态改变
-- (void)aiMouseConnectStateDidChange:(BOOL)connected{
-    [HidManager shared].connectStateCallBack(connected);
-}
-
-#pragma mark - public
-/// 获取设备信息
-- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{
-    [[AiMouse shared] getDeviceInfo:completation];
-}
-/// setDPI
-- (void)setDPI:(int)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{
-    [[AiMouse shared] setDPI:value completation:finished];
-}
-/// 打开录音
-- (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished{
-    [[AiMouse shared] openRecord:finished];
-}
-/// 停止录音
-- (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished{
-    [[AiMouse shared] closeRecord:finished];
-}
-
-#pragma mark - other
--(void)connectDevice{
-    //设置代理
-    [AiMouse shared].delegate = self;
-    //切换当前连接类型
-    [[AiMouse shared] switchConnect:AiMouseConnectType_hid];
-}
-
-/// 发送数据给设备
--(void)sendData:(NSData *)data toDevice:(IOHIDDeviceRef)device{
-    if(device == nil){
-        [self showLog:@"请先连上设备"];
-        return;
-    }
-    
-    IOReturn ret = IOHIDDeviceSetReport(device, kIOHIDReportTypeOutput, 0, data.bytes, data.length);
-    if (ret != kIOReturnSuccess) {
-        [self showLog:[NSString stringWithFormat:@"数据发送失败! code = %d",ret]];
-    }else{
-        [self showLog:@"数据发送成功!"];
-    }
-}
-
-//将录音数据保存
-- (void)finishRecored:(NSData *)data{
-    NSString *doc = NSTemporaryDirectory();
-    NSString *fullPath = [NSString stringWithFormat:@"%@1.pcm",doc];
-    NSError *err;
-    [data writeToFile:fullPath options:NSDataWritingAtomic error:&err];
-    if (err) {
-        [self showLog:[NSString stringWithFormat:@"录音保存失败 %@",err.description]];
-    }
-    else{
-        [self showLog:[NSString stringWithFormat:@"录音保存成功 path = %@",fullPath]];
-    }
-}
 - (void)showLog:(NSString *)log{
     NSLog(@"%@",log);
 }
 
-
-/// 实际到数据长度
-static inline int get_data_len(uint8_t *buf, u_long len) {
-    if (len <= 0 ) return 0;
-    int url_len = 0;
-    while (buf[url_len] != 0x00) url_len++;
-    return url_len;
-}
-
-/// 获取对应属性值
-static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey) {
-    uint32_t outValue = 0;
-    if (inIOHIDDeviceRef != NULL) {
-        CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty(inIOHIDDeviceRef, inKey);
-        if (tCFTypeRef != NULL && (CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef))  ) {
-            Boolean result = CFNumberGetValue((CFNumberRef) tCFTypeRef, kCFNumberSInt32Type, &outValue);
-            if (!result) {
-                [[HidManager shared] showLog:[NSString stringWithFormat:@"get %@ failed.", inKey]];
-            }
-        }
-    }
-    return outValue;
-}
 @end
index 6a6654d..dc2fcd3 100644 (file)
@@ -7,5 +7,7 @@
                <key>NSAllowsArbitraryLoads</key>
                <true/>
        </dict>
+       <key>NSHumanReadableCopyright</key>
+       <string>Copyright © 2019年 mac. All rights reserved.</string>
 </dict>
 </plist>
index 6fe1208..362f9a5 100644 (file)
@@ -4,8 +4,6 @@
 <dict>
        <key>com.apple.security.app-sandbox</key>
        <true/>
-       <key>com.apple.security.device.audio-input</key>
-       <true/>
        <key>com.apple.security.device.bluetooth</key>
        <true/>
        <key>com.apple.security.device.usb</key>
diff --git a/MouseSdk/MouseSdk/usbHid.h b/MouseSdk/MouseSdk/usbHid.h
new file mode 100644 (file)
index 0000000..ed164e6
--- /dev/null
@@ -0,0 +1,60 @@
+//
+//  usbHid.h
+//  MacOSHid
+//
+//  Created by Smile on 2019/3/21.
+//  Copyright © 2019年 mac. All rights reserved.
+
+/*
+ ⚠️注意usbHid.m
+ ID 信息需要改成通讯设备的ID
+ 查询-> 导航🍎图标 -> 概览 -> 系统报告 -> USB  -> 对应设备信息里面
+ const long vendorID = 0x0483;
+ const long productID = 0x5703;
+ 
+ 如有帮助不解敬请关注简书:隐身人
+ https://www.jianshu.com/u/86cc50fb916f
+ */
+
+#import <Foundation/Foundation.h>
+#include <IOKit/hid/IOHIDLib.h>
+#include <IOKit/hid/IOHIDKeys.h>
+
+@protocol UsbHidDelegate <NSObject>
+@optional
+
+//收到数据返回 recvData(具体数据) reportLength(数据长度)
+- (void)usbhidDidRecvData:(uint8_t*)recvData length:(CFIndex)reportLength;
+
+//设备插入/设备拔出
+- (void)usbhidDidMatch;
+- (void)usbhidDidRemove;
+@end
+
+@interface usbHid : NSObject
+{
+    IOHIDManagerRef managerRef;
+    IOHIDDeviceRef deviceRef;
+}
+
++ (usbHid *)sharedManager;
+
+//连接设备
+- (void)connectHID;
+
+//发送数据
+- (void)sendData:(unsigned char *)outbuffer;
+
+//数据返回代理
+@property(nonatomic,strong)id<UsbHidDelegate> delegate;
+
+//得到当前接口设备管理对象/更新新接口设备管理对象
+- (IOHIDManagerRef)getManageRef;
+- (void)setManageRef:(IOHIDManagerRef)ref;
+
+//得到当前设备/更新新的设备
+- (IOHIDDeviceRef)getDeviceRef;
+- (void)setDeviceRef:(IOHIDDeviceRef)ref;
+
+
+@end
diff --git a/MouseSdk/MouseSdk/usbHid.m b/MouseSdk/MouseSdk/usbHid.m
new file mode 100644 (file)
index 0000000..6444116
--- /dev/null
@@ -0,0 +1,157 @@
+//
+//  usbHid.m
+//  MacOSHid
+//
+//  Created by Smile on 2019/3/21.
+//  Copyright © 2019年 mac. All rights reserved.
+//
+
+#import "usbHid.h"
+
+@implementation usbHid
+
+static usbHid *_sharedManager = nil;
+
+@synthesize delegate;
+
++(usbHid *)sharedManager {
+    @synchronized( [usbHid class] ){
+        if(!_sharedManager)
+            _sharedManager = [[self alloc] init];
+        return _sharedManager;
+    }
+    return nil;
+}
+
++(id)alloc {
+    @synchronized ([usbHid class]){
+        NSAssert(_sharedManager == nil,
+                 @"Attempted to allocated a second instance");
+        _sharedManager = [super alloc];
+        return _sharedManager;
+    }
+    return nil;
+}
+
+#pragma mark - 初始化
+- (id)init {
+    self = [super init];
+    if (self) {
+        managerRef = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+        IOHIDManagerScheduleWithRunLoop(managerRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
+        IOReturn ret = IOHIDManagerOpen(managerRef, 0L);
+        if (ret != kIOReturnSuccess) {
+            NSLog(@"打开设备失败!");
+            return self;
+        }else{
+            NSLog(@"打开设备成功!");
+        }
+        
+        const long vendorID = 0x10D6;
+        const long productID = 0xB008;
+        NSMutableDictionary* dict= [NSMutableDictionary dictionary];
+        [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
+        [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
+        IOHIDManagerSetDeviceMatching(managerRef, (__bridge CFMutableDictionaryRef)dict);
+        
+        IOHIDManagerRegisterDeviceMatchingCallback(managerRef, &Handle_DeviceMatchingCallback, NULL);
+        IOHIDManagerRegisterDeviceRemovalCallback(managerRef, &Handle_DeviceRemovalCallback, NULL);
+        
+        NSSet* allDevices = (__bridge NSSet*)(IOHIDManagerCopyDevices(managerRef));
+        NSArray* deviceRefs = [allDevices allObjects];
+        if (deviceRefs.count==0) {
+            NSLog(@"初始化 - 连接设备  设备列表  -- %lu",(unsigned long)deviceRefs.count);
+        }
+    }
+    return self;
+}
+
+#pragma mark - 注册 Hid 方法
+static void Handle_DeviceMatchingCallback(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
+    [[usbHid sharedManager] setDeviceRef:inIOHIDDeviceRef];
+    char *inputbuffer = malloc(64);
+    IOHIDDeviceRegisterInputReportCallback([[usbHid sharedManager]getDeviceRef], (uint8_t*)inputbuffer, 64, inputCallback, NULL);
+    NSLog(@"%p \n设备插入,现在usb设备数量:%ld",(void *)inIOHIDDeviceRef,USBDeviceCount(inSender));
+    [[[usbHid sharedManager] delegate] usbhidDidMatch];
+}
+
+static void Handle_DeviceRemovalCallback(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
+    [[usbHid sharedManager] setDeviceRef:nil];
+    NSLog(@"%p \n设备拔出,现在usb设备数量:%ld",(void *)inIOHIDDeviceRef,USBDeviceCount(inSender));
+    [[[usbHid sharedManager] delegate] usbhidDidRemove];
+}
+
+static long USBDeviceCount(IOHIDManagerRef HIDManager){
+    CFSetRef devSet = IOHIDManagerCopyDevices(HIDManager);
+    if(devSet)
+        return CFSetGetCount(devSet);
+    return 0;
+}
+
+#pragma mark - 获取设备列表/连接设备
+- (void)connectHID {
+    NSSet* allDevices = (__bridge NSSet*)(IOHIDManagerCopyDevices(managerRef));
+    NSArray* deviceRefs = [allDevices allObjects];
+    
+    // 这里默认连接 设备列表第 一 个
+    deviceRef = (deviceRefs.count)?(__bridge IOHIDDeviceRef)[deviceRefs objectAtIndex:0]:nil;
+    
+    NSLog(@"连接设备  设备列表  -- %lu",(unsigned long)deviceRefs.count);
+}
+
+
+#pragma mark - 发送数据
+- (void)sendData:(unsigned char *)outbuffer {
+    
+//    NSData *data = [[NSData alloc] initWithBytes:outbuffer length:7];
+//    NSLog(@"发送数据内容: %@",data);
+    
+    if (!deviceRef) {
+        NSLog(@"没有找到设备 - ");
+        return ;
+    }
+    IOReturn ret = IOHIDDeviceSetReport(deviceRef, kIOHIDReportTypeOutput, 0, (uint8_t*)outbuffer, sizeof(outbuffer));
+    if (ret != kIOReturnSuccess) {
+        NSLog(@"发送数据失败!");
+    }else{
+        NSLog(@"发送数据成功!");
+    }
+}
+
+#pragma mark - 接收数据
+static void inputCallback(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength) {
+    
+    [[[usbHid sharedManager] delegate] usbhidDidRecvData:report length:reportLength];
+    
+}
+
+#pragma mark - Setter and Getter
+
+- (IOHIDManagerRef)getManageRef {
+    return managerRef;
+}
+
+- (void)setManageRef:(IOHIDManagerRef)ref {
+    managerRef = ref;
+}
+
+- (IOHIDDeviceRef)getDeviceRef {
+    return deviceRef;
+}
+
+- (void)setDeviceRef:(IOHIDDeviceRef)ref {
+    deviceRef = ref;
+}
+
+- (void)dealloc {
+    IOReturn ret = IOHIDDeviceClose(deviceRef, 0L);
+    if (ret == kIOReturnSuccess) {
+        deviceRef = nil;
+    }
+    ret = IOHIDManagerClose(managerRef, 0L);
+    if (ret == kIOReturnSuccess) {
+        managerRef = nil;
+    }
+}
+
+@end