OStack程序员社区-中国程序员成长平台

标题: ios - 获取设备信号强度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:12
标题: ios - 获取设备信号强度

我正在尝试获取载波、wifi、3g 和 4g 的信号强度(以 dBm 为单位)。

我目前正在使用此代码从状态栏中获取运营商和 wifi,我想知道是否有其他方法或更好的方法?还有,3g 和 4g 怎么买?

UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey"statusBar"] valueForKey"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
NSString *wifiNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
        dataNetworkItemView = subview;
    }
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        wifiNetworkItemView = subview;
    }
}

int carrierSignalStrength = [[dataNetworkItemView valueForKey"signalStrengthRaw"] intValue];
int wifiSignalStrength = [[wifiNetworkItemView valueForKey"wifiStrengthRaw"] intValue];

我使用的任何方法是否是私有(private)的都没有关系。



Best Answer-推荐答案


使用 CoreTelephony 和 CTTelephonyCenter 观察者:

#include <CoreTelephony/CoreTelephony.h>

// Event handler
static void SignalStrengthDidChange(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    long int raw = 0;
    long int graded = 0;
    long int bars = 0;

    CTIndicatorsGetSignalStrength(&raw, &graded, &bars);

    printf("Signal strength changed! Raw: %li, graded: %li bars: %li\n", raw, graded, bars);
    // Prints something like:
    // Signal strength changed! Raw: -96, graded: 27 bars: 3
}

在另一个函数中注册处理程序:

// Register as a listener to the kCTIndicatorsSignalStrengthNotification notification to be notified when the signal strength changed.
CTTelephonyCenterAddObserver(CTTelephonyCenterGetDefault(), NULL, SignalStrengthDidChange, kCTIndicatorsSignalStrengthNotification, NULL, CFNotificationSuspensionBehaviorCoalesce);

// Get the initial strength.
SignalStrengthDidChange();

CFRunLoopRun();

改编自 iPhone Dev Wiki article on CTIndicators .

我相信这些方法不再存在于任何高于 8.4 (?) 的 iOS SDK 中。要访问它们,请创建一个新的 header 来外部函数和常量:

#include <CoreFoundation/CoreFoundation.h>

#if __cplusplus
extern "C" {
#endif

#pragma mark - API

    /* This API is a mimic of CFNotificationCenter. */

    CFNotificationCenterRef CTTelephonyCenterGetDefault();
    void CTTelephonyCenterAddObserver(CFNotificationCenterRef center, const void *observer, CFNotificationCallback callBack, CFStringRef name, const void *object, CFNotificationSuspensionBehavior suspensionBehavior);
    void CTTelephonyCenterRemoveObserver(CFNotificationCenterRef center, const void *observer, CFStringRef name, const void *object);
    void CTTelephonyCenterRemoveEveryObserver(CFNotificationCenterRef center, const void *observer);

    void CTIndicatorsGetSignalStrength(long int *raw, long int *graded, long int *bars);

#pragma mark - Definitions

    /* For use with the CoreTelephony notification system. */
    extern CFStringRef kCTIndicatorsSignalStrengthNotification;

#if __cplusplus
}
#endif

关于ios - 获取设备信号强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655325/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4