我面临与常规蓝牙相关的2个问题。这是我的代码。
- (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标准。 帮我解决这些问题。感谢
答案 0 :(得分:10)
我今天遇到了同样的问题。解决方案很简单,您需要在.plist文件中添加额外的行。
<key>UISupportedExternalAccessoryProtocols</key>
<array>
<string>YOUR_DEVICE_PROTOCOL</string>
</array>
如果将设备添加到MFi Program,它应该拥有自己的协议。检查设备文档或询问设备创建者。
[[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
if (error) {
NSLog(@"error :%@", error);
}
else{
NSLog(@"Its Working");
}
}];
错误是EABluetoothAccessoryPickerError
的实例。有可能值:
public enum Code : Int {
public typealias _ErrorType = EABluetoothAccessoryPickerError
case alreadyConnected
case resultNotFound
case resultCancelled
case resultFailed
}
您的错误代码为1 resultNotFound
。请注意,修复.plist文件时showBluetoothAccessoryPickerWithNameFilter
有时会返回错误代码= 0.然后没有错误,因为您的设备是case alreadyConnected
。我添加此信息是因为在检测到此信息之前我丢失了很多时间。 :)
EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil) { (error) in
if let error = error {
switch error {
case EABluetoothAccessoryPickerError.alreadyConnected:
break
default:
break
}
}
}
答案 1 :(得分:0)
尝试进入iOS蓝牙设置并取消配对设备并再次配对。我得到了这个&#34; 305&#34;错误之前和问题是我已经配对设备然后更新了设备的固件。之后,在我从iPhone上移除设备然后在设备的固件更新后重新配对之前,它将无法再次连接。
这可能对你不起作用,但关于305错误的互联网上没有多少,所以希望这至少可以帮助别人。