iphoneobjective-csocketsbluetoothdns-sd

Usage example to connect two iOS devices over bluetooth? (client/server code)


I do not want to use GameKit. I am trying to use a low-level approach using dns_sd.h and methods like DNSServiceRegister to establish and maintain a connection. However, I found a wrapper for this called HHServices... Honestly at this point either a low-level answer using dns_sd or a high-level answer using HHServices is welcome.

Here is HHServices. Here is GCDAsyncSocket.

This is the code I have so far. I am able to successfully publish the HHService and the GCDAsyncSocket, and also start browing for HHServices on the other device, but no connection is made. Any help? :D

Server Side

Here's where I publish the HHService:

//....
NSUInteger serverPort = 19997;
HHServicePublisher *publisher = [[HHServicePublisher alloc] initWithName:@"MyDisplayName" type:@"_myexampleservice._tcp." domain:@"local." txtData:nil port:serverPort];

GCDAsyncSocket *listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

NSError *error = nil;
if (![listenSocket acceptOnPort:serverPort error:&error])
{
    [self alert:@"Error starting GCDAsyncsocket"];
}


publisher.delegate = self;
if(![publisher beginPublish])
{
    [self alert:@"Error publishing HHService"];
}
else
{
    [self alert:@"Successfully published HHService"];
}

This is where I see if a client connected:

- (void)socket:(GCDAsyncSocket *)sender didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    [self alert:@"didAcceptNewSocket!"];
}

Client-side

Where I start browsing

    HHServiceBrowser *browser = [[HHServiceBrowser alloc] initWithType:@"_myexampleservice._tcp." domain:@"local."];
    browser.delegate = self;
    if(![browser beginBrowse])
    {
        [self alert:@"Couldn't start browsing"];
    }
    else
    {
        [self alert:@"Successfully started browsing"];
    }

Callback for finding the published HHService:

- (void) serviceBrowser:(HHServiceBrowser*)serviceBrowser didFindService:(HHService*)service moreComing:(BOOL)moreComing {
    [self alert:@"Found a service! "];
}

Could someone shed some light as to what I'm doing wrong?


Solution

  • HHServices only handles discovery. It's up to you to make the connection. You need to resolve the service, then open a socket, like it shows in the HHServices example.