}
- (void)switchConnect:(AiMouseConnectType)type isConnected:(BOOL)isConneted{
- _connected = isConneted;
- if (isConneted) {
+ if(isConneted ){
+ _connected = true;
_connectType = type;
[self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
+ [self aiConnectManager:self.connectManager connectStateDidChange:true];
+ if (type == AiMouseConnectType_hid ) {
+ [self.recordManager checkPermissions];
+ }
}
- [self aiConnectManager:self.connectManager connectStateDidChange:isConneted];
-
- if (type == AiMouseConnectType_hid && isConneted) {
- [self.recordManager checkPermissions];
+ else{
+ if (self.connected) {
+ _connected = false;
+ [self aiConnectManager:self.connectManager connectStateDidChange:false];
+ }
}
}
//开始鉴权
}
- (void)showLog:(NSString *)log{
if (self.delegate && [self.delegate respondsToSelector:@selector(aiMouseAddLog:)]) {
- [self.delegate aiMouseAddLog:log];
+ [self.delegate aiMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk][frk]%@",log]];
}
if(self.logLevel == AiMouseLogLevel_info){
- NSLog(@"[AiMouseSdk]%@",log);
+ NSLog(@"[AiMouseSdk][frk]%@",log);
}
}
- (instancetype)init{
self = [super init];
if (self) {
+ [self setupSDK];
[self setupBle];
[self setupHid];
[self setupTimer];
return self;
}
#pragma mark ------ 设备初始化
+- (void)setupSDK{
+ self.tlwSdk = [[TlwMouse alloc] init];
+ [self.tlwSdk setDelegate:self];
+ [self.tlwSdk setLogLevel:(TlwMouseLogLevel_info)];
+
+ self.frkSdk = [AiMouse shared];
+ [self.frkSdk setDelegate:self];
+ [self.frkSdk setLogLevel:AiMouseLogLevel_info];
+}
+
+
- (void)setupTimer{
[TimerManager shared].timerCallBack = ^{
// [self showLog:@"timer action。。。"];
__weak typeof(self) ws = self;
self.hid.connectStateCallBack = ^(BOOL connected) {
- if (ws.hid.productType == AiMouseProductType_tlw) {
- ws.productType = AiMouseProductType_tlw;
- ws.tlwSdk = [[TlwMouse alloc] init];
- [ws.tlwSdk setDelegate:ws];
- [ws.tlwSdk setLogLevel:(TlwMouseLogLevel_info)];
- [ws.tlwSdk switchConnect:AiMouseConnectType_hid isConnected:connected];
+ if (connected) {
+ ws.productType = ws.hid.productType;
+ if (ws.hid.productType == AiMouseProductType_tlw) {
+ [ws.tlwSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
+ else{
+ [ws.frkSdk switchConnect:AiMouseConnectType_hid isConnected:true];
+ }
}
else{
- ws.productType = AiMouseProductType_frk;
- ws.frkSdk = [AiMouse shared];
- [ws.frkSdk setDelegate:ws];
- [ws.frkSdk setLogLevel:AiMouseLogLevel_info];
- [ws.frkSdk switchConnect:AiMouseConnectType_hid isConnected:connected];
+ if (ws.ble.productType == AiMouseProductType_tlw) {
+ [ws.tlwSdk switchConnect:AiMouseConnectType_hid isConnected:false];
+ }
+ else{
+ [ws.frkSdk switchConnect:AiMouseConnectType_hid isConnected:false];
+ }
}
-
};
self.hid.receiveInfoDataCallBack = ^(NSData *data) {
if (ws.productType == AiMouseProductType_tlw) {
self.ble.connectStateCallBack = ^(NSString *name,BOOL connected) {
if (connected) {
[ws.hid close];
- }
- if (ws.ble.productType == AiMouseProductType_tlw) {
- ws.productType = AiMouseProductType_tlw;
- ws.tlwSdk = [[TlwMouse alloc] init];
- [ws.tlwSdk setDelegate:ws];
- [ws.tlwSdk setLogLevel:(TlwMouseLogLevel_info)];
- [ws.tlwSdk switchConnect:AiMouseConnectType_bluetooth isConnected:connected];
+ ws.productType = ws.ble.productType;
+
+ if (ws.ble.productType == AiMouseProductType_tlw) {
+ [ws.tlwSdk switchConnect:AiMouseConnectType_bluetooth isConnected:true];
+ }
+ else{
+ [ws.frkSdk switchConnect:AiMouseConnectType_bluetooth isConnected:true];
+ }
}
else{
- ws.productType = AiMouseProductType_frk;
- ws.frkSdk = [AiMouse shared];
- [ws.frkSdk setDelegate:ws];
- [ws.frkSdk setLogLevel:AiMouseLogLevel_info];
- [ws.frkSdk switchConnect:AiMouseConnectType_bluetooth isConnected:connected];
+ if (ws.ble.productType == AiMouseProductType_tlw) {
+ [ws.tlwSdk switchConnect:AiMouseConnectType_bluetooth isConnected:false];
+ }
+ else{
+ [ws.frkSdk switchConnect:AiMouseConnectType_bluetooth isConnected:false];
+ }
}
};
self.ble.receiveInfoDataCallBack = ^(NSData *data) {
#pragma mark - TlwMouseDelegate
// 连接状态改变
- (void)tlwMouseConnectStateDidChange:(BOOL)connected{
- [self showLog:[NSString stringWithFormat:@"%@设备连接状态返回: %@",self.tlwSdk.connectType == AiMouseConnectType_bluetooth ? @"蓝牙 ":@"USB",connected ? @"true":@"false"]];
+ [self showLog:[NSString stringWithFormat:@"%@设备连接状态返回: %@",self.tlwSdk.connectType == AiMouseConnectType_bluetooth ? @"tlw 蓝牙 ":@"tlw USB",connected ? @"true":@"false"]];
+
+ if (connected) {
+ [self.frkSdk switchConnect:AiMouseConnectType_bluetooth isConnected:false];
+ }
if (self.aiMouseConnectStateDidChangeCallBack) {
self.aiMouseConnectStateDidChangeCallBack(self.tlwSdk.connectType == AiMouseConnectType_bluetooth ? 0:1,connected);
- (void)aiMouseConnectStateDidChange:(BOOL)connected{
[self showLog:[NSString stringWithFormat:@"%@设备连接状态返回: %@",self.frkSdk.connectType == AiMouseConnectType_bluetooth ? @"蓝牙 ":@"USB",connected ? @"true":@"false"]];
+ if (connected) {
+ [self.tlwSdk switchConnect:AiMouseConnectType_bluetooth isConnected:false];
+ }
+
if (self.aiMouseConnectStateDidChangeCallBack) {
self.aiMouseConnectStateDidChangeCallBack(self.frkSdk.connectType == AiMouseConnectType_bluetooth ? 0:1,connected);
}
}
uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey));
uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey));
-// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
-// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
[[HidManager shared] showLog:([NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef])];
- [[HidManager shared] showLog:([NSString stringWithFormat:@"pid = %d,vid = %d",pid,vid])];
-// return;
if(vid == 0x10D6 && pid == 0xB008){
[HidManager shared].productType = AiMouseProductType_tlw;
[HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
[HidManager shared].productType = AiMouseProductType_frk;
uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
- NSString *tmpPrimaryUsagePage = [NSString stringWithFormat:@"%u",PrimaryUsagePage];
- NSString *tmpPrimaryUsage = [NSString stringWithFormat:@"%u",PrimaryUsage];
- if([tmpPrimaryUsagePage isEqualToString:@"1"] && [tmpPrimaryUsage isEqualToString:@"0"]){
- //设备信息
- [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- if ([HidManager shared].connectStateCallBack != nil) {
- [HidManager shared].connectStateCallBack(true);
- }
+ if(PrimaryUsagePage == 1 && PrimaryUsage == 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]];
+ }else{
+ CFRetain(inIOHIDDeviceRef);
+ IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+
+ [[HidManager shared] showLog:@"打开设备信息成功!"];
+ if([HidManager shared].voiceCha != nil){
+ [HidManager shared].connectStateCallBack(true);
+ }
+ }
}
- else if([tmpPrimaryUsagePage isEqualToString:@"65283"] && [tmpPrimaryUsage isEqualToString:@"0"]){
- [HidManager shared].voiceCha = inIOHIDDeviceRef;
+ else if(PrimaryUsagePage == 65283 && PrimaryUsage == 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]];
+ }else{
+ CFRetain(inIOHIDDeviceRef);
+ IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+ [HidManager shared].voiceCha = inIOHIDDeviceRef;
+
+ [[HidManager shared] showLog:@"打开设备voice成功!"];
+ if([HidManager shared].deviceInfoCha != nil){
+ [HidManager shared].connectStateCallBack(true);
+ }
+ }
}
}
}
/// 设备移除回调
static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
[[HidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]];
- [HidManager shared].deviceInfoCha = nil;
- [HidManager shared].connectStateCallBack(false) ;
+
+ if([HidManager shared].deviceInfoCha){
+ [[HidManager shared] close];
+ [HidManager shared].connectStateCallBack(false) ;
+ }
}
[self close];
//初始化
self.deviceInfoCha = nil;
+ self.voiceCha = nil;
self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
[self showLog:(@"关闭hid成功!")];
}
self.deviceInfoCha = nil;
+ self.voiceCha = nil;
}
/// 发送数据给设备
-(void)writeToDevice:(NSData *)data{
#pragma mark - timer
- (void)addSendTimer{
dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{
- self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+ self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
});
}
- (void)switchConnect:(AiMouseConnectType)type isConnected:(BOOL)isConneted{
- // if(type != _connectType && self.connected){
- // [self.connectManager disConnected];
- // }
- _connected = isConneted;
- if (isConneted) {
- _connectType = type;
- [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
- }
- [self TlwConnectManager:self.connectManager connectStateDidChange:isConneted];
-
- if (type == AiMouseConnectType_hid && isConneted) {
- [self.recordManager checkPermissions];
- }
+ if(isConneted ){
+ _connected = true;
+ _connectType = type;
+ [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
+ [self TlwConnectManager:self.connectManager connectStateDidChange:true];
+ if (type == AiMouseConnectType_hid ) {
+ [self.recordManager checkPermissions];
+ }
+ }
+ else{
+ if (self.connected) {
+ _connected = false;
+ [self TlwConnectManager:self.connectManager connectStateDidChange:false];
+ }
+ }
}
- (void)sendAiDataToSdk:(NSData *)data{
- (void)showLog:(NSString *)log{
if (self.delegate && [self.delegate respondsToSelector:@selector(tlwMouseAddLog:)]) {
- [self.delegate tlwMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk]%@",log]];
+ [self.delegate tlwMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk][tlw]%@",log]];
}
if(self.logLevel == TlwMouseLogLevel_info){
- NSLog(@"[AiMouseSdk]%@",log);
+ NSLog(@"[AiMouseSdk][tlw]%@",log);
}
}