2016-09-23 2 views
2

이동 통신사, wifi, 3g 및 4g의 신호 강도를 dBm으로 얻으려고합니다.기기 신호 강도 가져 오기

현재이 코드를 사용하여 상태 표시 줄에서 이동 통신사와 무선 랜을 얻고 있으며 다른 방법이 있는지 또는 더 좋은 방법이 있는지 알고 싶습니다. 또한 어떻게 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에서 적응.

이러한 방법은 더 이상 iOS SDK에서 8.4 (?)보다 더 이상 없습니다. 에 액세스하려면 함수와 상수를 통근 할 수있는 새 헤더 생성 : 나는 너무 개인 API를 사용

#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' 상수와 함수를 extern해야했습니다. 이 선언을 포함하도록 내 대답을 편집합니다. – JAL

0

을 ..하지만 난 (표시) 상태 표시 줄에서이 신호 강도를 얻을.

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

희망이 있습니다.