菜鸟教程小白 发表于 2022-12-12 22:12:01

ios - 获取设备信号强度


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

<p>我目前正在使用此代码从状态栏中获取运营商和 wifi,我想知道是否有其他方法或更好的方法?还有,3g 和 4g 怎么买?</p>

<pre><code>UIApplication *app = ;
NSArray *subviews = [[ valueForKey:@&#34;foregroundView&#34;] subviews];
NSString *dataNetworkItemView = nil;
NSString *wifiNetworkItemView = nil;

for (id subview in subviews) {
    if(]) {
      dataNetworkItemView = subview;
    }
    if(]) {
      wifiNetworkItemView = subview;
    }
}

int carrierSignalStrength = [ intValue];
int wifiSignalStrength = [ intValue];
</code></pre>

<p>我使用的任何方法是否是私有(private)的都没有关系。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用 CoreTelephony 和 <code>CTTelephonyCenter</code> 观察者:</p>

<pre><code>#include &lt;CoreTelephony/CoreTelephony.h&gt;

// 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(&amp;raw, &amp;graded, &amp;bars);

    printf(&#34;Signal strength changed! Raw: %li, graded: %li bars: %li\n&#34;, raw, graded, bars);
    // Prints something like:
    // Signal strength changed! Raw: -96, graded: 27 bars: 3
}
</code></pre>

<p>在另一个函数中注册处理程序:</p>

<pre><code>// 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();
</code></pre>

<p>改编自 <a href="http://iphonedevwiki.net/index.php/CTIndicators" rel="noreferrer noopener nofollow">iPhone Dev Wiki article on CTIndicators</a> .</p>

<p>我相信这些方法不再存在于任何高于 8.4 (?) 的 iOS SDK 中。要访问它们,请创建一个新的 header 来外部函数和常量:</p>

<pre><code>#include &lt;CoreFoundation/CoreFoundation.h&gt;

#if __cplusplus
extern &#34;C&#34; {
#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
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 获取设备信号强度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39655325/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39655325/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 获取设备信号强度