Category Archives: Uncategorized

OS X Found me Within 23 Feet

Same topic as my last post, only this time a OS X implementation. Which is a little more troubling because now I’m next door to iOS and therefore iPhones. And iPads. And iPod touches…

int main(int argc, const char * argv[])
{
 @autoreleasepool {

  //Get the WiFi
  NSMutableArray* wifiAccessPoints = [[NSMutableArray alloc] init];
  for(NSString *name in [CWInterface interfaceNames])
  {
   CWInterface* interface = [CWInterface interfaceWithName:name];
   //enumerate over all visible networks.
   for(CWNetwork *network in [interface scanForNetworksWithName:nil error:nil])
   {
    NSDictionary* dict = @{@"macAddress" : [network bssid],
                       @"signalStrength" : [NSNumber numberWithInteger: 
                                                   [network rssiValue]]};
    [wifiAccessPoints addObject:dict];
   }
  }

  //Create the request object;
  NSDictionary* dictRequest = @{@"wifiAccessPoints" : wifiAccessPoints};
  NSError* err = nil;
  NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictRequest
                                      options:NSJSONWritingPrettyPrinted
                                        error:&err];

  //Transmit the request.
  NSURL *url = [NSURL URLWithString:(Still Elided)];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  [request addValue:(Still Elided) forHTTPHeaderField:@"User-Agent"];
  [request setHTTPMethod:@"POST"];
  [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  [request addValue:[NSString stringWithFormat:@"%ld", jsonData.length] 
                            forHTTPHeaderField:@"Content-Length"];
  [request setHTTPBody:jsonData];

  err = nil;
  NSHTTPURLResponse *res = nil;
  NSData *locationData = [NSURLConnection sendSynchronousRequest:request 
                                               returningResponse:&res 
                                                           error:&err];
  if(!err)
  {
   //There I am!
   NSString* locationString = [[NSString alloc] initWithData:locationData 
                                            encoding:NSUTF8StringEncoding];
   NSLog(@"%@", locationString);
  }
 }
 return 0;
}

I’m also a little shocked by how short it is.