首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取设备信号强度

获取设备信号强度
EN

Stack Overflow用户
提问于 2016-09-23 15:40:52
回答 2查看 2.7K关注 0票数 2

我正在尝试获取运营商、wifi、3g和4g的dBm格式的信号强度。

我目前正在使用这个代码从状态栏中获取运营商和wifi,我想知道是否有其他方法或更好的方法?另外,我怎样才能在3g和4g上获得它?

代码语言:javascript
复制
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];

我使用的任何方法是否是私有的都无关紧要。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-20 04:30:54

使用CoreTelephony和CTTelephonyCenter观察器:

代码语言:javascript
复制
#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
}

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

代码语言:javascript
复制
// 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中。我相信。要访问这些函数和常量,请使用创建一个新的标头,以将函数和常量外部化:

代码语言:javascript
复制
#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
票数 2
EN

Stack Overflow用户

发布于 2016-12-02 02:26:24

我也使用私有API..但这个信号强度是从(可见的)状态栏获得的。

代码语言:javascript
复制
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
NSString *signalStrengthView = nil;

for (id subview in subviews) {
    NSLog(@"Class - %@", NSStringFromClass([subview class]));

    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
        signalStrengthView = subview;
    }

    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
    }
}

int signalStrength = [[signalStrengthView valueForKey:@"signalStrengthRaw"] intValue];
NSLog(@"signal %d", signalStrength);

int wifiStrength = [[dataNetworkItemView valueForKey:@"wifiStrengthRaw"] intValue];
NSLog(@"wifi %d", wifiStrength);

希望这能有所帮助!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39655325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档