兼容版本优化
authorliangzhenying <867174124@qq.com>
Wed, 28 Aug 2024 13:03:14 +0000 (21:03 +0800)
committerliangzhenying <867174124@qq.com>
Wed, 28 Aug 2024 13:03:14 +0000 (21:03 +0800)
MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MouseSdk/MouseSdk/Ble/BleManager.h
MouseSdk/MouseSdk/Ble/BleManager.m
MouseSdk/MouseSdk/Ble/DeviceManger.m
MouseSdk/MouseSdk/Ble/HidManager.m
MouseSdk/MouseSdk/Ble/TimerManager.m

index 5d853ed..b2b3338 100644 (file)
Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index f356993..13f5777 100644 (file)
@@ -7,7 +7,7 @@
 
 #import <Foundation/Foundation.h>
 #import <CoreBluetooth/CoreBluetooth.h>
-
+#import "DeviceManger.h"
 
 @interface BleManager : NSObject
 
@@ -15,7 +15,7 @@
 @property (nonatomic, copy)void (^receiveVoiceDataCallBack)(NSData *data);/// 语音数据回调
 @property (nonatomic, copy)void (^receiveInfoDataCallBack)(NSData *data);/// 设备信息数据回调
 @property (nonatomic, copy)void (^logCallBack)(NSString *log);//输出日志
-@property (nonatomic, copy) NSArray *bleList;
+@property (nonatomic, assign)AiMouseProductType productType;//
 
 ///// 单利
 //+(BleManager *)shared;
index b7f1084..08a2338 100644 (file)
@@ -26,7 +26,7 @@
 
 @interface BleManager ()<CBCentralManagerDelegate,CBPeripheralDelegate>
 @property(nonatomic, strong) CBCentralManager *centralManager;///蓝牙管理
-@property(nonatomic, strong) CBPeripheral *crrentPeripheral;//连接的设备
+@property(nonatomic, strong) CBPeripheral *curentPeripheral;//连接的设备
 @property(nonatomic, strong) CBCharacteristic *deviceInfoCha;//设备信息特征
 @property (nonatomic,strong) NSMutableData *recordData;//录音的数据
 
 }
 
 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
-    if ([self isContainInSearchList:peripheral.name]) {
-        [self connectPeripheral:peripheral];
-    }
+//    [self showLog:[NSString stringWithFormat:@"%@",peripheral]];
+//    if ([self isContainInSearchList:peripheral.name]) {
+//        [self connectPeripheral:peripheral];
+//    }
 }
 
 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
 //    [self showLog:[NSString stringWithFormat:@"连接成功 name = %@",peripheral.name]];
-    peripheral.delegate = self;
     [peripheral discoverServices:nil];
-    self.crrentPeripheral = peripheral;
 }
 
 -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
     [self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]];
-    self.connectStateCallBack(self.crrentPeripheral.name,false);
-    self.crrentPeripheral = nil;
+    self.connectStateCallBack(self.curentPeripheral.name,false);
+    self.curentPeripheral = nil;
     
 }
 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
     [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]];
-    self.connectStateCallBack(self.crrentPeripheral.name,false);
-    self.crrentPeripheral = nil;
+//    self.connectStateCallBack(self.curentPeripheral.name,false);
+    self.curentPeripheral = nil;
 }
+
 #pragma mark - CBPeripheralDelegate
 //发现服务
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
 
 // 发现特征
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
-    if([peripheral.name isEqualToString:@"VMouse"]){
-        for (CBCharacteristic *characteristic in service.characteristics){
-            if ([characteristic.UUID.UUIDString isEqualToString:kFrkVoiceUUID]) {
-                //语音特征
-                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
-            }
-            else if ([characteristic.UUID.UUIDString isEqualToString:kFrkDeviceInfoUUID]) {
-                //通知设备信息特征
-                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
-                self.deviceInfoCha = characteristic;
+    
+    for (CBCharacteristic *characteristic in service.characteristics){
+        if ([characteristic.UUID.UUIDString isEqualToString:kFrkVoiceUUID]) {
+            //语音特征
+            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
+            self.productType = AiMouseProductType_frk;
+            
+            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
+            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                 [self showLog:[NSString stringWithFormat:@"连接成功 = %@",peripheral.name]];
-                self.connectStateCallBack(self.crrentPeripheral.name,true);
-            }
+                self.connectStateCallBack(self.curentPeripheral.name,true);
+            });
 
         }
+        else if ([characteristic.UUID.UUIDString isEqualToString:kFrkDeviceInfoUUID]) {
+            //通知设备信息特征
+            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
+            self.deviceInfoCha = characteristic;
+        }
     }
-    else{
-        for (CBCharacteristic *characteristic in service.characteristics){
-            if ([characteristic.UUID.UUIDString isEqualToString:kTlwVoiceUUID]) {
-                //语音特征
-                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
-            }
-            else if ([characteristic.UUID.UUIDString isEqualToString:kTlwNotifyDeviceInfoUUID]) {
-                //通知设备信息特征
-                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
-            }
-            else if ([characteristic.UUID.UUIDString isEqualToString:kTlwSetDeviceInfoUUID]) {
-                //设备信息特征
-                self.deviceInfoCha = characteristic;
+    
+    for (CBCharacteristic *characteristic in service.characteristics){
+        if ([characteristic.UUID.UUIDString isEqualToString:kTlwVoiceUUID]) {
+            //语音特征
+            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
+            self.productType = AiMouseProductType_tlw;
+            
+            // 使用dispatch_after来延迟执行任务
+            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
+            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                 [self showLog:[NSString stringWithFormat:@"连接成功 = %@",peripheral.name]];
-                self.connectStateCallBack(self.crrentPeripheral.name,true);
-            }
+                self.connectStateCallBack(self.curentPeripheral.name,true);
+            });
+
+        }
+        else if ([characteristic.UUID.UUIDString isEqualToString:kTlwNotifyDeviceInfoUUID]) {
+            //通知设备信息特征
+            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
+        }
+        else if ([characteristic.UUID.UUIDString isEqualToString:kTlwSetDeviceInfoUUID]) {
+            //设备信息特征
+            self.deviceInfoCha = characteristic;
         }
     }
 }
 //收到蓝牙数据
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
-    if ([peripheral.name isEqualToString:@"VMouse"]) {
+    if (self.productType == AiMouseProductType_tlw) {
         NSData *data = characteristic.value;
-        if([characteristic.UUID.UUIDString isEqualToString:kFrkVoiceUUID]){
+        if([characteristic.UUID.UUIDString isEqualToString:kTlwVoiceUUID]){
             self.receiveVoiceDataCallBack(data);
         }
-        else if([characteristic.UUID.UUIDString isEqualToString:kFrkDeviceInfoUUID]){
+        else if([characteristic.UUID.UUIDString isEqualToString:kTlwNotifyDeviceInfoUUID]){
             self.receiveInfoDataCallBack(data);
         }
     }
     else{
         NSData *data = characteristic.value;
-        if([characteristic.UUID.UUIDString isEqualToString:kTlwVoiceUUID]){
-            //AI数据
+        if([characteristic.UUID.UUIDString isEqualToString:kFrkVoiceUUID]){
             self.receiveVoiceDataCallBack(data);
         }
-        else if([characteristic.UUID.UUIDString isEqualToString:kTlwNotifyDeviceInfoUUID]){
-            //设备信息数据
+        else if([characteristic.UUID.UUIDString isEqualToString:kFrkDeviceInfoUUID]){
             self.receiveInfoDataCallBack(data);
         }
     }
 }
 
+- (void)checkRightDevice{
+    for (CBService *s in self.curentPeripheral.services) {
+        for (CBCharacteristic *c in s.characteristics) {
+            if([c.UUID.UUIDString isEqualToString:kFrkVoiceUUID] ||[c.UUID.UUIDString isEqualToString:kTlwVoiceUUID] ){
+                return;
+            }
+        }
+    }
+    [self.centralManager cancelPeripheralConnection:self.curentPeripheral];
+}
 #pragma mark - public
 - (void)startScanWithName:(NSArray *)bleList{
-    self.bleList = bleList;
-    //如果是已连接或者正在连接,不需要在链接了
-//    if (self.crrentPeripheral != nil ) {
-//        if ([self.crrentPeripheral.name isEqualToString:bleName]) {
-//            if (self.crrentPeripheral.state == CBPeripheralStateConnected || self.crrentPeripheral.state == CBPeripheralStateConnecting) {
-//                return;
-//            }
-//        }
-//        else {
-//            [self.centralManager cancelPeripheralConnection:self.crrentPeripheral];
-//        }
-//    }
-//    
-//    self.bleName = bleName;
-
     NSArray<CBPeripheral *> *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kFrkServiceUUID],[CBUUID UUIDWithString:kTlwServiceUUID]]];
-    if (devices.count > 0 && [self isContainInSearchList: devices[0].name]) {
+    [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",devices]];
+
+    if (devices.count > 0) {
         [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",devices[0].name]];
         [self connectPeripheral:devices[0]];
     }
     }
 }
 
-- (BOOL)isContainInSearchList:(NSString *)name{
-    for (NSString *item in self.bleList) {
-        if ([item isEqualToString:name]) {
-            return  true;
-        }
-    }
-    return false;
-}
-
 
 - (void)stopScan{
     if (self.centralManager.isScanning) {
 
 /// 连接设备
 - (void)connectPeripheral:(CBPeripheral *)peripheral{
-    if(self.crrentPeripheral != nil && self.crrentPeripheral.state == CBPeripheralStateConnected){
+    if(self.curentPeripheral != nil && self.curentPeripheral.state == CBPeripheralStateConnected){
         [self showLog:@"当前蓝牙已处理连接状态,不会重连!!!"];
         return;
     }
     
     [self showLog:[NSString stringWithFormat:@"连接设备 %@",peripheral.description]];
-    self.crrentPeripheral = peripheral;
+    self.curentPeripheral = peripheral;
+    self.curentPeripheral.delegate = self;
     [self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : [NSNumber numberWithBool:YES]}];
+    [self performSelector:@selector(checkRightDevice) withObject:nil afterDelay:1];
 }
 
 -(void)writeToDevice:(NSData *)data{
-    if (self.crrentPeripheral ==nil) {
+    if (self.curentPeripheral ==nil) {
         return;
     }
     if (self.deviceInfoCha ==nil) {
     }
     [self showLog:[NSString stringWithFormat:@"向蓝牙设备发送数据 %@",data]];
     
-    if ([self.crrentPeripheral.name isEqualToString:@"VMouse"]) {
-        [self.crrentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse];
+    if (self.productType == AiMouseProductType_tlw) {
+        [self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse];
     }
     else{
-        [self.crrentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse];
+        [self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse];
     }
 }
 
index 2b4c46f..c7518f4 100644 (file)
         if (connected) {
             [ws.ble stopScan];
         }
-//        if ([name isEqualToString:@"NNMouse"]) {
-//            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];
-//        }
-//        else{
+        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];
+        }
+        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];
-//        }
+        }
         
     };
     self.hid.receiveInfoDataCallBack = ^(NSData *data) {
         if (connected) {
             [ws.hid close];
         }
-        if ([name isEqualToString:@"NMMouse"]) {
+        if (ws.ble.productType == AiMouseProductType_tlw) {
             ws.productType = AiMouseProductType_tlw;
             ws.tlwSdk = [[TlwMouse alloc] init];
             [ws.tlwSdk setDelegate:ws];
index 6b0a054..a65c233 100644 (file)
@@ -41,10 +41,16 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
         [[HidManager shared] showLog:(@"当前处于连接状态,对监听到有USB 插入 的事件不做处理")];
         return;
     }
-    if([HidManager shared].productType == AiMouseProductType_tlw){
+    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 == 0x8601){
+        [HidManager shared].productType = AiMouseProductType_tlw;
         [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
-        [[HidManager shared] showLog:([NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef])];
-
         //注册消息回调
         unsigned char InputBuffer[64];
         IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
@@ -59,7 +65,8 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
         CFRetain([HidManager shared].deviceInfoCha);
         IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
     }
-    else{
+    else  if(vid == 0x6675 && pid == 0x8601){
+        [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];
@@ -90,6 +97,8 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,
 
 
 static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){
+    NSData *data = [[NSData alloc] initWithBytes:report length:reportLength];
+    [[HidManager shared] showLog:[NSString stringWithFormat:@"receive data = %@",data]];
     if([HidManager shared].productType == AiMouseProductType_tlw){
         if (report[0] == 0x01 || report[0] == 0x02) {
             //鼠标键盘活动事件
@@ -127,17 +136,17 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
         [self showLog:(@"打开hid成功!")];
     }
     //设置匹配设备
-    ////供应商id
-    const long vendorID = 0x6675;
-    //产品id
-    const long productID = 0x8601;
+//    ////供应商id
+//    const long vendorID = 0x6675;
+//    //产品id
+//    const long productID = 0x8601;
 //    //供应商id
 //    const long vendorID = 0x10D6;
 //    //产品id
 //    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]];
+//    [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
+//    [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
     IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
     //设置匹配到设备回调
     IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL);
index 9401170..ced8f49 100644 (file)
@@ -28,7 +28,7 @@
 #pragma mark - timer
 - (void)addSendTimer{
     dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{
-        self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+        self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
         [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
         [[NSRunLoop currentRunLoop] run];
     });