2016-09-23 105 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); 

希望這有助於!