TSNPeerBluetooth 1.0.6

TSNPeerBluetooth 1.0.6

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2015年5月

Brian Lambert维护。



  • Brian Lambert

TSNPeerBluetooth 是一个用于 iOS 的基于蓝牙低功耗(BLE)的点对点网络库。

我创建 TSNPeerBluetooth 作为如何使用苹果的 Core Bluetooth 框架定义自定义蓝牙低功耗服务和一系列服务特性的示例,允许附近的对等实体交换存在、位置和状态更新。TSNPeerBluetooth 既是蓝牙低功耗附件本身,也是其自身的消费者,因此它对于 iOS 的蓝牙低功耗编程示例来说非常有用。

为了使 TSNPeerBluetooth 成为一个更有用的示例,我创建了一个使用它的示例应用程序,名为 Bubble Chat。Bubble Chat 允许处于蓝牙低功耗通信范围内的 iOS 设备在地图上看到对方,并交换聊天消息。

我写了一篇关于 BubbleChat 和 TSNPeerBluetooth 的博客文章,其中包括一个展示 BubbleChat 在实际中运行的视频。

克隆TSNPeerBluetooth 和 / 或Bubble Chat,尝试它们。更好的是,分叉它们并发送一些 pull request。

使用 TSNPeerBluetooth

将 TSNPeerBluetooth 添加到您的 podfile。

pod 'TSNPeerBluetooth'

然后使用以下内容安装它

pod install

TSNPeerBluetooth 的工作原理

如以下所示分配并初始化 TSNPeerBluetooth 类的新实例。

// Allocate and initialize the service type.
NSUUID * serviceType = [[NSUUID alloc] initWithUUIDString:@"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"];

// Static declarations.
static NSString * const PEER_IDENTIFIER_KEY = @"PeerIdentifierKey";

// Obtain user defaults and see if we have a serialized peer identifier. If we do,
// deserialize it. If not, make one and serialize it for later use.
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSData * peerIdentifierData = [userDefaults dataForKey:PEER_IDENTIFIER_KEY];
if (!peerIdentifierData)
{
    // Create a new peer identifier.
    UInt8 uuid[16];
    [[NSUUID UUID] getUUIDBytes:uuid];
    peerIdentifierData = [NSData dataWithBytes:uuid
                                        length:sizeof(uuid)];

    // Save the peer identifier in user defaults.
    [userDefaults setValue:peerIdentifierData
                    forKey:PEER_IDENTIFIER_KEY];
    [userDefaults synchronize];
}
NSUUID * peerIdentifier = [[NSUUID alloc] initWithUUIDBytes:[peerIdentifierData bytes]];

// Allocate and initialize the peer Bluetooth context.
_peerBluetooth = [[TSNPeerBluetooth alloc] initWithServiceType:serviceType
                                                peerIdentifier:peerIdentifier
                                                      peerName:[[UIDevice currentDevice] name]];
[_peerBluetooth setDelegate:(id<TSNPeerBluetoothDelegate>)self];

用您自己的 UUID 替换 FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。

一旦您有了 TSNPeerBluetooth 实例,请使用 start 方法启动您的对等节点

[_peerBluetooth start];

然后使用 stop 方法停止您的对等节点

[_peerBluetooth stop];

要更新您的对等节点的位置,请调用 updateLocation 方法

[_peerBluetooth updateLocation:location];

要更新您的对等节点的状态,请调用 updateStatus 方法

[_peerBluetooth updateStatus:@"My new status!"];

实现 TSNPeerBluetoothDelegate 以接收 TSNPeerBluetooth 事件的回调。

当发现并连接到新的对等节点时,会调用 peerBluetooth:didConnectPeerIdentifier:peerName:。

// Notifies the delegate that a peer was connected.
- (void)peerBluetooth:(TSNPeerBluetooth *)peerBluetooth
didConnectPeerIdentifier:(NSUUID *)peerIdentifier
             peerName:(NSString *)peerName
         peerLocation:(CLLocation *)peerLocation
{
...
}

当先前连接的对等节点断开连接时,会调用 peerBluetooth:didDisconnectPeerIdentifier:

// Notifies the delegate that a peer was disconnected.
- (void)peerBluetooth:(TSNPeerBluetooth *)peerBluetooth
didDisconnectPeerIdentifier:(NSUUID *)peerIdentifier
{
...
}

当对等节点更新其位置时,会调用 peerBluetooth:didReceivePeerLocation:fromPeerIdentifier:

// Notifies the delegate that a peer location was received.
- (void)peerBluetooth:(TSNPeerBluetooth *)peerBluetooth
didReceivePeerLocation:(CLLocation *)peerLocation
   fromPeerIdentifier:(NSUUID *)peerIdentifier
{
...
}

当对等节点更新其状态时,会调用 peerBluetooth:didReceivePeerStatus:fromPeerIdentifier:

// Notifies the delegate that a peer status was received.
- (void)peerBluetooth:(TSNPeerBluetooth *)peerBluetooth
 didReceivePeerStatus:(NSString *)peerStatus
   fromPeerIdentifier:(NSUUID *)peerIdentifier
{
...
}

(查看Bubble Chat 的 TSNAppContext.m 文件以获取 TSNPeerBluetoothDelegate 的示例实现。)

许可

TSNPeerBluetooth 采用 MIT 许可发布,这意味着您可以在闭源和开源项目中自由使用。然而,即使在闭源项目中,也请在您的项目中包含一个公开可访问的 TSNPeerBluetooth 版权声明副本,您可以在 LICENSE 文件中找到它。

反馈

如果您对 TSNPeerBluetooth 有任何疑问、建议或贡献,请随时联系我