@property (nonatomic, strong) TlwAudioManager *audioManager;
@property (nonatomic, strong) AiRecordManager *recordManager;
+@property (nonatomic,strong)void (^dongleStateCallBack)(BOOL success);
+@property (nonatomic,assign)NSInteger dongleTimes;
+
@property (nonatomic,strong)void (^openRecordCallBack)(BOOL isSuccess,NSError *error);
@property (nonatomic,strong)void (^closeRecordCallBaack)(BOOL isSuccess,NSError *error);
@property (nonatomic,strong)void (^setDpiCallBack)(BOOL isSuccess,NSError *error);
[mData appendBytes:&tail length:1];
[self sendToDevice:mData];
}
-- (void)sendDongleCmd{
- uint8_t cmd = 0x01;
- uint8_t len = 0x00;
- uint8_t tail = 0xEF;
- NSMutableData *mData = NSMutableData.data;
- [mData appendData:[self getHidHeader]];
- [mData appendBytes:&cmd length:1];
- [mData appendBytes:&len length:1];
- [mData appendBytes:&tail length:1];
- [self sendToDevice:mData];
-}
#pragma mark - pub Api
- (void)setLogLevel:(TlwMouseLogLevel)level{
}
- (void)switchConnect:(AiMouseConnectType)type isConnected:(BOOL)isConneted{
+ [NSObject cancelPreviousPerformRequestsWithTarget:self];
if(isConneted ){
_connected = true;
_connectType = type;
[self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
- [self TlwConnectManager:nil connectStateDidChange:true];
+
if (type == AiMouseConnectType_hid ) {
[self.recordManager checkPermissions];
- [self sendOpenCmd];
- [self sendDongleCmd];
+// [self sendOpenCmd];
+ [self checkDongleState];
}
+ else{
+ [self TlwConnectManager:nil connectStateDidChange:true];
+ }
+// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+// [HidManager shared].connectStateCallBack(true);
+// });
}
else{
[self stopRecord];
[mData appendBytes:&value length:1];
[self sendToDevice:mData];
}
-
-
}
- (void)getDeviceInfo:(void (^)(TlwDeviceInfo *deviceInfo,NSError *error))finished{
if(self.connectType == AiMouseConnectType_hid){
- (void)getFullDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished{
- if (self.getFullInfoCallBack) {
- return;
- }
self.getFullInfoCallBack = finished;
self.questTimes = 0;
[self checkFullInfoAction];
- (void)checkFullInfoAction{
self.questTimes++;
if (self.questTimes > 3) {
+ self.getFullInfoCallBack(nil,[NSError errorWithDomain:@"AiMouse SDK Error" code:AiMouseErrorCode_unknown userInfo:@{@"NSLocalizedDescriptionKey":@"请求失败"}]);
[self switchConnect:AiMouseConnectType_hid isConnected:false ];
return;
}
[self performSelector:@selector(checkFullInfoAction) withObject:nil afterDelay:1];
}
+#pragma mark - check dongle
+- (void)checkDongleState{
+ self.dongleTimes = 0;
+ __weak typeof(self) ws = self;
+ [self checkDongleStateAction:^(BOOL success) {
+ ws.dongleStateCallBack = nil;
+ if (success) {
+ [ws TlwConnectManager:nil connectStateDidChange:true];
+ }
+ }];
+}
+- (void)checkDongleStateAction:(void (^)(BOOL success))finished{
+ self.dongleStateCallBack = finished;
+ [self performSelector:@selector(sendDongleCmd) withObject:nil afterDelay:1];
+}
+- (void)sendDongleCmd{
+ self.dongleTimes++;
+ if ( self.dongleStateCallBack == nil) {
+ return;
+ }
+ if (self.dongleTimes > 2) {
+ [self TlwConnectManager:nil connectStateDidChange:false];
+ return;
+ }
+ uint8_t cmd = 0x0D;
+ uint8_t len = 0x00;
+ uint8_t tail = 0xEF;
+ NSMutableData *mData = NSMutableData.data;
+ [mData appendData:[self getHidHeader]];
+ [mData appendBytes:&cmd length:1];
+ [mData appendBytes:&len length:1];
+ [mData appendBytes:&tail length:1];
+ [self sendToDevice:mData];
+ [self performSelector:@selector(sendDongleCmd) withObject:nil afterDelay:1];
+}
#pragma mark - ble
- (void)bleReceiveAiData:(NSData *)data{
[self.audioManager receiveData:data];
[[data subdataWithRange:NSMakeRange(9, 1)] getBytes:&state length:1];
[self showLog:[NSString stringWithFormat:@"dongle version = %d,bind = %d",version,state]];
+ if(self.dongleStateCallBack)self.dongleStateCallBack(state == 1);
break;
}
case 0x9f:
}
}
-#pragma mark - TlwConnectManagerDelegate
+#pragma mark - other
-(void)TlwConnectManager:(NSString *)manager connectStateDidChange:(BOOL)connected{
_connected = connected;
if([self.delegate respondsToSelector:@selector(tlwMouseConnectStateDidChange:)]) [self.delegate tlwMouseConnectStateDidChange:connected];