2013-03-03 82 views
1

我試圖在我的應用程序中連接一個附件,而沒有在「設置 - >藍牙」中進行設置。如何使用BluetoothManager正確連接到附件?

我按照以下鏈接中的步驟操作:http://www.pocketmagic.net/2012/07/bluetooth-and-ios-use-bluetooth-in-your-iphone-apps/#.UTKqLKVvdha運行良好,直到我嘗試連接到附件時收到消息「失敗,出現錯誤305」。

這裏是我的步驟列表:

  1. 獲取BluetoothManager服務的處理程序和實例:

    btManager = [BluetoothManager sharedInstance]; 
    
  2. 註冊通知,藍牙無線電的變化(開/關)和用於發現新設備:

    // 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]; 
    

    這裏的應用程序可以使用BluetoothDevice方法類似[btdev connect]發現在

    - (void)deviceDiscovered:(NSNotification *) notification 
    
  3. 連接到附件附件。在這裏,我收到消息

    「與設備上的服務0x00001000的連接」附件「F0:B4:79:0B:68:45失敗,錯誤305」。

我嘗試其他方法,如[acceptSSP][connectWithServices]但它並沒有幫助。我必須先配對嗎?如果是這樣,我該怎麼做?

回答

1

接受配對請求:

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

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

然後

- (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]); 

} 

現在該設備配對。但我因爲無法連接或交換數據而卡在這裏。

1

我已經收到「失敗,錯誤305」約一個星期了。在掃描服務之前,我最終通過將BluetoothManager設置爲可連接來解決此問題。我還沒有在任何其他論壇上看到過這個設置。希望能幫助到你。

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

注意的是,爲了得到這個代碼編譯,你必須將下列行添加到BluetoothManager類定義:

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

1

我希望這會幫助你,你可以試試帶服務標籤0x00002000

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