gpt4 book ai didi

ios - 使用串行蓝牙连接设备时出现问题

转载 作者:技术小花猫 更新时间:2023-10-29 10:49:52 24 4
gpt4 key购买 nike

我面临 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 标准。帮我解决这些问题。谢谢

最佳答案

我今天遇到了同样的问题。解决方案很简单,您需要在 .plist 文件中添加额外的行。

<key>UISupportedExternalAccessoryProtocols</key>
<array>
<string>YOUR_DEVICE_PROTOCOL</string>
</array>

如果设备添加到 MFi Program它应该有自己的协议(protocol)。检查您的设备文档或询问设备创建者。

编辑

[[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。我添加此信息是因为我在检测到此之前浪费了很多时间。 :)

祝你好运。

编辑(Swift 3.0)

EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil) { (error) in
if let error = error {
switch error {
case EABluetoothAccessoryPickerError.alreadyConnected:
break
default:
break
}
}
}

关于ios - 使用串行蓝牙连接设备时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com