BlueChat 0.0.3

BlueChat 0.0.3

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2016年6月

Max Zou 维护。




BlueChat 0.0.3

  • 作者:
  • Max Zou

通过蓝牙低功耗进行 1-to-1 聊天

要求
  • 应用程序应针对每个手机单独进行广播。
  • 您可以选择另一部手机,将其“添加为好友”(无需与其他方确认)。
  • 一旦添加,您就应该能够开始输入要发送给另一部手机的消息。
  • 另一部手机可以随时回复消息。
  • 只需简单消息即可,为了简化,请不要保留太多消息(2-10 个字符左右)
  • 作为额外功能:提供最多140个字符的发送能力。
演示
screen0 screen1 screen2 screen3 screen4 screen5
screen0 screen1 screen2 screen3 screen4 screen5

YouTube BlueChat

设计考虑
  • 用户不应需要知道两部手机如何通过蓝牙低功耗连接,对他们来说,只需简单地添加另一部手机作为好友即可。这意味着应用程序需要能够隐藏蓝牙低功耗中央和外围的详细信息,并在不同的场景中使用正确的角色
  • 应用程序在外围和中央角色上运行,并在获取用户输入的名称后立即开始进行广播
  • 虽然外围和中央侧的搜索/连接部分不同,但聊天过程在双方都是常见的,如 sendMessage、leaveChatroom、newMessage 通知等,它们在 BCChatManager 和 BCChatManagerDelegate 中进行了抽象
手机 1 的工作流程(中央侧 BCChatClient.h
  • (screen 0) 当应用程序启动时,它首先要求输入名称
  • (screen 1 - 外围) 获取名称后,它以名称作为本地名称、BCChatService 作为主要服务开始广播
  • (screen 2 - 中央) 当用户选择添加新好友时,它搜索具有 BCChatService 的附近手机
  • (screen 3 - 外围) 当用户选择一个好友并返回好友视图时,它重新加载列表并继续在外围侧进行广播
  • (screen 4 - 中央) 当用户在列表中轻按一个好友时,它停止广播(外围侧)并连接到手机 2 上的应用程序(中央侧)
  • (屏幕 5 - 中央)连接到外围设备后进入聊天视图。通过 BCChatCentralToPeripheralCharacteristic 发送消息,并监听 BCChatPeripheralToCentralCharacteristic 的指示来接收消息。
手机 2 上的工作流程(外围侧 BCChatServer.h
  • (screen 0) 当应用程序启动时,它首先要求输入名称
  • (屏幕 1/3 - 外围)获取名称后,以本地名称和 BCChatService 开始广播。
  • (屏幕 5 - 外围)手机 1 连接到应用程序后,在 BCChatPeripheralToCentralCharacteristic 上启用通知,这样手机 2 就知道已有客户端加入,因此进入聊天视图并停止广播。它更新 BCChatPeripheralToCentralCharacteristic 以发送消息,并在接收消息时获取 BCChatCentralToPeripheralCharacteristic 的写入值。
  • (屏幕 5 - 外围)CBPeripheralManager 没有关闭连接的方法,因此向 BCChatPeripheralToCentralDisconnectRequestCharacteristic 写入以告知另一侧断开连接。
BLE 服务定义
/**
 *  UUID of the Chat Server service
 */
extern NSString *const BCChatServiceUUID;

/**
 *  UUID of the Characteristic for the central to send messages to the peripheral. Write only. The data is UTF8 encoded text.
 */
extern NSString *const BCChatCentralToPeripheralCharacteristicUUID;

/**
 *  UUID of the Characteristic for the peripheral to send messages to the central. Indicate and Read. The data is UTF8 encoded text.
 */
extern NSString *const BCChatPeripheralToCentralCharacteristicUUID;

/**
 *  UUID of the Characteristic for the peripheral to tell the central to disconnect. Indicate and Read. The data is a one-byte integer and the value is not checked by the central.
 */
extern NSString *const BCChatPeripheralToCentralDisconnectRequestCharacteristicUUID;

请检查 BCConstants.m 以获取 UUIDs。

未来改进
  • 目前没有任何内容持久化。在将来,我们可以保存服务器名称、好友列表等。
  • 由于 CoreBluetooth 直接支持,消息中支持 140 个字符没有特殊处理。但如果应用程序需要与 Android 或其他设备配合使用,另一端可能无法处理如此长的包,需要开发某种应用程序级协议。
  • 在存在多个挂起写入操作时,没有简单的方法来跟踪写入结果。目前,应用程序将 CB 写入回调作为第一个挂起写入操作的结果,但这可能并不总是准确。再次需要应用程序级协议。
  • 为简化用户界面,连接仅限一对。但这不必太困难来支持多个聊天(仍然是 1 对 1)。
类图

Class diagram