我遇到了两个与常规蓝牙相关的问题。这是我的代码。
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showElements) userInfo:nil repeats:NO];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryConnected:) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryDisconnected:) name:EAAccessoryDidConnectNotification object:nil];
[[EAAccessoryManager sharedAccessoryManager]registerForLocalNotifications];
}
-(void)showElements{
[[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
if (error) {
NSLog(@"error :%@", error);
}
else{
NSLog(@"Its Working");
}
}];
}
- (void)accessoryConnected:(NSNotification *)notification
{
EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
}
1)建立连接后我收到了这个错误。
error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"
以下是完整的日志:
BTM: attaching to BTServer
BTM: setting pairing enabled
BTM: found device "ESGAA0010" 00:04:3E:95:BF:82
BTM: disabling device scanning
BTM: connecting to device "ESGAA0010" 00:04:3E:95:BF:82
BTM: attempting to connect to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82
BTM: connection to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82 succeeded
BTM: setting pairing disabled
error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"
你可以看到日志的最后一行,显示了错误。我搜索并发现苹果文档说这个错误意味着设备未被找到(EABluetoothAccessoryPickerResultNotFound
),但是如果设备没有被找到,为什么日志中它显示已连接。
2) accessoryConnected:
方法没有被调用。这很可能是由于第一个问题引起的。但我认为在这里提一下也值得一提。
我已经添加了 ExternalAccessory 框架,设备也符合 MFI 要求。请帮助我解决这些问题。谢谢
EAAccessorySelectedKey
而不是EAAccessoryKey
,请注意。根据文档,不知道正确的键应该是哪个,一个是当选择器显示连接时。此外,您获得的错误输出是在EAAccessory *connectedAccessory
行之前还是之后? - Larme