获得设备信号强度 [英] Get device signal strength

查看:221
本文介绍了获得设备信号强度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得载波,wifi,3g和4g的dBm信号强度。

I am trying to get the signal strength in dBm for the carrier, wifi, 3g, and 4g.

我目前正在使用此代码来获取运营商和从状态栏的wifi,我想知道是否有另一种方式或更好的方式?另外我怎么能得到3g和4g?

I am currently using this code to get the carrier's and wifi from the status bar and I would like to know if there is another way or a better way? Also how could I get it for 3g and 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];

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

It doesn't matter if any methods I use are private or not.

推荐答案

使用CoreTelephony和 CTTelephonyCenter 观察员:

Use CoreTelephony and CTTelephonyCenter observers:

#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 the handler in another function:

// 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();

改编自关于CTIndicators的iPhone Dev Wiki文章

这些方法不再适用于任何大于8.4的iOS SDK(?)我相信。要访问它们,请使用extern函数和常量创建一个新标题:

These methods are no longer in any iOS SDKs greater than 8.4 (?) I believe. To access them, create a new header with to extern the functions and constants:

#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

这篇关于获得设备信号强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆