【问题标题】:Get device signal strength获取设备信号强度
【发布时间】:2016-09-23 07:40:52
【问题描述】:

我正在尝试获取载波、wifi、3g 和 4g 的信号强度(以 dBm 为单位)。

我目前正在使用此代码从状态栏中获取运营商和 wifi,我想知道是否有其他方法或更好的方法?还有,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];

我使用的任何方法是否是私有的都没有关系。

【问题讨论】:

    标签: ios iphone 3g iphone-privateapi 4g


    【解决方案1】:

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

    #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
    

    【讨论】:

    • 谢谢!你认为这会被认为是私人的吗?因为如果这对于 appStore 来说是可以接受的,那就更好了
    • 这绝对是一个私有API。为了让它在现代 SDK 上编译,我必须手动外部化一些 CoreTelephony 常量和函数。我将编辑我的答案以包含这些声明。
    • @JAL 我想获取附近 WiFi 网络的接收信号强度。我可以使用上面的代码吗?是否有任何限制,例如。越狱的设备还是iOS版本的呢?谢谢。
    • 在 iOS 12 中:信号强度发生了变化!原始:0,分级:100 条:0 .
    【解决方案2】:

    我也使用私有 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);
    

    希望这会有所帮助!

    【讨论】:

    • 在 iOS 12 中载波信号为 0。
    猜你喜欢
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多