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中构建框架
PresenceInsightsSDK.xcodeproj
。PresenceInsightsSDK-Universal
目标。输出
文件夹中拖取构建的框架。注意:您在使用该框架之前需要构建此框架。
要使用框架,简单地将PresenceInsightsSDK.framework文件拖到您项目目标常规标签页的嵌入的二进制文件部分。选择“如有必要则复制项”。这将自动将该框架添加到所有其他必要的位置。然后,要在您的代码中使用它,请添加以下内容
在Swift中
import PresenceInsightsSDK
或在Objective-C中
#import <PresenceInsightsSDk/PresenceInsightsSDK.h>
#import <PresenceInsightsSDk/PresenceInsightsSDK-Swift.h>
注意:在Objective-C中,您还需要在目标构建设置中将“嵌入内容包含Swift代码”设置为“是”。
有两个类负责大部分重载工作:PIAdapter和PIBeaconSensor。
注意:所有示例都将使用Swift作为首选语言。
首先,您需要初始化一个适配器
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.
})
初始化适配器后,您就可以初始化一个信标传感器,并开始感知信标。
var piBeaconSensor = PIBeaconSensor(adapter: piAdapter)
piBeaconSensor.start()
注意: 要使用PI信标感知,您需要修改Info.plist。添加以下键,并设置您想要显示的消息值
这样,您就可以使应用程序开始将设备位置发送回您的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
我开始启动信标传感器,但它没有检测到任何信标。出现这种情况可能有几个原因。
当应用程序处于后台或未打开时,我该如何发送位置事件?