return _recordManager;
}
--(TlwConnectManager *)connectManager{
- if (!_connectManager) {
- _connectManager = [[TlwConnectManager alloc] init];
- _connectManager.delegate = self;
- }
- return _connectManager;
-}
+//-(TlwConnectManager *)connectManager{
+// if (!_connectManager) {
+// _connectManager = [[TlwConnectManager alloc] init];
+// _connectManager.delegate = self;
+// }
+// return _connectManager;
+//}
#pragma mark - pub Api
- (void)setLogLevel:(TlwMouseLogLevel)level{
_connected = isConneted;
if (isConneted) {
_connectType = type;
+ [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
}
-// [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
[self TlwConnectManager:self.connectManager connectStateDidChange:isConneted];
if (type == AiMouseConnectType_hid && isConneted) {
- (void)sendAiDataToSdk:(NSData *)data{
[self.connectManager resetTimeOut];
- [self showLog:[NSString stringWithFormat:@"[receive][Ai] %@",data]];
+ [self showLog:[NSString stringWithFormat:@"[receive][Voice] %@",data]];
if(self.connectType == AiMouseConnectType_hid){
-//// 0AFFFF12BC32
-// if(data.length == 6){
-// NSData *subData = [data subdataWithRange:NSMakeRange(5, 1)];
-// [self sendCmdData:subData];
-// uint8 cmd;
-// [subData getBytes:&cmd length:1];
-//// [self showLog:[NSString stringWithFormat:@"cmd = %X",cmdInt]];
-// if(cmd == 0x31 || cmd == 0x21 || cmd == 0x25 ||cmd == 0x27){
-// self.recordManager.startCmd = subData;
-// [self startRecord];
-// }
-// else if(cmd == 0x32 || cmd == 0x22 || cmd == 0x26 ||cmd == 0x28){
-// [self stopRecord:false];
-// }
-// }
-// else{
-// [self showLog:@"未实现指令"];
-// }
}
else if (self.connectType == AiMouseConnectType_bluetooth){
[self bleReceiveAiData:data];
#import "TimerManager.h"
@interface DeviceManger()<TlwMouseDelegate>
+
+@property (nonatomic, strong) AiMouseDeviceInfo *deviceInfo;
@property (nonatomic,strong) NSMutableData *recordData;//录音的数据
@property (nonatomic,strong) BleManager *ble;
@property (nonatomic,strong) HidManager *hid;
+@property (nonatomic,strong) NSString *mouseName;
//connectType 返回为0代表蓝牙,为1代表usb
@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(int connectType, BOOL connected);/// 设备连接状态回调
@end
- (instancetype)init{
self = [super init];
if (self) {
+ [[TlwMouse shared] setDelegate:self];
[self setupBle];
[self setupHid];
-
- //先默认设置蓝牙管理对象为鼠标
- [[TlwMouse shared] setDelegate:self];
+ [self setupTimer];
}
return self;
}
}
#pragma mark ------ 设备初始化
+- (void)setupTimer{
+ [TimerManager shared].timerCallBack = ^{
+ [self showLog:@"timer action。。。"];
+ if([TlwMouse shared].connected){
+ [self.ble stopScan];
+// [self.hid close];
+ }
+ else{
+ if(self.mouseName.length > 0 ) {
+ [self.ble startScanWithName:self.mouseName];
+ [self.hid open];
+ }
+ }
+ };
+}
- (void)setupHid{
self.hid = [HidManager shared];
__weak typeof(self) ws = self;
self.hid.connectStateCallBack = ^(BOOL connected) {
+ if (connected) {
+ [ws.ble stopScan];
+ }
[[TlwMouse shared] switchConnect:AiMouseConnectType_hid isConnected:connected];
-// if (connected) {
-// [[TlwMouse shared] switchConnect:AiMouseConnectType_hid isConnected:connected];
-// }
-// else{
-// if ([TlwMouse shared].connectType == AiMouseConnectType_hid && [TlwMouse shared].connected){
-// [[TlwMouse shared] switchConnect:AiMouseConnectType_hid isConnected:false];
-// }
-// }
-//
-// if(ws.authCallBack && connected)ws.authCallBack(true);
-// if (ws.aiMouseConnectStateDidChangeCallBack) {
-// ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_hid,connected);
-// }
+
};
self.hid.receiveInfoDataCallBack = ^(NSData *data) {
[[TlwMouse shared] sendDeviceDataToSdk:data];
__weak typeof(self) ws = self;
self.ble.connectStateCallBack = ^(BOOL connected) {
-
+ if (connected) {
+ [ws.hid close];
+ }
[[TlwMouse shared] switchConnect:AiMouseConnectType_bluetooth isConnected:connected];
-
-// if(ws.authCallBack && connected)ws.authCallBack(true);
-// if (ws.aiMouseConnectStateDidChangeCallBack) {
-// ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_bluetooth,connected);
-// }
};
self.ble.receiveInfoDataCallBack = ^(NSData *data) {
[[TlwMouse shared] sendDeviceDataToSdk:data];
- (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack{
self.aiMouseConnectStateDidChangeCallBack = callBack;
- [self.ble startScanWithName:mouseName];
-
- [TimerManager shared].timerCallBack = ^{
- [self showLog:@"timer action。。。"];
- if([TlwMouse shared].connected){
- [self.ble stopScan];
-// [self.hid close];
- }
- else{
- [self.ble startScanWithName:mouseName];
- [self.hid open];
- }
- };
+ self.mouseName = mouseName;
}
///获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation{
+ [self showLog:@"getDeviceInfo request"];
[[TlwMouse shared] getDeviceInfo:^(TlwDeviceInfo *deviceInfo, NSError *error) {
NSString *CPI_list = @"800,1200,1800,2400";// 1-4,800 1200 1800 2400
info.brand = @"NNMouse";
info.sn = @"MSCL1BKZ12105000042371";
info.mac = @"C0310000A583";
+ self.deviceInfo = info;
[self showLog:info.description];
completation(info,nil);
[self.hid writeToDevice:data];
}
- if (self.aiMouseSendDeviceInfoDataCallBack) {
- // 将NSData转换为Base64编码的字符串
- NSString *base64String = [data base64EncodedStringWithOptions:0];
- self.aiMouseSendDeviceInfoDataCallBack(base64String);
- }
+// if (self.aiMouseSendDeviceInfoDataCallBack) {
+// // 将NSData转换为Base64编码的字符串
+// NSString *base64String = [data base64EncodedStringWithOptions:0];
+// self.aiMouseSendDeviceInfoDataCallBack(base64String);
+// }
}
/// 鼠标按键指令回调
/// 设备移除回调
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) ;
static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){
-// [[HidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[[HidManager shared] dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]];
if (report[0] == 0x01 || report[0] == 0x02) {
//鼠标键盘活动事件
return;
[HidManager shared].receiveInfoDataCallBack(data);
}
-
-///// hid数据异常,需要重新开启监听usb设备
-//- (void)hidDataError {
-// //收到数据异常,需要关闭后重新打开
-// [self close];
-// [self open];
-//}
-
#pragma mark - public
- (void)open{
[self close];