本文介绍了在iOS上同时使用外围设备和中央设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了无处不在并尝试了所有内容,但似乎没有任何效果:(

I've looked everywhere and tried everything, but nothing seems to work :(

在iOS上,我正在制作iOS设备需要交换数据的应用程序(适用于iOS 6及更高版本)。因此,两个设备需要同时是外围设备和中心设备。我已经完成了在WWDC视频中指定,但设备无法相互成功连接。

On iOS, I'm making an app (for iOS 6 and above) in which iOS devices need to exchange data. Therefore, both devices need to be peripheral and central at the same time. I've done exactly as specified in the WWDC video, but the devices can't connect successfully with each other.

当我制作一个设备中心时其他唯一外设,中央将无缝连接到外围设备。

When I make one device only central and the other only peripheral, the central connects seamlessly to the peripheral.

然而,当两个设备都是外围设备和中央设备时与此同时,我得到随机错误:在任何阶段(发现服务/特征或将通知值设置为 YES )有时会发生错误,有时会出现 discoverServices 甚至没有调用 didDiscoverServices

However, when both devices are peripheral and central at the same time, I get random errors: at any stage (discovering services/characteristics or setting notify value to YES) errors sometimes happen, and sometimes discoverServices doesn't even call didDiscoverServices

我应该做些什么不同的事情我只是将外围设备和中央代码放入一个视图控制器中。我注意到,如果设备a连接到设备b,然后然后设备b连接到设备a,它通常会工作。我通过在每个设备上手动使用 NSThread sleepForTimeInterval:来管理这个,但我怎样才能让一个设备首先连接(然后另一个) (而不是手动预定义)方式?

Is there something different I should be doing? I simply merged the peripheral and central code into one view controller. I've noticed that if device "a" connects to device "b", and then device "b" connects to device "a", it works more often than not. I manage this by using NSThread sleepForTimeInterval: manually for different amounts of time on each device, but how could I get one device to connect first (and then the other) in a reliable (and not manually pre-defined) way?

如果我确实收到错误,通常它们只是未知错误

If I do get errors, usually they're simply Unknown error

如果您需要任何代码或任何其他信息,请告诉我们。)

Please let me know if you need any code or any other information :)

推荐答案

是的,它可以同时在两个角色中。你只需初始化一个 CBPeripheralManager 和一个 CBCentralManager 。一旦外围设备管理器初始化并且您收到 POWER ON 状态,设备就会开始充当外围设备。您可以在此时添加服务并从其他设备接收连接。同时,您可以使用中央管理器扫描并启动与其他外围设备的连接。

Yes, it can be in both roles at the same time. You just have to initialize a CBPeripheralManager and a CBCentralManager. As soon as the peripheral manager is initialized and you receive the POWER ON state the device starts acting as a peripheral. You can add your services at this point and receive connections from other devices. At the same time you can use the central manager to scan and initiate connections to other peripherals.

请注意,即使它充当外围设备,也无法连接到您自己的设备。

Note that you cannot connect to your own device even if it acts as a peripheral.

对于您的错误,我建议:

For your errors, I suggest:


  1. 在启动之前关闭扫描连接。也就是说,扫描,查找外围设备,停止扫描,连接。连接和扫描彼此不喜欢。

  2. 使用专用队列处理蓝牙事件,而不是主队列。 [[CBCentralManager alloc] initWithDelegate:self queue:my_dedicated_bluetooth_q]

  3. 不幸的是,堆栈有时会变得不稳定。甚至可以重启。但这通常只发生在重负载或多个同时连接的情况下。希望这会在iOS7中得到改进。

  4. 最近几个开发人员开始出现不为人知的未知错误。从您的描述来看,可能有很多原因可能导致您的设置失败,并且需要更多信息才能更好地解决SO问题。

  1. Turn off scanning before initiating a connection. That is, scan, find peripheral, stop scan, connect. Connection and scanning do not like each other.
  2. Use a dedicated queue for handling bluetooth events, not the main queue. [[CBCentralManager alloc] initWithDelegate:self queue:my_dedicated_bluetooth_q]
  3. Unfortunately, the stack sometimes become unstable. Even restarts are possible. But this usually happens only under heavy loads or several simultaneous connections. Hopefully, this will be improved in iOS7.
  4. The unfamous Unknown error started to appear for several developers recently. Judging from your description there are probably a number of reasons why your setup may fail and it would require much more info that what fits well into a SO question.

如需了解更多信息,我建议您搜索bluetooth-dev邮件列表档案或发送邮件[email protected]。如果你接近这样的合理问题,社区会提供很大的帮助。

For more info I suggest you search the bluetooth-dev mailing list archives https://lists.apple.com/archives/Bluetooth-dev or send a mail [email protected]. The community provides great help if you approach with reasonable questions like this.

这篇关于在iOS上同时使用外围设备和中央设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:31