2

キャリア、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];

私が使用するメソッドがプライベートかどうかは問題ではありません。

4

2 に答える 2

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

CTIndicators に関する iPhone Dev Wiki 記事からの転載。

これらのメソッドは、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
于 2016-10-19T20:30:53.893 に答える
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);

お役に立てれば!!

于 2016-12-01T18:26:23.750 に答える