AWARE: 连接性
连接性传感器提供设备网络传感器的可用性信息。这包括 Wi-Fi、蓝牙、GPS、移动、推通知、低电量模式、后台刷新状态和网络可用性。此传感器可用来检测设备上无线传感器和网络在任何时间点的可用性。
需求
iOS 10 或更高版本
安装
com.awareframework.ios.sensor.connectivity 通过 CocoaPods 提供。
- 要安装它,只需将以下行添加到您的 Podfile
pod 'com.awareframework.ios.sensor.connectivity'
- 将 com.awareframework.ios.sensor.connectivity 库导入到您的源代码中。
import com_awareframework_ios_sensor_connectivity
- 将
UIRequiresPersistentWiFi
添加到Info.plist
公共函数
连接传感器
init(config:连接传感器.Config?)
: 使用可选配置初始化连接传感器。start()
: 使用可选配置启动连接传感器。stop()
: 停止服务。
连接传感器.Config
用于存储传感器配置的类。
字段
sensorObserver: 连接观察者
: 实时数据更新的回调。interval: Int
连接检查间隔(以分钟为单位)(默认 =10
)enabled: Boolean
传感器是否启用。 (默认 =false
)debug: Boolean
启用/禁用将日志记录到Xcode控制台。 (默认 =false
)label: String
数据的标签。 (默认 = "")deviceId: String
将与其事件和传感器相关联的设备ID。 (默认 = "")dbEncryptionKey
数据库的加密密钥。 (默认 =null
)dbType: Engine
用于保存数据的数据库引擎。 (默认 =Engine.DatabaseType.NONE
)dbPath: String
数据库路径。 (默认 = "aware_connectivity")dbHost: String
同步数据库的宿主。 (默认 =null
)
广播
已激发的广播
连接传感器.ACTION_AWARE_WIFI_ON
: 激活Wi-Fi时激发。连接传感器.ACTION_AWARE_WIFI_OFF
: 禁用Wi-Fi时激发。连接传感器.ACTION_AWARE_MOBILE_ON
: 激活移动网络时激发。ConnectivitySensor.ACTION_AWARE_MOBILE_OFF
:当移动网络关闭时触发。ConnectivitySensor.ACTION_AWARE_BLUETOOTH_ON
:当蓝牙开启时触发。ConnectivitySensor.ACTION_AWARE_BLUETOOTH_OFF
:当蓝牙关闭时触发。ConnectivitySensor.ACTION_AWARE_GPS_ON
:当GPS开启时触发。ConnectivitySensor.ACTION_AWARE_GPS_OFF
:当GPS关闭时触发。ConnectivitySensor.ACTION_AWARE_INTERNET_AVAILABLE
:当设备连接到互联网时触发。额外包括一项以提供活跃的互联网接入网络ConnectivitySensor.EXTRA_ACCESS
:一个整数,包含以下常量之一:1=Wi-Fi,4=移动
ConnectivitySensor.ACTION_AWARE_INTERNET_UNAVAILABLE
:当设备没有连接到互联网时触发。
接收到的 广播
ConnectivitySensor.ACTION_AWARE_CONNECTIVITY_START
:接收到启动传感器的广播。ConnectivitySensor.ACTION_AWARE_CONNECTIVITY_STOP
:接收到停止传感器的广播。ConnectivitySensor.ACTION_AWARE_CONNECTIVITY_SYNC
:接收到发送同步尝试到主机的广播。ConnectivitySensor.ACTION_AWARE_CONNECTIVITY_SET_LABEL
:接收到设置数据标签的广播。数据标签位于intent extras的ConnectivitySensor.EXTRA_LABEL
字段中。
数据表示
连接数据
包含连接数据。
字段 | 类型 | 描述 |
---|---|---|
type | Int | 连接类型,以下选项之一:-1=AIRPLANE, 1=WIFI, 2=BLUETOOTH, 3=GPS, 4=MOBILE, 5=WIMAX, 6=PUSH_NOTIFICATION, 7=LOW_POWER_MODE, 8=BACKGROUND_REFRESH |
subtype | String | 类型的文本标签,以下选项之一:AIRPLANE, WIFI, BLUETOOTH, GPS, MOBILE, WIMAX, PUSH_NOTIFICATION, LOW_POWER_MODE, BACKGROUND_REFRESH |
state | Int | 网络状态 1=ON, 0=OFF |
deviceId | String | AWARE 设备 UUID |
label | String | 可自定义的标签。用于数据校准或可追溯性 |
timestamp | Long | 自1970年以来的Unix时间毫秒 |
timezone | Int | 设备的时区 |
os | String | 设备的操作系统(例如,ios) |
注意:iOS不支持AIRPLANE(-1)和WIMAX(5)。
示例用法
let connectivity = ConnectivitySensor.init(ConnectivitySensor.Config().apply{config in
config.debug = true
config.dbType = .REALM
config.sensorObserver = Observer()
})
connectivity?.start()
class Observer:ConnectivityObserver {
func onInternetON() {
// Your code here
}
func onInternetOFF() {
// Your code here
}
func onGPSON() {
// Your code here
}
func onGPSOFF() {
// Your code here
}
func onBluetoothON() {
// Your code here
}
func onBluetoothOFF() {
// Your code here
}
func onBackgroundRefreshON() {
// Your code here
}
func onBackgroundRefreshOFF() {
// Your code here
}
func onLowPowerModeON() {
// Your code here
}
func onLowPowerModeOFF() {
// Your code here
}
func onPushNotificationOn() {
// Your code here
}
func onPushNotificationOff() {
// Your code here
}
func onWiFiON() {
// Your code here
}
func onWiFiOFF() {
// Your code here
}
}
作者
Yuuki Nishiyama, [email protected]
许可
版权所有(c)2018 AWARE Mobile Context Instrumentation Middleware/Framework (http://www.awareframework.com)
遵照Apache License,版本2.0(“许可证”);除非符合许可证规定或经书面同意,否则不得使用此文件。您可以在以下位置获取许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0 除非适用法律要求或经书面同意,否则按照“现状”方式在许可证下分发软件,不提供任何形式的明示或暗示担保。有关许可的具体语言、权限和限制,请参阅许可证。