@property (nonatomic,strong) M8MouseSdk *m8Sdk;
@property (nonatomic,assign) AiMouseProductType productType;
@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调
+@property (nonatomic, assign)NSInteger retryTimes;//重试次数
//connectType 返回为0代表蓝牙,为1代表usb
@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(int connectType, BOOL connected);/// 设备连接状态回调
- (instancetype)init{
self = [super init];
if (self) {
- self.version = @"MouseSdk_v2025052202";
+ self.version = @"MouseSdk_v2025052401";
}
return self;
}
// [self.hid close];
}
else{
- [self.ble open];
- [self.hid open];
+// [self.ble open];
+ if (self.retryTimes < 5) {
+ [self.hid open];
+ }
+ else{
+ if (self.hid.isOpen) {
+ if (self.hid.productType == AiMouseProductType_n) {
+ [self.nSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ else if (self.hid.productType == AiMouseProductType_tlw) {
+ [self.tlwSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ else if (self.hid.productType == AiMouseProductType_frk){
+ [self.frkSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ else if (self.hid.productType == AiMouseProductType_keyboard){
+ [self.kbSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ else if (self.hid.productType == AiMouseProductType_m8){
+ [self.m8Sdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ }
+ else{
+ [self.hid open];
+ }
+ }
}
};
}
///获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished{
[self showLog:@"app主动获取设备信息 。。。"];
+ __weak typeof(self) ws = self;
if(self.productType == AiMouseProductType_keyboard){
- [self.kbSdk getDeviceInfo:finished];
+ [self.kbSdk getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
+ finished(deviceInfo,error);
+ [ws checkedDisconect:error];
+ }];
}
else if(self.productType == AiMouseProductType_n){
- [self.nSdk getDeviceInfo:finished];
+ [self.nSdk getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
+ finished(deviceInfo,error);
+ [ws checkedDisconect:error];
+ }];
}else if (self.productType == AiMouseProductType_tlw) {
- [self.tlwSdk getFullDeviceInfo:finished];
+ [self.tlwSdk getFullDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
+ finished(deviceInfo,error);
+ [ws checkedDisconect:error];
+ }];
}
else if (self.productType == AiMouseProductType_m8) {
- [self.m8Sdk getFullDeviceInfo:finished];
+ [self.m8Sdk getFullDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
+ finished(deviceInfo,error);
+ [ws checkedDisconect:error];
+ }];
}
else{
- [self.frkSdk getDeviceInfo:finished];
+ [self.frkSdk getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
+ finished(deviceInfo,error);
+ [ws checkedDisconect:error];
+ }];
+ }
+}
+
+- (void)checkedDisconect:(NSError *)error{
+ if (error == nil) {
+ self.retryTimes = 0;
}
+ self.retryTimes++;
}
/// 获取信号强度
uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey));
uint32_t page = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
uint32_t usage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
-// [[HidManager shared] showLog: [NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef]];
+ [[HidManager shared] showLog: [NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef]];
if(vid == 3468 && pid == 786){
if (page == 65282 && usage == 2) {
[HidManager shared].productType = AiMouseProductType_m8;
return;
}else{
[[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true) ;
}
}
return;
}else{
[[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true) ;
}
return;
return;
}else{
[[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true) ;
}
}
return;
}else{
[[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true) ;
}
}
[[HidManager shared] showLog:@"打开设备信息成功!"];
if([HidManager shared].voiceCha != nil){
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true);
}
}
[[HidManager shared] showLog:@"打开设备voice成功!"];
if([HidManager shared].deviceInfoCha != nil){
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true);
}
}
[[HidManager shared] showLog:@"打开设备voice成功!"];
if([HidManager shared].deviceInfoCha != nil){
+ [HidManager shared].isOpen = true;
[HidManager shared].connectStateCallBack(true);
}
}
[HidManager shared].deviceInfoCha = nil;
[HidManager shared].voiceCha = nil;
// [HidManager shared].hidManager = nil;
+ [HidManager shared].isOpen = false;
[HidManager shared].connectStateCallBack(false) ;
}
}
IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
}
-//- (void)open{
-// if(self.hidManager == nil){
-// self.deviceInfoCha = nil;
-// self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
-// IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
-// IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
-// if (ret != kIOReturnSuccess) {
-// [self showLog:(@"打开hid失败!")];
-// }else{
-// [self showLog:(@"打开hid成功!")];
-// }
-// NSMutableDictionary* dict= [NSMutableDictionary dictionary];
-// IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
-// IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
-// }
-//
-// if (self.deviceInfoCha != nil) {
-// [HidManager shared].connectStateCallBack(true);
-// return;
-// }
-//
-// // 在需要使用的地方调用
-// CFSetRef devices = IOHIDManagerCopyDevices(self.hidManager);
-// if (devices) {
-// [self showLog:[NSString stringWithFormat:@"找到 %ld 个HID设备", CFSetGetCount(devices)]];
-// // 使用C函数遍历集合
-// CFSetApplyFunction(devices, deviceApplier, NULL);
-// CFRelease(devices);
-// } else {
-// [self showLog:@"未找到HID设备"];
-// }
-//}
-
-// 定义C函数作为集合应用函数
-void deviceApplier(const void *value, void *context) {
- IOHIDDeviceRef inIOHIDDeviceRef = (IOHIDDeviceRef)value;
-
- if ([DeviceManger shared].isConneted) {
- // [[HidManager shared] showLog:(@"当前处于连接状态,对监听到有USB 插入 的事件不做处理")];
- return;
- }
- uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey));
- uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey));
- uint32_t page = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
- uint32_t usage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
- [[HidManager shared] showLog: [NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef]];
- if(vid == 3468 && pid == 786){
- if (page == 65282 && usage == 2) {
- [HidManager shared].productType = AiMouseProductType_m8;
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- unsigned char InputBuffer[64];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
-
- IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [[HidManager shared] showLog:@"打开设备成功!"];
- [HidManager shared].connectStateCallBack(true) ;
+- (BOOL)close{
+ if (self.hidManager != nil) {
+ if (self.deviceInfoCha != nil) {
+ // 取消所有报告回调
+ IOReturn ret = IOHIDDeviceClose(self.deviceInfoCha, kIOHIDOptionsTypeNone);
+ if (ret != kIOReturnSuccess) {
+ [self showLog:(@"关闭device失败!")];
+ return false;
}
}
- // else if (page == 1 && usage == 2){
- // [HidManager shared].productType = AiMouseProductType_m8;
- // [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- // unsigned char InputBuffer[64];
- // IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
- //
- // IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- // if (result != kIOReturnSuccess) {
- // [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
- // [[AiDeviceAuthManager shared] showHidAlert];
- // return;
- // }else{
- // [[HidManager shared] showLog:@"打开设备成功!"];
- // // [HidManager shared].connectStateCallBack(true) ;
- // }
- // }
- return;
- }
- if((vid == 0x248a && pid == 0x6092)){
- [HidManager shared].productType = AiMouseProductType_keyboard;
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- unsigned char InputBuffer[64];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
-
- IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [[HidManager shared] showLog:@"打开设备成功!"];
- [HidManager shared].connectStateCallBack(true) ;
+ if (self.voiceCha != nil) {
+ IOReturn ret = IOHIDDeviceClose(self.voiceCha, kIOHIDOptionsTypeNone);
+ if (ret != kIOReturnSuccess) {
+ [self showLog:(@"关闭voice失败!")];
+ return false;
+ }
}
- return;
- }
- else if((vid == 0x1ea7 && pid == 0x0064)
- || (vid == 0x1ea7 && pid == 0x0066)
- || (vid == 0x32c2 && pid == 0x0066)
- ){
- [HidManager shared].productType = AiMouseProductType_n;
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- unsigned char InputBuffer[64];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
- IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [[HidManager shared] showLog:@"打开设备成功!"];
- [HidManager shared].connectStateCallBack(true) ;
+ IOReturn ret = IOHIDManagerClose(self.hidManager, 0);
+ if (ret != kIOReturnSuccess) {
+ [self showLog:(@"关闭hid失败!")];
+ return false;
}
- }
- else if(vid == 0x10D6 && pid == 0xB008){
- [HidManager shared].productType = AiMouseProductType_tlw;
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- unsigned char InputBuffer[64];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
- IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [[HidManager shared] showLog:@"打开设备成功!"];
- [HidManager shared].connectStateCallBack(true) ;
- }
- }
- else if(vid == 0x6675 && pid == 0x8601){
- [HidManager shared].productType = AiMouseProductType_frk;
- AiMouse.shared.tlw2 = false;
- if(page == 1 && usage == 0){
- unsigned char InputBuffer[128];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
- IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备信息失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
-
- [[HidManager shared] showLog:@"打开设备信息成功!"];
- if([HidManager shared].voiceCha != nil){
- [HidManager shared].connectStateCallBack(true);
- }
- }
- }
- else if(page == 65283 && usage == 0){
- unsigned char InputBuffer[128];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
-
- IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备voice失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [HidManager shared].voiceCha = inIOHIDDeviceRef;
-
- [[HidManager shared] showLog:@"打开设备voice成功!"];
- if([HidManager shared].deviceInfoCha != nil){
- [HidManager shared].connectStateCallBack(true);
- }
- }
- }
- }
- else if(vid == 0x6675 && pid == 0x8602){
- [HidManager shared].productType = AiMouseProductType_frk;
- AiMouse.shared.tlw2 = true;
- [HidManager shared].tlw2Receiver = [[Tlw2ReceiveManager alloc] init];
- if(page == 65519 && usage == 0){
- unsigned char InputBuffer[128];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
-
- IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- if (result != kIOReturnSuccess) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备voice失败! %d",result]];
- [[AiDeviceAuthManager shared] showHidAlert];
- return;
- }else{
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
-
- [[HidManager shared] showLog:@"打开设备voice成功!"];
- if([HidManager shared].deviceInfoCha != nil){
- [HidManager shared].connectStateCallBack(true);
- }
- }
- }
- }
-}
-
-
-
-- (void)close{
- if (self.hidManager == nil) {
- return;
- }
- [self showLog:@"关闭hid"];
-
- IOReturn ret = IOHIDManagerClose(self.hidManager, 0);
- self.hidManager = nil;
-
- if (ret != kIOReturnSuccess) {
- [self showLog:(@"关闭hid失败!")];
- }else{
- [self showLog:(@"关闭hid成功!")];
self.deviceInfoCha = nil;
self.voiceCha = nil;
self.hidManager = nil;
}
+ return true;
}
/// 发送数据给设备
-(void)writeToDevice:(NSData *)data{