rssi参数获取_使用私有API读取WiFi RSSI值

I'm working on something that will not need to be on the App store, so I have no issues with using private APIs to meet my needs

I'm trying to use the MobileWiFi. framework to read the RSSI value for the wireless network the phone is currently connected to. I've included thehttps://github.com/Cykey/ios-reversed-headers/tree/c613e45f3ee5ad9f85ec7d43906cf69ee812ec6a/MobileWiFi` headers and used a bridging header to include them in my swift project and wrote the code as follows. Please excuse me, I am a newbie.

import SystemConfiguration.CaptiveNetwork

typealias _WiFiManagerClientCreate = @convention(c) (CFAllocator, CInt) -> UnsafeRawPointer

typealias _WiFiManagerClientCopyDevices = @convention(c) (UnsafeRawPointer) -> CFArray

typealias _WiFiDeviceClientCopyProperty = @convention(c) (UnsafeRawPointer, CFString) -> CFPropertyList

if let libHandle = dlopen (Paths.ipConfiguration, RTLD_LAZY) {

result = libHandle.debugDescription

let _createManagerPtr = dlsym(libHandle, "WiFiManagerClientCreate")

let _clientCopyDevicesPtr = dlsym(libHandle, "WiFiManagerClientCopyDevices")

let _clientCopyPropertyPtr = dlsym(libHandle, "WiFiDeviceClientCopyProperty")

if (_createManagerPtr != nil) && (_clientCopyDevicesPtr != nil) && (_clientCopyPropertyPtr != nil) {

let _createManager = unsafeBitCast(_createManagerPtr, to: _WiFiManagerClientCreate.self)

let _clientCopyDevices = unsafeBitCast(_clientCopyDevicesPtr, to: _WiFiManagerClientCopyDevices.self)

let _clientCopyProperty = unsafeBitCast(_clientCopyPropertyPtr, to: _WiFiDeviceClientCopyProperty.self)

let manager = _createManager(kCFAllocatorDefault, 0)

let devices = _clientCopyDevices(manager)

let client = CFArrayGetValueAtIndex(devices, 0)

let data = _clientCopyProperty(client!, "RSSI" as CFString)

let rssi = CFDictionaryGetValue(data as! CFDictionary, "RSSI_CTL_AGR")

NSLog("RSSI: \(rssi)")

}

dlclose(libHandle)

}

Which yields an error fatal error: unexpectedly found nil while unwrapping an Optional value which stems from trying to call _createManager

解决方案

I ended up using this workaround:

+ (int) wifiStrength {

UIApplication *app = [UIApplication sharedApplication];

NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];

NSString *dataNetworkItemView = nil;

for (id subview in subviews) {

if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {

dataNetworkItemView = subview;

break;

}

}

return[[dataNetworkItemView valueForKey:@"wifiStrengthRaw"] intValue];

}

Works without any entitlements or jailbreaking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值