xcode EAAccessoryManager connectedAccessories 返回空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31810498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:37:18  来源:igfitidea点击:

EAAccessoryManager connectedAccessories returns empty array

iosobjective-ciphonexcodebluetooth

提问by Arun Kumar Munusamy

I am using EAAccessoryManager to connect my application to a MFI accessory. During the initial connection, in bluetooth setting screen, it showing as device connected.

我正在使用 EAAccessoryManager 将我的应用程序连接到 MFI 附件。在初始连接期间,在蓝牙设置屏幕中,它显示为设备已连接。

When i try to get the list of connected device using [accessoryManager connectedAccessories], it returns a empty array. But when i use showBluetoothAccessoryPickerWithNameFilter, it shows me the accessory in the list.

当我尝试使用 获取已连接设备的列表时 [accessoryManager connectedAccessories],它返回一个空数组。但是当我使用时showBluetoothAccessoryPickerWithNameFilter,它会向我显示列表中的配件。

The problem is i don't want user to choose the accessory. I want to make this a automated process. I have included the accessory protocol string in info.plist too. Please guide me with this issue. What mistake i am doing here ?

问题是我不希望用户选择配件。我想让它成为一个自动化的过程。我也在 info.plist 中包含了附件协议字符串。请指导我解决这个问题。我在这里犯了什么错误?

回答by Markus Schneider

I had the same issue and was able to resolve it by adding a Supported external accessory protocolskey to my info.plistfile (raw key name is UISupportedExternalAccessoryProtocols). In my case, I wanted to scan for connected PayPal™ credit card terminals and Zebra™ printers. Here's the corresponding extract from my info.plist:

我遇到了同样的问题,并且能够通过Supported external accessory protocols在我的info.plist文件中添加一个密钥来解决它(原始密钥名称是UISupportedExternalAccessoryProtocols)。就我而言,我想扫描连接的 PayPal™ 信用卡终端和 Zebra™ 打印机。这是我的 info.plist 的相应摘录:

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>com.paypal.here.reader</string>
    <string>com.zebra.rawport</string>
</array>

Once I added these, connectedAccessorieswas populated.

一旦我添加了这些,就会connectedAccessories被填充。

回答by Massimo Polimeni

Could you try this function?

你可以试试这个功能吗?

- (void)_getAttachedDevices;
{
    EAAccessoryManager* accessoryManager = [EAAccessoryManager sharedAccessoryManager];
    if (accessoryManager)
    {
        NSArray* connectedAccessories = [accessoryManager connectedAccessories];
        NSLog(@"ConnectedAccessories = %@", connectedAccessories);
    }
    else 
    {
       NSLog(@"No accessoryManager");
    }
}

Which result do you get?

你得到哪个结果?

Obviously remember that EAAccessoryis only for the Made-For-iPod/iPhone/iPad/AirPlay licensed accessories; so if you have no licensed accessory you'll see always an empty array. Do you have a regular licensed MFI accessory?

显然请记住,EAAccessory仅适用于 Made-For-iPod/iPhone/iPad/AirPlay 许可的配件;因此,如果您没有获得许可的配件,您将始终看到一个空数组。您有常规许可的 MFI 配件吗?

In addiction I suggest if you haven't read it yet the Apple Documentation.

如果您还没有阅读Apple 文档,我建议您上瘾。

EDIT 1:

编辑 1:

If you are still stuck try to implement the notification for connect/disconnect:

如果您仍然卡住,请尝试实现连接/断开连接的通知:

[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];  
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(accessoryDidConnect:)
                                                 name:EAAccessoryDidConnectNotification
                                               object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(accessoryDidDisconnect:)
                                                 name:EAAccessoryDidDisconnectNotification
                                               object:nil];

Do you see the connection for your device? If yes try to get the list of connected devices on

您看到设备的连接了吗?如果是,请尝试获取已连接设备的列表

accessoryDidConnect

附件DidConnect