@property(nonatomic, strong) CBCharacteristic *deviceInfoCha;//设备信息特征
@property (nonatomic,strong) NSMutableData *recordData;//录音的数据
+@property (nonatomic, strong) NSTimer *timer;
+@property (nonatomic, strong) dispatch_queue_t mainQueue;
+
@end
@implementation BleManager
-//+(BleManager *)shared{
-// static BleManager *manager = nil;
-// static dispatch_once_t oncePredicate;
-// dispatch_once(&oncePredicate, ^{
-// manager = [[BleManager alloc]init];
-// });
-// return manager;
-//}
-
- (instancetype)init{
self = [super init];
if (self) {
[self.centralManager cancelPeripheralConnection:self.curentPeripheral];
}
#pragma mark - public
-- (void)startScanWithName:(NSArray *)bleList{
+- (void)startScan{
NSArray<CBPeripheral *> *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kFrkServiceUUID],[CBUUID UUIDWithString:kTlwServiceUUID]]];
- [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",devices]];
+ [self showLog:[NSString stringWithFormat:@"查询配对过的设备%@",devices]];
if (devices.count > 0) {
- [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",devices[0].name]];
+ [self showLog:[NSString stringWithFormat:@"配对匹配过的设备%@",devices[0].name]];
[self connectPeripheral:devices[0]];
}
else{
- [self showLog:[NSString stringWithFormat:@"开始扫描 设备名称 %@",bleList]];
- [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}];
+// [self showLog:[NSString stringWithFormat:@"开始扫描%@",@""]];
+// [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}];
}
}
-
- (void)stopScan{
if (self.centralManager.isScanning) {
[self showLog:[NSString stringWithFormat:@":停止扫描"]];
}
}
-/// 连接设备
-- (void)connectPeripheral:(CBPeripheral *)peripheral{
- if(self.curentPeripheral != nil && self.curentPeripheral.state == CBPeripheralStateConnected){
- [self showLog:@"当前蓝牙已处理连接状态,不会重连!!!"];
- return;
- }
-
- [self showLog:[NSString stringWithFormat:@"连接设备 %@",peripheral.description]];
- 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.curentPeripheral ==nil) {
return;
}
}
+#pragma mark - other
+- (void)connectPeripheral:(CBPeripheral *)peripheral{
+ if(self.curentPeripheral != nil && self.curentPeripheral.state == CBPeripheralStateConnected){
+ [self showLog:@"当前蓝牙已处理连接状态,不会重连!!!"];
+ return;
+ }
+
+ [self showLog:[NSString stringWithFormat:@"连接设备 %@",peripheral.description]];
+ 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)showLog:(NSString *)log{
NSString *text = [NSString stringWithFormat:@"[BleManager] %@",log];
if(self.logCallBack) self.logCallBack(text);
}
+#pragma mark - timer
+- (void)start{
+ if (self.timer && [self.timer isValid]) {
+ [self.timer invalidate];
+ self.timer = nil;
+ }
+ dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{
+ self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+ [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
+ [[NSRunLoop currentRunLoop] run];
+ });
+}
+
+- (void)timerAction{
+ if(self.curentPeripheral == nil || self.curentPeripheral.state == CBPeripheralStateDisconnected){
+ [self startScan];
+ }
+}
@end
+////
+//// HidManager.m
+//// MouseSdk
+////
+//// Created by incar on 2024/3/26.
+////
//
-// HidManager.m
-// MouseSdk
-//
-// Created by incar on 2024/3/26.
-//
-
-#import "AiHidManager.h"
-#include <IOKit/hid/IOHIDLib.h>
-#include <IOKit/hid/IOHIDKeys.h>
-
-
-
-//供应商id
-//const long vendorID = 0x6675;
-//const long productID = 0x8601;
-
-@interface AiHidManager ()
-
-@property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理
-@property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道
-@property (nonatomic,strong) NSMutableData *recordData;//录音的数据
-@end
-
-@implementation AiHidManager
-+(AiHidManager *)shared{
- static AiHidManager *manager = nil;
- static dispatch_once_t oncePredicate;
- dispatch_once(&oncePredicate, ^{
- manager = [[AiHidManager alloc]init];
- });
- return manager;
-}
-
-- (instancetype)init{
- self = [super init];
- if (self) {
- [self initHid];
- [self open];
- }
- return self;
-}
-
-- (void)initHid{
-
-}
-
-- (void)open{
- self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
- IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
- IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
- if (ret != kIOReturnSuccess) {
- NSLog(@"打开设备失败!");
- }else{
- NSLog(@"打开设备成功!");
- }
- //供应商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]];
- IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
- //设置匹配到设备回调
- IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL);
- //设置移除设备回调
- IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
-}
-
-- (void)close{
- IOReturn ret = IOHIDManagerClose(self.hidManager, 0);
- if (ret != kIOReturnSuccess) {
- NSLog(@"关闭设备失败!");
- }else{
- NSLog(@"关闭设备成功!");
- }
-}
-
-#pragma mark - Hid CallBack
-/// 匹配到设备回调
-static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
- [[AiHidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]];
-// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
- //设备信息
- [AiHidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- //注册消息回调
- unsigned char InputBuffer[128];
- IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
- IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
- if (ret != kIOReturnSuccess) {
- NSLog(@"打开设备信息失败!");
- }else{
- NSLog(@"打开设备信息成功!");
- }
- CFRetain(inIOHIDDeviceRef);
- IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+//#import "AiHidManager.h"
+//#include <IOKit/hid/IOHIDLib.h>
+//#include <IOKit/hid/IOHIDKeys.h>
+//
+//
+//
+////供应商id
+////const long vendorID = 0x6675;
+////const long productID = 0x8601;
+//
+//@interface AiHidManager ()
+//
+//@property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理
+//@property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道
+//@property (nonatomic,strong) NSMutableData *recordData;//录音的数据
+//@end
+//
+//@implementation AiHidManager
+//+(AiHidManager *)shared{
+// static AiHidManager *manager = nil;
+// static dispatch_once_t oncePredicate;
+// dispatch_once(&oncePredicate, ^{
+// manager = [[AiHidManager alloc]init];
+// });
+// return manager;
+//}
+//
+//- (instancetype)init{
+// self = [super init];
+// if (self) {
+// [self initHid];
+// [self open];
+// }
+// return self;
+//}
+//
+//- (void)initHid{
+//
+//}
+//
+//- (void)open{
+// self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+// IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+// IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
+// if (ret != kIOReturnSuccess) {
+// NSLog(@"打开设备失败!");
+// }else{
+// NSLog(@"打开设备成功!");
// }
-// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
-// //ai
-// [AiHidManager shared].aiCha = inIOHIDDeviceRef;
+// //供应商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]];
+// IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
+// //设置匹配到设备回调
+// IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL);
+// //设置移除设备回调
+// IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
+//}
+//
+//- (void)close{
+// IOReturn ret = IOHIDManagerClose(self.hidManager, 0);
+// if (ret != kIOReturnSuccess) {
+// NSLog(@"关闭设备失败!");
+// }else{
+// NSLog(@"关闭设备成功!");
+// }
+//}
+//
+//#pragma mark - Hid CallBack
+///// 匹配到设备回调
+//static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
+// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]];
+//// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
+// //设备信息
+// [AiHidManager shared].deviceInfoCha = inIOHIDDeviceRef;
// //注册消息回调
// unsigned char InputBuffer[128];
// IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
-// IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0);
+// IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
// if (ret != kIOReturnSuccess) {
-// NSLog(@"打开AI失败!");
+// NSLog(@"打开设备信息失败!");
// }else{
-// NSLog(@"打开AI成功!");
+// NSLog(@"打开设备信息成功!");
// }
// CFRetain(inIOHIDDeviceRef);
// IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
-// }
-}
-
-/// 设备移除回调
-static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
- [[AiHidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]];
-// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
-// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
-//
-// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
-// [AiHidManager shared].deviceInfoCha = nil;
-//// [AiHidManager 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){
-// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[AiMouseTools dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]];
-
-// NSInteger len = get_data_len(report, 64);
- NSData *data = [[NSData alloc] initWithBytes:report length:64];
- [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理后 %@",data]];
-
-// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey));
-// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey));
-//
-// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
-// [[AiMouse shared] sendDeviceDataToSdk:data];
-//
-// }
-// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
-// [[AiMouse shared] sendAiDataToSdk:data];
-// }
-}
-
-
-- (void)showLog:(NSString *)log{
- NSLog(@"%@",log);
-}
-
-@end
+//// }
+//// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
+//// //ai
+//// [AiHidManager shared].aiCha = inIOHIDDeviceRef;
+//// //注册消息回调
+//// unsigned char InputBuffer[128];
+//// IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
+//// IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0);
+//// if (ret != kIOReturnSuccess) {
+//// NSLog(@"打开AI失败!");
+//// }else{
+//// NSLog(@"打开AI成功!");
+//// }
+//// CFRetain(inIOHIDDeviceRef);
+//// IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+//// }
+//}
+//
+///// 设备移除回调
+//static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
+// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]];
+//// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
+//// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
+////
+//// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
+//// [AiHidManager shared].deviceInfoCha = nil;
+////// [AiHidManager 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){
+//// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[AiMouseTools dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]];
+//
+//// NSInteger len = get_data_len(report, 64);
+// NSData *data = [[NSData alloc] initWithBytes:report length:64];
+// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理后 %@",data]];
+//
+//// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey));
+//// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey));
+////
+//// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
+//// [[AiMouse shared] sendDeviceDataToSdk:data];
+////
+//// }
+//// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
+//// [[AiMouse shared] sendAiDataToSdk:data];
+//// }
+//}
+//
+//
+//- (void)showLog:(NSString *)log{
+// NSLog(@"%@",log);
+//}
+//
+//@end