///直连蓝牙设备,该函数已经处理了startScanDevice、connectPeripheral和stopScanDevice的调用
/// - Parameter bleName:蓝牙设备名称
- (void)connectBleDeviceWithName:(NSString *)bleName;
-//断开设备链接
-- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result;
+
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
/// 获取信号强度
- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete;
-/////开始鉴权
-//-(void)startAuthentication;
-
@end
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), _block);
}
-- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result {
- self.activeDisconnectCallBack = result;
-
- _isActiveDisconnect = YES;
- if (self.connectPeripheral == nil) {
- if (result) {
- result(YES);
- }
- return;
- }
-
- if (self.connectPeripheral.state == CBPeripheralStateDisconnected) {
- if (result) {
- result(YES);
- }
- return;
- }
-
- int sec = 0;
- if (self.connectPeripheral.state == CBPeripheralStateConnecting) {
- sec = 10;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self.centralManager cancelPeripheralConnection:self.connectPeripheral];
- });
-
- }else if (self.connectPeripheral.state == CBPeripheralStateConnected) {
- [self.centralManager cancelPeripheralConnection:self.connectPeripheral];
- }
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec+10 * NSEC_PER_SEC)), dispatch_get_main_queue(), _disconnectBlock);
-// [self removeMonitoring];
-}
-
/// 连接设备
- (void)startScanDevice{
[self showLog:[NSString stringWithFormat:@"(BleManager) startScanDevice:开始扫描"]];
info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI];
info.CPI_list = @"1,2,3,4";
info.version = [NSString stringWithFormat:@"%d",deviceInfo.version];
- info.brand = @"";
+ info.brand = @"NNMouse";
info.sn = @"";
info.mac = @"";
completation(info,nil);
}
-///// 鉴权
-//-(void)startAuthentication{
-// //设置代理
-// [TlwMouse shared].delegate = self;
-// //切换当前连接类型
-// [[TlwMouse shared] switchConnect:AiMouseConnectType_bluetooth];
-// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用
-// [self showLog:@"(BleManager) startAuthentication 开始鉴权"];
-// //鉴权
-// [[TlwMouse shared] startAuthentication:^(AiMouseAuthState state) {
-// [self showLog:[NSString stringWithFormat:@"(BleManager) startAuthentication 鉴权结果 = %@",@(state)]];
-// if(self.authCallBack){
-// self.authCallBack(state == AiMouseAuthState_Success);
-// }
-// }];
-//}
-
#pragma mark - other
//自动连接,获取已配对的蓝牙,取第一个连接
- (void)autoConnectDevice{
NS_ASSUME_NONNULL_BEGIN
-///用于connectStateCallBack的connectState
-#define connectState_Success @"success"
-#define connectState_Fail @"fail"
-
@interface DeviceManger : NSObject
@property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型
///开始监听鼠标的链接状态
- (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack;
-//#pragma mark ------- 蓝牙的开始连接
-///// 根据蓝牙设备名称直连蓝牙设备 超时时间15s
-///// - Parameter bleName: 设备名
-//- (void)connectBleDeviceWithName:(NSString *)bleName;
-/////断开蓝牙设备链接
-//- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result;
-//
-//#pragma mark ------- USB的开始连接
-/////开始尝试连接usb 超时时间15s
-//- (void)connectHidDevice;
-
-
#pragma mark ------- 共用
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
/// - finished: 设置结果回调
- (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished;
-/////开始鉴权
-//-(void)startAuthentication;
-
-///// 开始鉴权
-//- (void)startAuthentication:(void (^)(BOOL isSuccess))completation;
/// 设置录音模式
/// - Parameters:
@property (nonatomic,strong) BleManager *ble;
@property (nonatomic,strong) HidManager *hid;
-
//connectType 返回为0代表蓝牙,为1代表usb
@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(int connectType, BOOL connected);/// 设备连接状态回调
-
@end
@implementation DeviceManger
}
}
-//- (void)switchConnect:(AiMouseConnectType)type{
-// [[TlwMouse shared]] switchConnect:type];
-//
-// if ([TlwMouse shared]].connectType == AiMouseConnectType_bluetooth) {
-// [self setupBle];
-// [TlwMouse shared]].delegate = self.ble;
-// }
-// else{
-// [self setupHid];
-// [TlwMouse shared]].delegate = self.hid;
-// }
-//}
-
#pragma mark ------ 设备连接相关
- (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack{
self.aiMouseConnectStateDidChangeCallBack = callBack;
[self.ble connectBleDeviceWithName:mouseName];
}
-- (void)connectBleDeviceWithName:(NSString *)bleName {
-// //初始化蓝牙
-// [self setupBle];
- [self.ble connectBleDeviceWithName:bleName];
-}
-
-//断开蓝牙设备链接
-- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result {
- [self.ble disconnectBleDevice:result];
-}
-
-
-- (void)connectHidDevice {
-// //初始化hid
-// [self setupHid];
- [self.hid connectHidDevice];
-}
#pragma mark ------ 设备初始化
- (void)setupHid{
self.hid.authCallBack = ^(BOOL isSuccess) {
if(ws.authCallBack)ws.authCallBack(isSuccess);
};
- //改为由aiMouseConnectStateDidChangeCallBack统一监听
-// self.hid.connectStateCallBack = ^(BOOL isConnnect) {
-// NSString *state = connectState_Success;
-// if (!isConnnect) {
-// state = connectState_Fail;
-// }
-// //单独的usb监听链接接口
-// if(ws.hidConnectStateCallBack)ws.hidConnectStateCallBack(state);
-// };
self.hid.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) {
if (ws.aiMouseConnectStateDidChangeCallBack) {
self.ble.authCallBack = ^(BOOL isSuccess) {
if(ws.authCallBack)ws.authCallBack(isSuccess);
};
-
- //改为由aiMouseConnectStateDidChangeCallBack统一监听
-// self.ble.didConnectDevice = ^(CBPeripheral *peripheral) {
-// //单独的蓝牙监听链接接口
-// if(ws.bleConnectStateCallBack)ws.bleConnectStateCallBack(connectState_Success);
-// };
-// self.ble.didDisConnectDevice = ^(CBPeripheral *peripheral) {
-// //单独的蓝牙监听链接接口
-// if(ws.bleConnectStateCallBack)ws.bleConnectStateCallBack(connectState_Fail);
-// };
-
+
self.ble.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) {
if (ws.aiMouseConnectStateDidChangeCallBack) {
ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_bluetooth,connected);
info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI];
info.CPI_list = @"1,2,3,4";
info.version = [NSString stringWithFormat:@"%d",deviceInfo.version];
- info.brand = @"";
+ info.brand = @"NNMouse";
info.sn = @"";
info.mac = @"";
completation(info,nil);
finished(false,[NSError errorWithDomain:@"AiMouse SDK Error" code:AiMouseErrorCode_unknown userInfo:@{@"NSLocalizedDescriptionKey":@"请求失败"}]);
}
-/////设备鉴权
-//-(void)startAuthentication {
-// if ([TlwMouse shared]].connectType == AiMouseConnectType_bluetooth) {
-// [self.ble startAuthentication];
-// }
-// else{
-// [self.hid startAuthentication];
-// }
-//}
-//
-//- (void)startAuthentication:(void (^)(BOOL isSuccess))completation {
-// self.authCallBack = completation;
-// //设备鉴权
-// [self startAuthentication];
-//}
-
/// 设置录音模式
- (void)setRecordMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished{
// [[TlwMouse shared] setRecordMode:mode completation:finished];
NS_ASSUME_NONNULL_BEGIN
@interface HidManager : NSObject<TlwMouseDelegate>
-@property (nonatomic,copy)void (^connectStateCallBack)(BOOL isConnnect);/// 设备是否连上
-///
-///
@property (nonatomic,copy)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调
@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(BOOL connected);/// 设备连接状态回调
@property (nonatomic, copy)void (^aiMouseSendDeviceInfoDataCallBack)(NSData *data);/// 设备信息数据回调
- (void)open;
- (void)close;
-///开始尝试连接usb
-- (void)connectHidDevice;
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
/// 获取信号强度
- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete;
-///// 开始鉴权
-//-(void)startAuthentication;
+
@end
NS_ASSUME_NONNULL_END
#import "HidManager.h"
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
-
#import "BleManager.h"
-////供应商id
-//const long vendorID = 0x6675;
-////产品id
-//const long productID = 0x8601;
@interface HidManager ()
{
- dispatch_block_t _block;
+
}
@property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理
@property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道
- (instancetype)init{
self = [super init];
if (self) {
- _block = dispatch_block_create(0, ^{
- //执行超时操作
- [self showLog:(@"(HidManager)执行超时操作 ")];
- //连接超时走这里,返回失败
- if (self.deviceInfoCha == nil && self.connectStateCallBack != nil) {
- [self showLog:(@"(HidManager)执行超时操作成功,回调连接失败回调 ")];
- if(self.connectStateCallBack){
- self.connectStateCallBack(false);
- }
- }
- });
- }
- return self;
-}
-///开始尝试连接usb,其实就是设置一个超时时间,要是超时了,就返回连接失败,防止一直得不到结果
-- (void)connectHidDevice {
- //设备是链接状态
- if (self.deviceInfoCha != nil) {
- return;
}
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), _block);
+ return self;
}
-
- (void)open{
//初始化
self.deviceInfoCha = nil;
[self showLog:(@"(HidManager)open:打开hid成功!")];
}
//设置匹配设备
+ ////供应商id
+ //const long vendorID = 0x6675;
+ ////产品id
+ //const long productID = 0x8601;
//供应商id
const long vendorID = 0x10D6;
//产品id
[[TlwMouse shared] switchConnect:AiMouseConnectType_hid];
[[HidManager shared] showLog:(@"(HidManager)hidAppearDevice:回调连接成功 ")];
- [[HidManager shared] cancelBlock];
-
- //连上设备
- if ([HidManager shared].connectStateCallBack != nil) {
- [HidManager shared].connectStateCallBack(true);
- }
+
//注册消息回调
unsigned char InputBuffer[64];
IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
- (void)openDiveceAction{
IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
if (result != kIOReturnSuccess) {
- NSLog([NSString stringWithFormat:@"打开设备信息失败! %d",result]);
+ NSLog([NSString stringWithFormat:@"打开hid失败! %d",result]);
[self performSelector:@selector(openDiveceAction) withObject:nil afterDelay:2];
}else{
- NSLog(@"打开设备信息成功!");
+ NSLog(@"打开hid成功!");
}
CFRetain([HidManager shared].deviceInfoCha);
IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
[[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidDisAppearDevice:拔出设备 %@ ",inIOHIDDeviceRef]];
//取消执行超时操作
[[HidManager shared] showLog:(@"(HidManager)hidDisAppearDevice:回调连接失败 ")];
- [[HidManager shared] cancelBlock];
- if ([HidManager shared].connectStateCallBack != nil) {
- [HidManager shared].connectStateCallBack(false);
- }
[HidManager shared].deviceInfoCha = nil;
-// [[HidManager shared] aiMouseConnectStateDidChange:false];
+ [[HidManager shared] tlwMouseConnectStateDidChange:false];
[[TlwMouse shared] didDisconnect];
}
-- (void)cancelBlock {
- dispatch_block_cancel(_block); // 取消定时的执行
-}
+
static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){
if (report[0] == 0x01 || report[0] == 0x02) {
}
#pragma mark - public
-//-(void)startAuthentication{
-// //设置代理
-// [TlwMouse shared].delegate = self;
-// //切换当前连接类型
-// [[TlwMouse shared] switchConnect:AiMouseConnectType_hid];
-// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用
-// [self showLog:@"(HidManager)开始鉴权"];
-// [[TlwMouse shared] startAuthentication:^(AiMouseAuthState state) {
-// [self showLog:[NSString stringWithFormat:@"(HidManager)鉴权结果 = %@",@(state)]];
-// if(self.authCallBack){
-// self.authCallBack(state == AiMouseAuthState_Success);
-// }
-// }];
-//}
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{
[self showLog:@"(HidManager)getDeviceInfo: 获取设备信息"];
info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI];
info.CPI_list = @"1,2,3,4";
info.version = [NSString stringWithFormat:@"%d",deviceInfo.version];
- info.brand = @"";
+ info.brand = @"NNMouse";
info.sn = @"";
info.mac = @"";
completation(info,nil);
-// @property (nonatomic, assign) int DPI;/// DPI值 数值1 ~ 4 (分别对应 800 1200 1800 2400)
-// @property (nonatomic, assign) int version;///软件版本
-// @property (nonatomic, assign) int batteryState;/// 电池状态
-// @property (nonatomic, assign) int batteryValue;/// 电池百分百电量,0-100
-// ///
-// @property (nonatomic, strong) NSString *battery_power;/// 电池百分百电量,0-100
-// @property (nonatomic, strong) NSString *CPI;/// CPI值
-// @property (nonatomic, strong) NSString *CPI_list;/// 可设置的CPI参数列表
-// @property (nonatomic, strong) NSString *version;///软件版本
-// @property (nonatomic, strong) NSString *brand;/// 品牌名称
-// @property (nonatomic, strong) NSString *sn;/// 设备序列号
-// @property (nonatomic, strong) NSString *mac;///设备MAC地址
}];
}
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:@"(HidManager)IOHIDDevGetUInt32Property:get %@ failed.", inKey]];
- }
- }
- }
- return outValue;
-}
-
@end
IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
if (result != kIOReturnSuccess) {
- NSLog([NSString stringWithFormat:@"打开设备信息失败! %d",result]);
+ NSLog([NSString stringWithFormat:@"打开hid失败! %d",result]);
}else{
- NSLog(@"打开设备信息成功!");
+ NSLog(@"打开hid成功!");
}
CFRetain(inIOHIDDeviceRef);
IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);