IBMPICore 2.0.2

IBMPICore 2.0.2

测试已测试
语言语言 SwiftSwift
许可证 自定义
发布最新发布2016年7月
SPM支持 SPM

Ciaran Hannigan 维护。



IBMPICore 2.0.2

  • IBM Corp.

针对 iOS 的存在洞察 SDK

存在洞察将被弃用。请访问 我们的文档 了解更多信息。

此仓库包含存在洞察 SDK 的源代码,这是一个 iOS SDK,它允许轻松集成可在 Bluemix(功能

  • BLE 信号发射器传感器 - 监控区域、信标范围并将信标通知消息发送到 PI

  • 管理配置 REST - 向管理配置服务器发送调用以获取有关您组织的信息

  • 设备注册 - 可以轻松地将智能手机或平板电脑注册到您的组织中。


入门

构建

然后从您的 Carthage/Build/iOS 目录中获取 PresenceInsightsSDK.framework

如果您使用的是 CocoaPods,请将以下内容添加到您的 Podfile

use_frameworks!

pod 'PresenceInsightsSDK'

或者,要在 Xcode 中构建框架

  1. 打开 PresenceInsightsSDK.xcodeproj
  2. 运行 PresenceInsightsSDK-Universal 目标。
  3. 输出 文件夹中提取构建的框架。

注意:您需要构建此框架才能使用它。

链接

要使用该框架,只需将 PresenceInsightsSDK.framework 文件拖放到您的项目目标的 通用 选项卡的 嵌入的二进制文件 部分。选择“如果需要复制项目”。这应自动将框架添加到所有其他所需位置。然后,为了在代码中使用该框架添加

在 Swift 中

import PresenceInsightsSDK

或在 Objective-C 中

#import <PresenceInsightsSDk/PresenceInsightsSDK.h>
#import <PresenceInsightsSDk/PresenceInsightsSDK-Swift.h>

注意:在 Objective-C 中,您还需要在目标构建设置中将“嵌入式内容包含 Swift 代码”设置为“是”。


使用 SDK

有两个类完成了大部分繁重的任务:PIAdapter 和 PIBeaconSensor。

注意:所有示例都将使用 Swift,因为它是首选语言。

PI 适配器

您需要做的第一件事是初始化一个适配器

var piAdapter = PIAdapter(tenant: <tenant>,
                             org: <org>,
                         baseURL: "https://presenceinsights.ibmcloud.com"
                        username: <username>,
                        password: <password>)

您可以从Bluemix的Presence Insights仪表板上获取您的租户、组织、用户名和密码。(我们建议您将它们放在.plist文件中以方便使用和修改。)

现在您可以查询来自Presence Insights的各种有用信息了!

  • 那么,向您的客户展示楼层平面图怎么样?
piAdapter.getMap(<site code>, floor: <floor code>, callback: {floorMap, error in
    // floorMap is of type UIImage
    // display it!
})
  • 您想获取该楼层上所有信标的列表并在您刚刚检索的地图上显示它们的位置吗?
piAdapter.getAllBeacons(<site code>, floor: <floor code>, callback: {beacons, error in
    // beacons is of type [PIBeacon]
    // use the x and y coords from each beacon obj to place them on the map.
})

PI信标传感器

初始化适配器后,您可以初始化一个信标传感器并开始检测信标。

var piBeaconSensor = PIBeaconSensor(adapter: piAdapter)
piBeaconSensor.start()

注意: 要使用PI信标检测,您需要修改Info.plist。添加以下键并设置您希望显示的消息

  • NSLocationAlwaysUsageDescription

这就是让应用程序开始将设备位置发送回您的Presence Insights实例的所有内容。

SDK默认每5秒发送一次您周围信标的信息。如果想要调整发送间隔(以毫秒为单位),这真的很简单。

piBeaconSensor.setReportInterval(10000)

要停止信标检测

piBeaconSensor.stop()


但是等等,还有更多!

尽管如此,此SDK不仅可以做到这一点。以下是一些您可以使用的其他功能,以让您的用户能够享受Presence Insights的益处。

设备处理

要创建一个PIDevice

var device = PIDevice(name: <your device name>)
device.type = "External" // these values can be found under Settings of your org in the UI

要将加密数据添加到PIDevice中

device.addToDataObject(<Custom Data Object>, key: <Custom Key>)

要将未加密数据添加到PIDevice中

device.addToUnencryptedDataObject(<Custom Data Object>, key: <Custom Key>)

将设备列入黑名单

device.blacklist = true

将PIDevice在PI中注册

piAdapter.registerDevice(device, callback: {newDevice, error in
    // newDevice is of type PIDevice.
    // Do whatever you want with your newDevice here.    
})

更新PI中的PIDevice

piAdapter.updateDevice(device, callback: {newDevice, error in
    // newDevice is of type PIDevice.
    // Do whatever you want with your newDevice here.    
})

将PIDevice从PI中注销

piAdapter.unregisterDevice(device, callback: {newDevice, error in
    // newDevice is of type PIDevice.
    // Do whatever you want with your newDevice here.    
})

获取PI中注册的所有设备的列表

piAdapter.getRegisteredDevices({devices, error in
    // devices is of type [PIDevice]
    // Do whatever you want with the devices array here.
})

从PI中获取特定设备

piAdapter.getDeviceByCode(<device code>, callback: {device, error in
    // device is of type PIDevice.
    // Do whatever you want with your device here.    
})

piAdapter.getDeviceByDescriptor(<device UUID>, callback: {device, error in
    // device is of type PIDevice.
    // Do whatever you want with your device here.    
})

信标传感器代理

我们公开了几个回调,以便您能够以您喜欢的方式处理信标事件,而不仅仅是按我们使用它们的方式。

extension <Your class name>: PIBeaconSensorDelegate {
    func didRangeBeacons(beacons: [CLBeacon]) {
        // Do whatever you want with the ranged beacons here.
    }
    func didEnterRegion(region: CLRegion) {
        // Do something with the region you just entered.
    }
    func didExitRegion(region: CLRegion) {
        // Do something with the region you just exited.
    }
}

其他

您可以使用此SDK做其他一些事情(主要是不同的方法来初始化或修改对象),但这些基础知识应该足以让您开始。祝您玩得开心!


故障排除

  • 首先,如果事情没有工作,请启用调试以在控制台查看所有内部操作。

    adapter.enableLogging() // adapter is an instance of PIAdapter
    
  • 我已经启动了信标传感器,但它没有检测到任何信标。可能发生这种情况有几个原因。

    1. 信标在PI UI中配置不正确。请确保已正确设置邻近范围UUID。我们检索它以创建用于信标范围的区域。
    2. 用于创建PIAdapter的代码(用户名、密码、租户、组织)可能输入有误。
  • 当应用程序处于后台或未打开时,我如何发送位置事件?