为什么使用coreBluetooth connectPeripheral没有在IOS8中调用委托方法

时间:2015-01-27 10:07:46

标签: ios objective-c ios8 core-bluetooth

我想在 IOS8 中使用 CoreBluetooth.framework 来实现数据传输,我确实在以下方法中发现了外设并尝试连接外围设备。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discover name : %@", peripheral.name);
    [self.centralManager connectPeripheral:peripheral options:nil];
}

但它没有调用委托方法 didFailToConnectPeripheral didConnectPeripheral ,我对它有什么问题,是代码错误还是IOS8需要一些额外的东西?如何处理,提前谢谢!

这是github中的代码,我将程序编写为服务器,另一个编写为Central。

3 个答案:

答案 0 :(得分:9)

@Sj评论为我解决了这个问题,将其复制成答案以防万一有人会错过它:

  

您是否尝试将外围物体存放在ivar中然后连接到它?即self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];

答案 1 :(得分:3)

我正在使用TemperatureSensor iOS sample app,其中didDiscoverPeripheral没有调用。通过引用Heart Rate Monitor Mac sample app的流程。请确保代码的运行顺序为:

    app = angular.module 'myapp', ['ng-bootstrap-datepicker']

AppCtrl = ($scope)->
  $scope.datepickerOptions =
    format: 'yyyy-mm-dd'
    language: 'fr'
    autoclose: true
    weekStart: 0

  $scope.date = '2000-03-12'

app.controller 'AppCtrl', AppCtrl    
angular.bootstrap document, ['myapp']
之后将调用

1. centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; 2. - (void) centralManagerDidUpdateState:(CBCentralManager *)central 3. [centralManager scanForPeripheralsWithServices:...

答案 2 :(得分:0)

仅调用connect方法一次并等待连接委托响应。您发布的上述代码将更频繁地调用connect方法(当它发现外围设备时)

if(!self.detectedBLE){ [self.centralManager stopScan]; self.detectedBLE = peripheral; [self.centralManager connectPeripheral:self.detectedBLE options:nil]; }