2013-11-14 40 views
12

我有支持Wifi的OBD2設備適配器。現在我想通過OBD2設備獲得有關Wifi的通知,因此我可以開始與該設備通話並讀取數據,並且使用OBD2設備的Wifi不可用。獲取WiFi啓用設備的Wifi可用性通知

當設備連接到OBD2端口時,wifi正在廣播。我使用了Reachability類的示例代碼。但我無法得到正確的通知。

我試着用SimplePingHelper代碼。它適用於主線程,但不能與後臺線程一起運行。 SimplePingHelper Source code

SimplePingHelper代碼實際上使用Apple的SimplePing示例代碼。 SimplePing Code By Apple

你能幫我在這個代碼,它與後臺線程工作嗎?或者我可以檢查以獲取此通知的其他方式是什麼?

回答

0

使用蘋果的默認可達性類:從您的項目This link

複製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];  
} 

對於下面的代碼獲得可達性變化的通知應用:

在應用didFinishLaunching添加該通知方法

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

,並添加這個方法:

- (void)reachabilityChanged: (NSNotification*)note 
{ 
    NSLog(@"Reachability changed"); 
    Reachability* curReach = [note object]; 
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 
    [self updateInterfaceWithReachability: curReach]; 
} 
+0

我已經使用了相同的代碼,你mentioned..But它不與WiFi工作啓用設備 – NSS

0

可達只檢測您的iOS設備本身的網絡可用性。
此外,蘋果不允許應用程序做任何Wi-Fi掃描,所以沒有公共API來做到這一點。

是您的OBD2設備物理適配器通過電纜或通過本地Wi-Fi連接到iOS設備?
我想你應該專注於如何與iOS中的外部OBD2設備進行通信。

1

`使用蘋果的默認可達性類:

下載可達性項目從項目This link

複製Reachability.h和Reachability.m文件。

並在應用程序委託文件中設置此方法。

- (無效)initializeRechabilityObeserver {// 更改主機名此處更改服務器的監控 hostReach = [可達reachabilityWithHostName:@「www.apple。COM http://www.apple.com「]; [hostReach startNotifier]; // [自updateInterfaceWithReachability:hostReach];

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

wifiReach = [Reachability reachabilityForLocalWiFi] ; 
[wifiReach startNotifier]; 
//[self updateInterfaceWithReachability: wifiReach];  

}