rockyliao
rockyliao

Reputation: 33

How to connect to the Accessory correctly with BluetoothManager?

I am trying to connect an accessory in my application without doing setting in “setting->bluetooth”.

I have followed the steps from this link: http://www.pocketmagic.net/2012/07/bluetooth-and-ios-use-bluetooth-in-your-iphone-apps/#.UTKqLKVvdha It's running well until I get the message “failed with error 305″ when I try to connect to the accessory.

Here is my list of steps:

  1. Get a handler and instance of the BluetoothManager service:

    btManager = [BluetoothManager sharedInstance];
    
  2. Register for notifications, for Bluetooth radio change (on/off) and for discovering a new device:

    // setup bluetooth notifications
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(deviceDiscovered:)
     name:@"BluetoothDeviceDiscoveredNotification"
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(bluetoothAvailabilityChanged:)
     name:@"BluetoothAvailabilityChangedNotification"
     object:nil];
    

    Here the application can discover the accessory in

    - (void)deviceDiscovered:(NSNotification *) notification
    
  3. Connect to the accessory using the BluetoothDevice method like [btdev connect]. Here I get the message

    "connection to service 0x00001000 on device "accessory" F0:B4:79:0B:68:45 failed with error 305".

I have tried the other methods like [acceptSSP], [connectWithServices] but it didn't help. Do I have to pair first? If so, how should I do that?

Upvotes: 2

Views: 2146

Answers (4)

Nick
Nick

Reputation: 101

I Hope this would help you,you can try with service tag 0x00002000

BluetoothManager *btManager =  [[self bluetoothScanner] bluetoothManager];
[btManager setDevicePairingEnabled:YES];
[btManager connectDevice:bluetoothDevice withServices:0x00002000];

Upvotes: 1

Bretton Wade
Bretton Wade

Reputation: 957

Note that in order to get this to compile, you will have to add the following line to the BluetoothManager class definition:

-(void)acceptSSP:(NSInteger)errorCodeShouldBeZero forDevice:(id)device;

Upvotes: 1

user2614952
user2614952

Reputation: 11

I've been getting the "failed with error 305" for about a week now. I finally solved it by setting the BluetoothManager as connectable before scanning for services. I haven't seen a reference to this setting in any other forum. Hope it helps.

[btManager setConnectable:YES];
[btManager scanForServices:0xFFFFFFFF];

Upvotes: 1

lucianoenrico
lucianoenrico

Reputation: 1494

to accept pairing requests:

 [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(bluetoothPairingUserNumericComparision:)
 name:@"BluetoothPairingUserNumericComparisionNotification"
 object:nil];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(bluetoothPairingPINResultSuccess:)
 name:@"BluetoothPairingPINResultSuccessNotification"
 object:nil];

then

    - (void)bluetoothPairingUserNumericComparision:(NSNotification *)notification {

    NSLog(@"NOTIFICATION:bluetoothPairingUserNumericComparision: called. BT State: %d", [btManager enabled]);
    NSLog(@"NOTIFICATION: %@", notification);

    NSDictionary *dict = notification.object;

    BluetoothDevice *device = dict[@"device"];
    NSNumber *value = dict[@"value"];

    NSLog(@"device: %@ value: %@", device, value);

    [btManager acceptSSP:0 forDevice:device];
}

- (void)bluetoothPairingPINResultSuccess:(NSNotification *)notification {

    NSLog(@"NOTIFICATION: bluetoothPairingPINResultSuccess called.");
    NSLog(@"NOTIFICATION: %@", notification);
    NSLog(@"paired devices: %@", [btManager pairedDevices]);

}

Now the device is paired. But i'm stuck here because I cannot connect or exchange data.

Upvotes: 1

Related Questions