CBPeripheralManager连接回调 [英] CBPeripheralManager connection callback

查看:51
本文介绍了CBPeripheralManager连接回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个iOS设备之间建立蓝牙连接。这个想法是,一个设备提供一项服务,并且有多个设备连接到它:



设备A(服务):用户在演示文稿中滑动



设备B(多个客户端):用户观看了演示文稿,没有任何互动



在我的理解中,提供该服务的用户应该对其进行宣传并分别通过创建CBPeripheralManager并实现CBPeripheralManagerDelegate->设备A



来成为外围设备。另一方面,有多个中央设备使用CBCentralManager扫描外围设备并实现CBCentralManagerDelegate。建立连接后,他们使用CBPeripheral类和CBPeripheralDelegate->设备B



向外围设备发送读写请求,现在我很困惑:被理解的设备A无法将信息直接发送到设备B,甚至无法识别连接是否已建立或丢失。但是客户端(设备B)可以读取和写入数据并获得连接和断开连接的通知。



所以我的第一个想法是交换角色。现在,中央设备通过扫描宣告要使用该服务的设备(?!?)来提供服务,并且多个外围设备可以连接到一个中央设备。听起来已经一定是错误的,对吧?麻烦就来了:现在,如果已建立连接或断开连接(aaahhhhh),则语言使用者(观看演示文稿的用户)将不再得到通知。



现在的问题是:有没有一种好的方法可以使iOS设备成为外围设备并识别连接和断开连接(应该包括按距离断开连接等)?

添加了

:在WWDC 2012会话705中(链接到演示幻灯片)提到了central:didConnect和central:didDisconnect方法。但是,即使在CBPeripheralManagerDelegate的iOS 7 beta文档中也没有提到这些方法。也许这是一个死胡同,Apple现在正在嘲笑我...



感谢您的帮助!



Benjamin

解决方案

您应该使Device A(演示者)成为外围设备,为服务做广告,而Device B(观看者)



您可以使用CBPeripheralManagerDelegate来在中心连接到外围设备时获取通知。这是CBPeripheralManager委托方法的文档: CBPeripheralManagerDelegate协议参考



这是您需要实现的方法,以识别中央(设备B,查看器)何时连接到外围设备(设备A,演示者):

 -(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic * )特征

这是在中央(设备B,查看器)具有与外围设备(设备A,演示者)断开连接:

 -(void)peripheralManager:(CBPeripheralManager *)外围中央设备:( CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)c当您需要通知已订阅的中央设备(设备B,查看者)幻灯片已更改时, ,请使用此方法(newValue将是演示文稿中更新位置的某种指示器,例如幻灯片编号):

  [ self.peripheralManager updateValue:newValue forCharacteristic:yourCharacteristic onSubscribedCentrals:nil]; 

如果您想通过一个简单的蓝牙LE共享演示来演示如何与充当中心和外围设备的设备共享,您可以查看该项目: SimpleShare



希望有帮助!让我知道是否可以澄清一些事情。



Laura


I'm trying to set up a bluetooth connection between two iOS devices. The idea is that one device offers a service and multiple devices connect to it:

Device A (service): User slides through a presentation

Device B (multiple clients): User watches the presentation, no interaction

In my understanding the one which offers the service should advertise it and respectively be the peripheral by creating a CBPeripheralManager and implement the CBPeripheralManagerDelegate -> Device A

On the other side, there are multiple central devices which scan for peripherals using the CBCentralManager and implementing the CBCentralManagerDelegate. After a connection is established they send read and write requests to the peripheral, using the CBPeripheral class and the CBPeripheralDelegate -> Device B

And now I get confused: From what I understood device A can not send information directly to device B and doesn't even recognize if a connection has been established or lost. But the clients (device B) can read and write data and get connect and disconnect notifications.

So my first idea was to swap the roles. Now the central device offers the service by scanning for devices which advertise that they want to use the service (?!?) and multiple peripherals can connect to one central device. Already sounds like it has to be wrong, right? And here comes the trouble: Now the periphals (users who watch the presentation) do not get notified anymore if a connection has been established or lost (aaahhhhh).

Now to the question: Is there a good way to make an iOS device a peripheral and recognize connects and disconnects (which should include disconnects by distance etc)?

ADDED: In WWDC 2012 Session 705 (Link to presentation slides) the methods central:didConnect and central:didDisconnect are mentioned. But even in the iOS 7 beta documentation of CBPeripheralManagerDelegate the methods are not mentioned. Maybe this is a dead end and Apple is laughing at me right now...

Thanks for any help!

Benjamin

解决方案

You should make Device A (the presenter) the peripheral, advertising the service, and Device B (the viewers) the central, which subscribes to the peripheral's service.

You can use the CBPeripheralManagerDelegate to get notifications when a central has connected to your peripheral. Here's the documentation of the CBPeripheralManager delegate methods: CBPeripheralManagerDelegate Protocol Reference

This is the method you need to implement to recognize when a central (Device B, a viewer) has connected to your peripheral (Device A, the presenter):

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic

This is the method you need to recognize when a central (Device B, a viewer) has disconnected from your peripheral (Device A, the presenter):

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic

When you need to notify the subscribed centrals (Device B, the viewers) that the slide has changed, use this method (newValue would be some kind of indicator of the updated position in the presentation, like slide number):

[self.peripheralManager updateValue:newValue forCharacteristic:yourCharacteristic onSubscribedCentrals:nil];

If you want to look through a simple demo of Bluetooth LE sharing with devices acting as both centrals and peripherals, you can check out this project: SimpleShare

Hope that helps! Let me know if I can clarify something.

Laura

这篇关于CBPeripheralManager连接回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆