Berkanan SDK Tweet

build Contributions Contributor Covenant

Berkanan SDK 支持附近应用之间的蓝牙网状网络消息传递。它是 Berkanan Messenger (Product Hunt, TechCrunch) 和 Berkanan Messenger Lite (GitHub) 使用的框架。

目标是创建一个由人们设备蓝牙天线驱动的去中心化网状网络,服务于大众。人们可以在紧急情况等没有其他连接可用的情况下依赖此网络进行短信通讯——它实际上可以拯救生命。

使用 Berkanan SDK,应用可以发现附近也集成了该 SDK 并开启了蓝牙的应用,并向它们发送小消息。这些消息的范围约为 70 米,但由于 SDK 会在收到消息后自动重新发送,因此可以传播得更远。使用 Berkanan SDK 的应用越多,消息的覆盖范围就越广。

功能和限制

隐私政策

Berkanan SDK 不会将消息发送到任何中央服务器或公司。

将 Berkanan SDK 集成到您的应用中

iOS

要将 Berkanan SDK 集成到您的 iOS 应用中,请使用 Xcode 11 或更高版本。打开您应用的 .xcodeproj 或 .xcworkspace 文件,并按照以下步骤操作。

配置您的应用目标

选择您的应用目标,然后转到 Editor / Add Capability / Background Modes。同时勾选 Uses Bluetooth LE accessoriesActs as a Bluetooth LE accessory

转到 Signing & Capabilities / App Sandbox 并勾选 Bluetooth 复选框。

NSBluetoothAlwaysUsageDescriptionNSBluetoothPeripheralUsageDescription 添加到 Info.plist,值为

允许访问 Berkanan 蓝牙服务,以便即使在离线时也能通信。

将 Berkanan SDK 添加到您的应用

转到 File / Swift Packages / Add Package Dependency... 并输入 https://github.com/zssz/BerkananSDK.git

在您需要的源文件中添加 @import BerkananSDK

在您的应用中使用 Berkanan SDK

初始化一个本地服务,并配置要广播的内容
let configuration = Configuration(
  // The identifier is used to identify what kind of configuration the service has. 
  // It should be the same across app runs.
  identifier: UUID(uuidString: "3749ED8E-DBA0-4095-822B-1DC61762CCF3")!, 
  userInfo: "My User Info".data(using: .utf8)!
)
// Throws if the configuration is too big or invalid.
let localService = try BerkananBluetoothService(configuration: configuration)
启动本地服务
localService.start()
发现附近的服务及其广播的配置
let discoverServiceCanceller = localService.discoverServiceSubject
  .receive(on: RunLoop.main)
  .sink { remoteService in
    print("Discovered \(remoteService) with \(remoteService.getConfiguration())")
}
构建包含有效负载类型标识符和有效负载的消息
let message = Message(
  // The payloadType is used to identify what kind of payload the message carries.
  payloadType: UUID(uuidString: "E268F3C1-5ADB-4412-BE04-F4A04F9B3D1A")!,
  payload: "Hello, World!".data(using: .utf8)
)
发送消息
// Throws if the message is too big or invalid.
try localService.send(message)
接收消息
let receiveMessageCanceller = localService.receiveMessageSubject
  .receive(on: RunLoop.main)
  .sink { message in
    print("Received \(message.payloadType) \(message.payload)")
}
停止本地服务
localService.stop()

示例应用

要查看 Berkanan Messenger Lite 应用 如何集成 Berkanan SDK,请查看其 源代码