\r
\r
```objective-c\r
+/// 鉴权状态回调\r
/// - Parameter isSuccess:是否鉴权成功\r
- (void)aiMouseAuthCallBack:(BOOL)isSuccess;\r
\r
- (void)aiMouseSendAuthData:(NSData *)data;\r
\r
/// 鼠标按键指令回调\r
-/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知\r
+/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义\r
- (void)aiMouseSendCmdData:(NSData *)data;\r
\r
/// 录音回调\r
//ai特征\r
#define kAiUUID @"0783B03E-8535-B5A0-7140-A304D2194CB8"\r
//鉴权特征\r
- #define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7"\r
+ #define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7"\r
```\r
\r
\r
[self showLog:[NSString stringWithFormat:@"[send] %@",[AiTools dataToHexString:data]]];\r
[self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse];\r
}\r
- /// 指令回调\r
- /// - Parameter data: 数据\r
+ /// 鼠标按键指令回调\r
+ /// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义\r
- (void)aiMouseSendCmdData:(NSData *)data{\r
- NSString *cmd = [AiTools dataToHexString:data];\r
+ NSString *cmd = [[AiTools dataToHexString:data] lowercaseString];\r
if([cmd hasSuffix:@"31"]){\r
[self showLog:@"录音键开始录音"];\r
self.recordData = [[NSMutableData alloc] init];\r
[self showLog:@"AI键结束录音"];\r
[self finishRecored:self.recordData];\r
}\r
+ else{\r
+ [self showLog:@"未定义指令!!!!!!!!"];\r
+ }\r
}\r
/// 录音回调\r
/// - Parameter data: pcm格式语音数据\r
- (void)aiMouseSendRecordData:(NSData *)data{\r
[self.recordData appendData:data];\r
}\r
- \r
```\r
-\r
+ \r
\r
\r
## 数据交互流程 \r
\r
-鉴权特征数据走鉴权接口,AI特征数据走AI接口\r
+鉴权特征数据走鉴权接口,AI特征数据走AI接口\r
+\r
\r
-\r
\r
18AE9D502B9AFE7C00743515 /* MouseSdkDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D4F2B9AFE7C00743515 /* MouseSdkDemoTests.m */; };
18AE9D5A2B9AFE7C00743515 /* MouseSdkDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D592B9AFE7C00743515 /* MouseSdkDemoUITests.m */; };
18AE9D5C2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D5B2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m */; };
- 18AE9D6E2B9B017C00743515 /* AiMouseSdk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */; };
- 18AE9D6F2B9B017C00743515 /* AiMouseSdk.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
- 18AE9D712B9B017E00743515 /* AliIotConnectKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */; };
- 18AE9D722B9B017E00743515 /* AliIotConnectKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
18AE9D7B2B9B02BC00743515 /* BleManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D732B9B02BB00743515 /* BleManager.m */; };
18AE9D7C2B9B02BC00743515 /* ScanVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D762B9B02BC00743515 /* ScanVC.m */; };
18AE9D7D2B9B02BC00743515 /* ScanCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D772B9B02BC00743515 /* ScanCell.m */; };
18AE9D7E2B9B02BC00743515 /* Config.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D7A2B9B02BC00743515 /* Config.m */; };
+ 18BF8F2F2BA7D45B008F6933 /* AiMouseSdk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */; };
+ 18BF8F302BA7D45B008F6933 /* AiMouseSdk.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
+ 18BF8F312BA7D45D008F6933 /* AliIotConnectKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */; };
+ 18BF8F322BA7D45D008F6933 /* AliIotConnectKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
dstPath = "";
dstSubfolderSpec = 10;
files = (
- 18AE9D722B9B017E00743515 /* AliIotConnectKit.framework in Embed Frameworks */,
- 18AE9D6F2B9B017C00743515 /* AiMouseSdk.framework in Embed Frameworks */,
+ 18BF8F322BA7D45D008F6933 /* AliIotConnectKit.framework in Embed Frameworks */,
+ 18BF8F302BA7D45B008F6933 /* AiMouseSdk.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
18AE9D552B9AFE7C00743515 /* MouseSdkDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MouseSdkDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
18AE9D592B9AFE7C00743515 /* MouseSdkDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkDemoUITests.m; sourceTree = "<group>"; };
18AE9D5B2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkDemoUITestsLaunchTests.m; sourceTree = "<group>"; };
- 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AliIotConnectKit.framework; sourceTree = "<group>"; };
- 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AiMouseSdk.framework; sourceTree = "<group>"; };
18AE9D732B9B02BB00743515 /* BleManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BleManager.m; sourceTree = "<group>"; };
18AE9D742B9B02BB00743515 /* Config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Config.h; sourceTree = "<group>"; };
18AE9D752B9B02BC00743515 /* ScanCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScanCell.h; sourceTree = "<group>"; };
18AE9D782B9B02BC00743515 /* BleManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BleManager.h; sourceTree = "<group>"; };
18AE9D792B9B02BC00743515 /* ScanVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScanVC.h; sourceTree = "<group>"; };
18AE9D7A2B9B02BC00743515 /* Config.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Config.m; sourceTree = "<group>"; };
+ 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AiMouseSdk.framework; sourceTree = "<group>"; };
+ 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AliIotConnectKit.framework; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
- 18AE9D712B9B017E00743515 /* AliIotConnectKit.framework in Frameworks */,
- 18AE9D6E2B9B017C00743515 /* AiMouseSdk.framework in Frameworks */,
+ 18BF8F312BA7D45D008F6933 /* AliIotConnectKit.framework in Frameworks */,
+ 18BF8F2F2BA7D45B008F6933 /* AiMouseSdk.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
18AE9D692B9B008400743515 /* framework */ = {
isa = PBXGroup;
children = (
- 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */,
- 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */,
+ 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */,
+ 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */,
);
path = framework;
sourceTree = "<group>";
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
+
+#define kDidConnectBleNotification @"kDidConnectBleNotification"
+#define kDidDisconnectBleNotification @"kDidDisconnectBleNotification"
+
NS_ASSUME_NONNULL_BEGIN
@interface BleManager : NSObject
-@property (nonatomic,strong)void (^discoverDevice)(CBPeripheral *peripheral,NSDictionary *advertisementData) ;
-@property (nonatomic,strong)void (^didConnectDevice)(CBPeripheral *peripheral);
-@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);
+@property (nonatomic,strong)void (^discoverDevice)(CBPeripheral *peripheral) ;/// 扫描到设备
+@property (nonatomic,strong)void (^didConnectDevice)(CBPeripheral *peripheral);/// 连接成功
+@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调
-+(id)shared;
++(BleManager *)shared;
+/// 扫描
- (void)startScanDevice;
+/// 停止扫描
- (void)stopScanDevice;
+/// 连接设备
+/// - Parameter peripheral: 蓝牙设备
- (void)connectPeripheral:(CBPeripheral *)peripheral;
-- (void)startAuth;
+/// 开始鉴权
+- (void)startAuthentication;
@end
NS_ASSUME_NONNULL_END
#import <AiMouseSdk/AiMouseSdk.h>
#import "Config.h"
-
+// 服务id
+#define kServiceUUID @"FFF1"
//ai特征
#define kAiUUID @"0783B03E-8535-B5A0-7140-A304D2194CB8"
//鉴权特征
-#define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7"
-//#define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7"
-
-
+//#define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7"
+#define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7"
@interface BleManager ()<CBCentralManagerDelegate,CBPeripheralDelegate,AiMouseDelegate>
-@property (nonatomic,strong) NSMutableArray *scanArray;
-@property(nonatomic, strong) CBCentralManager *centralManager;
-@property(nonatomic, strong) CBPeripheral *connectPeripheral;
-@property(nonatomic, strong) CBCharacteristic *authCha;
-@property(nonatomic, strong) CBCharacteristic *aiCha;
-@property (nonatomic,strong)NSMutableData *recordData;
+@property(nonatomic, strong) CBCentralManager *centralManager;///蓝牙管理
+@property(nonatomic, strong) CBPeripheral *connectPeripheral;//连接的设备
+@property(nonatomic, strong) CBCharacteristic *authCha;//鉴权特征
+@property(nonatomic, strong) CBCharacteristic *aiCha;//ai特征
+@property (nonatomic,strong) NSMutableData *recordData;//录音的数据
@end
@implementation BleManager
-+(id)shared{
++(BleManager *)shared{
static BleManager *manager = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
return manager;
}
--(NSMutableArray *)scanArray{
- if (_scanArray == nil) {
- _scanArray = [[NSMutableArray alloc] init];
- }
- return _scanArray;
-}
-
- (instancetype)init{
self = [super init];
if (self) {
#pragma mark - CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
+ [self showLog:[NSString stringWithFormat:@"设备状态 = %@",@(central.state)]];
switch (central.state) {
-
case CBManagerStateUnknown:
break;
break;
case CBManagerStatePoweredOn:
-// [self startScanDevice];
+ [self autoConnectDevice];
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
- if ([self.scanArray containsObject:peripheral]){
- return;
- }
- if(peripheral.name == nil){
- return;
- }
- [self showLog:peripheral.name];
- [self.scanArray insertObject:peripheral atIndex:0];
+// [self showLog:[NSString stringWithFormat:@"扫描到设备 = %@, data = %@",peripheral,advertisementData]];
if(self.discoverDevice){
- self.discoverDevice(peripheral, advertisementData);
+ self.discoverDevice(peripheral);
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
+ [self showLog:[NSString stringWithFormat:@"连接成功 name = %@",peripheral.name]];
if(self.didConnectDevice){
self.didConnectDevice(peripheral);
}
-
- self.connectPeripheral = peripheral;
peripheral.delegate = self;
[peripheral discoverServices:nil];
+
+ [[NSNotificationCenter defaultCenter] postNotificationName:kDidConnectBleNotification object:nil];
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
[self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]];
+ [[NSNotificationCenter defaultCenter] postNotificationName:kDidDisconnectBleNotification object:nil];
+
+ [self autoConnectDevice];
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
[self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]];
}
#pragma mark - CBPeripheralDelegate
+//发现服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services){
[peripheral discoverCharacteristics:nil forService:service];
}
}
+// 发现特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics){
if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID]) {
}
}
}
-
+//收到蓝牙数据
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSData *data = characteristic.value;
[self showLog:[NSString stringWithFormat:@"[receive] %@",[AiTools dataToHexString:data]]];
[self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse];
}
/// 鼠标按键指令回调
-/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知
+/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义
- (void)aiMouseSendCmdData:(NSData *)data{
NSString *cmd = [[AiTools dataToHexString:data] lowercaseString];
if([cmd hasSuffix:@"31"]){
[self showLog:@"AI键结束录音"];
[self finishRecored:self.recordData];
}
+ else{
+ [self showLog:@"未定义指令!!!!!!!!"];
+ }
}
/// 录音回调
/// - Parameter data: pcm格式语音数据
#pragma mark - public
/// 连接设备
- (void)startScanDevice{
- [self.scanArray removeAllObjects];
[self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}];
}
/// 连接设备
- (void)connectPeripheral:(CBPeripheral *)peripheral{
+ self.connectPeripheral = peripheral;
[self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : [NSNumber numberWithBool:YES]}];
}
/// 开始鉴权
-- (void)startAuth{
+- (void)startAuthentication{
[[AiMouse shared] startAuthentication];
}
#pragma mark - other
+//自动连接,获取已配对的蓝牙,取第一个连接
+- (void)autoConnectDevice{
+ //如果是已连接或者正在连接,则不再重连
+ if (self.connectPeripheral && (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting)) {
+ return;
+ }
+ NSArray<CBPeripheral *> *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
+ for (CBPeripheral *peripheral in devices) {
+ [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",peripheral.name]];
+ [self connectPeripheral:peripheral];
+ break;
+ }
+ // 10s自动重连
+ [self performSelector:@selector(autoConnectDevice) withObject:nil afterDelay:10];
+}
- (void)showLog:(NSString *)log{
NSLog(@"%@",log);
}
-//拿到录音数据,可以上传调用AI接口处理
+//将录音数据保存
- (void)finishRecored:(NSData *)data{
NSString *doc = NSTemporaryDirectory();
NSString *fullPath = [NSString stringWithFormat:@"%@1.pcm",doc];
@interface ScanCell : NSTableCellView
@property(nonatomic,strong)CBPeripheral *peripheral;
-@property(nonatomic,strong)void (^connecCallBack)(CBPeripheral *peripheral);
+@property(nonatomic,strong)void (^connecBtnActionCallBack)(CBPeripheral *peripheral);
@end
NS_ASSUME_NONNULL_END
btn.state = 1;
[self addSubview:btn];
self.connectBtn = btn;
-
-
}
-(void)setPeripheral:(CBPeripheral *)peripheral{
}
- (void)connetAction{
- if (self.connecCallBack) {
- self.connecCallBack(self.peripheral);
+ if (self.connecBtnActionCallBack) {
+ self.connecBtnActionCallBack(self.peripheral);
}
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
- self.title = @"扫描设备";
- [self tableViewSetting];
- [self setupBle];
+ [self setupUI];
+ [self setupBleManger];
}
- (void)loadView{
self.view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
}
-- (void)tableViewSetting{
+- (void)setupUI{
CALayer *viewLayer = [CALayer layer];
NSView *backgroundView= [[NSView alloc]init];
[backgroundView setWantsLayer:YES];
[backgroundView setNeedsDisplay:YES];
[self.view addSubview:backgroundView];
+ //实现tableview的滚动效果
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [NSColor redColor];
- [scrollView setDrawsBackground:NO];
- [scrollView setHasVerticalScroller:YES];
- scrollView.autohidesScrollers = YES;
+ [scrollView setDrawsBackground:NO];//不画背景(背景默认画成白色)
+ [scrollView setHasVerticalScroller:YES];//有垂直滚动条
+ //[_tableContainer setHasHorizontalScroller:YES]; //有水平滚动条
+ scrollView.autohidesScrollers = YES;//自动隐藏滚动条(滚动的时候出现)
[self.view addSubview:scrollView];
self.tableView = [[NSTableView alloc] initWithFrame:scrollView.bounds];
column1.title = @"设备名称";
[column1 setWidth:self.tableView.bounds.size.width];
[self.tableView addTableColumn:column1];
-
+
+// self.tableView.focusRingType = NSFocusRingTypeNone;//tableview获得焦点时的风格
+// self.tableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;//行高亮的风格
+// self.tableView.backgroundColor = [NSColor greenColor];
+// self.tableView.usesAlternatingRowBackgroundColors = YES; //背景颜色的交替,一行白色,一行灰色。设置后,原来设置的 backgroundColor 就无效了。
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.gridStyleMask = NSTableViewSolidHorizontalGridLineMask;
}
-- (void)setupBle{
+- (void)setupBleManger{
BleManager *manager = [BleManager shared];
__weak typeof(self) weakSelf= self;
- __weak typeof(BleManager) *weakManager = manager;
- manager.discoverDevice = ^(CBPeripheral *peripheral,NSDictionary *advertisementData){
+ manager.discoverDevice = ^(CBPeripheral *peripheral){
if ([weakSelf.scanArray containsObject:peripheral]){
return;
}
}
[weakSelf.scanArray addObject:peripheral];
[weakSelf.tableView reloadData];
-// if([peripheral.name isEqualToString:@"iMouse"]){
-// [weakManager connectPeripheral:peripheral];
-// [weakManager stopScanDevice];
-// }
};
manager.didConnectDevice = ^(CBPeripheral * _Nonnull peripheral) {
- NSLog(@"连接成功");
- [weakManager stopScanDevice];
- [weakSelf dismissViewController:weakSelf];
+ [weakSelf connectSuccess];
};
[manager startScanDevice];
}
-- (void)connectDevice:(CBPeripheral *)peripheral{
+- (void)tapDevice:(CBPeripheral *)peripheral{
[[BleManager shared] connectPeripheral:peripheral];
}
+
+- (void)connectSuccess{
+ [[BleManager shared] stopScanDevice];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSAlert *alert = [NSAlert new];
+ [alert addButtonWithTitle:@"确定"];
+ [alert setMessageText:@"连接成功"];
+ [alert setAlertStyle:NSAlertStyleInformational];
+ [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
+ [self dismissViewController:self];
+ }];
+ });
+}
#pragma mark - NSTableViewDelegate,NSTableViewDataSource
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return self.scanArray.count;
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
+// NSString *columnID = tableColumn.identifier;
+// NSString *strIdt = @"NSTableCellView";
+// NSTableCellView *cell = [tableView makeViewWithIdentifier:strIdt owner:self];
+// if (!cell) {
+// cell = [[NSTableCellView alloc]init];
+// cell.identifier = strIdt;
+// }
+// cell.wantsLayer = YES;
+// cell.textField.stringValue = [NSString stringWithFormat:@"cell %ld",(long)row];
+//
+// return cell;
ScanCell *cell = [[ScanCell alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
cell.peripheral = self.scanArray[row];
__weak typeof(self) weakSelf = self;
- cell.connecCallBack = ^(CBPeripheral * _Nonnull peripheral) {
- [weakSelf connectDevice:peripheral];
+ cell.connecBtnActionCallBack = ^(CBPeripheral * _Nonnull peripheral) {
+ [weakSelf tapDevice:peripheral];
};
return cell;
}
@interface ViewController ()
+@property(nonatomic,strong)NSTextField *connectLabel;
+@property(nonatomic,strong)NSTextField *autoLabel;
@end
@implementation ViewController
-
-
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
+ [self setupUI];
+ [self setupNotification];
+ [self setupBleManger];
+}
+
+- (void)setRepresentedObject:(id)representedObject {
+ [super setRepresentedObject:representedObject];
+
+ // Update the view, if already loaded.
+}
+
+- (void)setupUI{
self.title = @"AI SDK";
+
{
- NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 100, 100, 45)];
+ NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 120, 100, 45)];
btn.bordered = true;
btn.title = @"扫描";
[btn setTarget:self];
[self.view addSubview:btn];
}
{
- NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 150, 100, 45)];
+ NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 200, 100, 45)];
btn.bordered = true;
btn.title = @"鉴权";
[btn setTarget:self];
btn.state = 1;
[self.view addSubview:btn];
}
- [self setupBleManger];
-}
-
-
-- (void)setRepresentedObject:(id)representedObject {
- [super setRepresentedObject:representedObject];
-
- // Update the view, if already loaded.
-}
-
-- (void)scanAction{
- ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil];
- [self presentViewControllerAsModalWindow:vc];
-// [[BleManager shared] startScanDevice];
+ {
+ NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 120, 200, 35)];
+ label.editable = NO;
+ label.bordered = NO;
+ label.stringValue = @"连接状态:未连接";
+ label.backgroundColor = NSColor.clearColor;
+ self.connectLabel = label;
+ [self.view addSubview:label];
+ }
+ {
+ NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 200, 200, 35)];
+ label.editable = NO;
+ label.bordered = NO;
+ label.stringValue = @"鉴权状态:--";
+ label.backgroundColor = NSColor.clearColor;
+ self.autoLabel = label;
+ [self.view addSubview:label];
+ }
}
- (void)setupBleManger{
BleManager *manager = [BleManager shared];
};
}
+- (void)scanAction{
+ ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil];
+ [self presentViewControllerAsModalWindow:vc];
+}
+
- (void)checkAuth{
- [[BleManager shared] startAuth];
+ [[BleManager shared] startAuthentication];
}
- (void)showAuthResult:(BOOL)isSuccess{
dispatch_async(dispatch_get_main_queue(), ^{
- NSAlert *alert = [NSAlert new];
- [alert addButtonWithTitle:@"确定"];
- [alert setMessageText:isSuccess?@"鉴权成功":@"鉴权失败"];
- [alert setAlertStyle:NSAlertStyleInformational];
- [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
- }];
+ self.autoLabel.stringValue = isSuccess?@"鉴权结果:鉴权成功":@"鉴权结果:鉴权失败";
});
}
+
+#pragma mark - notication
+- (void)setupNotification{
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidConnect:) name:kDidConnectBleNotification object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidDisconnect:) name:kDidDisconnectBleNotification object:nil];
+}
+
+- (void)bleDidConnect:(NSNotification *)notification{
+ self.connectLabel.stringValue = @"连接状态:已连接";
+}
+- (void)bleDidDisconnect:(NSNotification *)notification{
+ self.connectLabel.stringValue = @"连接状态:未连接";
+}
@end
- (void)aiMouseSendAuthData:(NSData *)data;
/// 鼠标按键指令回调
-/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知
+/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义
- (void)aiMouseSendCmdData:(NSData *)data;
/// 录音回调
<dict>
<key>hash2</key>
<data>
- JyvxVxrmWssBwECs5Ix0vn7n5HUulWxcOT7uWBUWoI0=
+ fZL1gyEtDIrBtKXvRHJ+p9DtmNnDDuR+hRWmsxYGAuI=
</data>
</dict>
<key>Headers/AiMouseSdk.h</key>