2016-09-23 101 views
2

我正在尝试以dBm为载波,wifi,3G和4G的信号强度。获取设备信号强度

我目前使用此代码从状态栏获取运营商和无线网络,我想知道是否有其他方式或更好的方法?另外我怎样才能得到它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]; 

如果我使用的任何方法是私人的或不是私人的。

回答

2

使用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中。要访问它们,创建一个新的标头为extern的函数和常量:

#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 
+0

谢谢!你认为这会被认为是私人的吗?因为它会更好,如果这是可以接受的appStore – razvan

+2

这绝对是一个私人API。为了在一个现代的SDK上编译,我不得不手动地扩展一些'CoreTelephony'常量和函数。我将编辑我的答案以包含这些声明。 – JAL

0

我使用私有的API太..但我从(可见)状态栏此信号强度。

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

希望这有助于!