Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /* The official example for CoreWLAN is now obsolete,
  2. so here's a small command-line example that works with Xcode 6 and Yosemite.
  3. It only demonstrates how to get basic wi-fi connection properties and scan.
  4. Enjoy!
  5. pavel_a@fastmail.fm 01-Mar-2015
  6. */
  7.  
  8. #import <Foundation/Foundation.h>
  9. #import <CoreWLAN/CoreWLAN.h>
  10. #include <stdio.h>
  11.  
  12. char const *phyModeName( enum CWPHYMode n )
  13. {
  14. switch( (int)n ) {
  15. case kCWPHYModeNone: return "none";
  16. case kCWPHYMode11n: return "802.11n";
  17. case kCWPHYMode11a: return "802.11a";
  18. case kCWPHYMode11ac: return "802.11ac";
  19. case kCWPHYMode11g: return "802.11g";
  20. case kCWPHYMode11b: return "802.11b";
  21. default: return "other/unknown";
  22.  
  23. }
  24. }
  25.  
  26. char const *intfModeName( enum CWInterfaceMode m )
  27. {
  28. switch( (int)m ) {
  29. case kCWInterfaceModeNone: return "none";
  30. case kCWInterfaceModeStation: return "STA";
  31. case kCWInterfaceModeIBSS: return "ADHOC";
  32. case kCWInterfaceModeHostAP: return "AP";
  33. default: return "unknown";
  34. }
  35. }
  36.  
  37. // Enumerate available wi-fi interfaces and print:
  38. void enumWifiInterfaces()
  39. {
  40. NSArray *aif = CWWiFiClient.interfaceNames;
  41. for ( NSString *s in aif) {
  42. NSLog(@"Ifname: %@\n", s);
  43. }
  44. }
  45.  
  46. // Convert NSstring to a "normal C" string, fast and dirty
  47. char const * toCstr(NSString *ns ) {
  48. return [ns cStringUsingEncoding:[NSString defaultCStringEncoding]];
  49. }
  50.  
  51. ///////////
  52. // - Register as CoreWLAN client
  53. // - get some connection properties
  54. // - Scan and display some neigbor info
  55. //////////
  56. int wlanTest()
  57. {
  58. CWWiFiClient *wfc = CWWiFiClient.sharedWiFiClient;
  59. //NSLog(@"client: %@\n", wfc);
  60. if ( !wfc ) {
  61. return 1;
  62. }
  63.  
  64. CWInterface *wif = wfc.interface; // get default interface
  65. if (!wif) {
  66. return 1;
  67. }
  68.  
  69. printf("Default wifi interface name: %s\n", toCstr(wif.interfaceName));
  70.  
  71. CWPHYMode phyMode = wif.activePHYMode;
  72. printf("Phy mode = %s\n", phyModeName((int)phyMode) );
  73. printf("Intf. mode: %s\n", intfModeName(wif.interfaceMode));
  74.  
  75. if ( wif.interfaceMode == kCWInterfaceModeStation ) {
  76. printf("RSSI with assoc. AP (dBm)=%d ", (int)wif.rssiValue); // returns 0 if not assoc. or error
  77. printf("Noise (dBm)=%d\n", (int)wif.noiseMeasurement); // -"-"-
  78. }
  79.  
  80. printf("Scanning...\n");
  81. NSError *err;
  82. // Nil for SSID does normal passive scan
  83. // Scan returns NSset of CWNetwork*
  84. NSSet *scanset = [wif scanForNetworksWithSSID:Nil error:&err];
  85. if (err) {
  86. printf("Scan failed, err=%ld\n", err.code);
  87. return 1;
  88. }
  89.  
  90. printf("Scan found %d networks\n", (int)scanset.count);
  91. if ( scanset.count != 0 ) {
  92. int ix = 1;
  93. for (CWNetwork * nw in scanset)
  94. {
  95. printf(" %d. Bssid=%s rssi=%ld on channel %ld\n", ix++, toCstr([nw bssid] ), (long)[nw rssiValue], (long)[[nw wlanChannel] channelNumber]);
  96. }
  97. }
  98.  
  99. return 0;
  100. }
  101.  
  102.  
  103. int main(int argc, const char * argv[])
  104. {
  105. @autoreleasepool {
  106. printf("Wi-Fi scan test on the default adapter\n");
  107. wlanTest();
  108. }
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement