获取有关启用Wifi的设备的Wifi可用性的通知

NSS*_*NSS 12 cfnetwork wifi ios

我有OBD2设备适配器,支持Wifi.现在我想获得有关带OBD2设备的Wifi的通知,因此我可以开始与该设备通信并读取数据,并且无法使用OBD2设备进行Wifi.

当设备连接到OBD2端口时,wifi正在广播.我使用了Reachability类的示例代码.但我无法得到适当的通知.

我尝试使用SimplePingHelper代码.它适用于主线程,但它不与后台线程运行.SimplePingHelper源代码

SimplePingHelper代码实际上使用Apple的SimplePing示例代码. SimplePing Code由Apple提供

你可以帮助我在这个代码中使用后台线程吗?或者我可以通过其他方式来获取此通知?

Pra*_*inh 0

使用 Apple 的默认可达性类别:

从此链接下载可达性项目

将 Reachability.h 和 Reachability.m 文件复制到您的项目中。

并在应用程序委托文件中设置此方法。

-(void)initializeRechabilityObeserver
{ 
    //Change the host name here to change the server your monitoring
    hostReach = [Reachability reachabilityWithHostName: @"www.apple.com <http://www.apple.com>"];
    [hostReach startNotifier];
    //[self updateInterfaceWithReachability: hostReach];

    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    //[self updateInterfaceWithReachability: internetReach];

    wifiReach = [Reachability reachabilityForLocalWiFi] ;
    [wifiReach startNotifier];
    //[self updateInterfaceWithReachability: wifiReach];    
}
Run Code Online (Sandbox Code Playgroud)

要获取可达性更改通知,请使用以下代码:

在Application didFinishLaunching中添加此通知方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    internetReachable = [Reachability reachabilityForInternetConnection] ;
    [internetReachable startNotifier];
}
Run Code Online (Sandbox Code Playgroud)

并添加这个方法:

- (void)reachabilityChanged: (NSNotification* )note
{
    NSLog(@"Reachability changed");
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
   [self updateInterfaceWithReachability: curReach];
}
Run Code Online (Sandbox Code Playgroud)