IBMPIBeacon 2.0.2

IBMPIBeacon 2.0.2

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

Ciaran Hannigan维护。



  • IBM公司

基于iOS的Presence Insights SDK

Presence Insights正在退役。请访问我们的文档获取更多信息。

此仓库包含PresenceInsightsSDK的源代码,这是一个iOS SDK,允许轻松集成在Bluemix上可用的IBM Presence Insights服务。

此SDK支持iOS 8及以上版本。

功能

  • 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

在PI中注册PIDevice

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.    
})

从PI中注销PIDevice

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用户界面中的信标配置可能不正确。请确保已正确设置邻近UUID。我们使用它来创建用于信标测距的区域。
    2. 创建PIAdapter时使用的代码(用户名、密码、租户、组织)可能有误。
  • 当应用程序处于后台或未打开时,我该如何发送位置事件?