12

I have OBD2 device adapter which supports Wifi. Now I want to get notification about Wifi with OBD2 device is available so I can start talking to that device and read data and Wifi with OBD2 device is not available.

When device is connected to OBD2 Port,wifi is broadcasting. I have used sample code of Reachability class. But I can't get proper notification.

I tried with SimplePingHelper code. It works fine with Main thread but it not runs with background thread. SimplePingHelper Source code

SimplePingHelper code actually uses SimplePing sample code from Apple. SimplePing Code By Apple

Can you help me in this code that it works with background thread Or What are other ways I can check to get this notification ?

NSS
  • 711
  • 1
  • 9
  • 23

3 Answers3

1

`Use Apple's default reachability class:

Download reachability project from This link

Copy Reachability.h and Reachability.m file in your project.

And set this method in Application delegate file.

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

}

bLacK hoLE
  • 681
  • 1
  • 7
  • 20
0

Use Apple's default reachability class:

Download reachability project from This link

Copy Reachability.h and Reachability.m file in your project.

And set this method in Application delegate file.

-(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];    
}

For getting Reachability change notification use below code:

Add this notification method in 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];
}

and also add this method:

- (void)reachabilityChanged: (NSNotification* )note
{
    NSLog(@"Reachability changed");
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
   [self updateInterfaceWithReachability: curReach];
}
gavriln
  • 19
  • 3
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38
  • I have used the same code as you have mentioned..But it's not working with Wifi Enabled Device – NSS Nov 16 '13 at 06:46
0

Reachability only detect the network availability of your iOS device itself.
In addition, Apple doesn't allow apps to do any Wi-Fi scans, so there is no public API to do so.

Is your OBD2 device adapter connected to iOS device physically by cable or by local Wi-Fi?
I think you should focus on how to communicate to the external OBD2 device in iOS.

Davis Cho
  • 230
  • 3
  • 12