【问题标题】:IOS connecting to BLE devices EXC_BREAKPOINT at didSelectRowAtIndexPathIOS 在 didSelectRowAtIndexPath 连接到 BLE 设备 EXC_BREAKPOINT
【发布时间】:2014-07-15 04:00:47
【问题描述】:

我一直致力于实现蓝牙模块以连接 BLE 设备。说到执行,就说明这里有

EXC_BREAKPOINT(EXC_ARM_BREAKPOINT..那个)

并在通过单击表格单元格调用方法 didSelectRowAtIndexPath 时突然退出。

是否有其他替代方案可以连接 BLE 设备或加载更多 BLE 设备信息。似乎如果 BLE 设备已经与其他 iOS 设备配对,我们无法启动配对和连接。真的吗 ?

停止线是

    _currentperipheral =    [_foundPeripherals objectAtIndex:indexPath.row];

下面是我的代码

#import "BluetoothTableViewController.h"
#import "BTSentCommandViewController.h"
#import "BTDevice.h";


@interface BluetoothTableViewController ()

@end

@implementation BluetoothTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _pendingInit = YES;

    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
    _currentperipheral=[[CBPeripheral alloc]init];
    _founddevice=FALSE;
    _foundPeripherals = [[NSMutableArray alloc] init];
    _connectedServices = [[NSMutableArray alloc] init];

}

- (void)viewWillDisappear:(BOOL)animated {

    [_centralManager stopScan];
    NSLog(@"Scanning stopped");
    [super viewWillDisappear:animated];
}


#pragma mark - Table view data source

- (void)tableView: (UITableView *)tableView
didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
    if( [_centralManager isEqual:nil ] ){
        NSLog(@"Bluetooth central manager is nil , cannot instantiate");
    }else{
        if( _centralManager.state != CBCentralManagerStatePoweredOn)
        {
            NSLog(@"Bluetooth not turned on");
        }else{

            _currentperipheral =    [_foundPeripherals objectAtIndex:indexPath.row];
            [_centralManager connectPeripheral: _currentperipheral options:nil]; // it's wrong
            BTSentCommandViewController* sliderVC= [self.storyboard instantiateViewControllerWithIdentifier:@"BTSentCommandViewController"];

            sliderVC.centralManager=_centralManager;
            sliderVC.periperhal= _currentperipheral;
            // sliderVC.delegate = self;
            [self presentViewController:sliderVC animated:NO completion:nil];
        }
    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_foundPeripherals count];
}



- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    // You should test all scenarios
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    if (central.state == CBCentralManagerStatePoweredOn) {
        // Scan for devices

        [_centralManager scanForPeripheralsWithServices:nil options:nil];
        NSLog(@"Scanning started");
    }
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

    //  NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);

    if ( ![_foundPeripherals containsObject:peripheral]) {

        //  BTDevice *myDevice=[[BTDevice alloc] init];
        // [myDevice setName: peripheral.name];
        //  NSString * macAddress =   [NSString stringWithFormat:@"%@" , peripheral.identifier];
        //  [myDevice setMacAddress: macAddress];

        [_foundPeripherals addObject: peripheral ] ;

        [self.tableView reloadData];
        // And connect
    }
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"Failed to connect");
    [self cleanup];
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"Connected");

    [_centralManager stopScan];
    NSLog(@"Scanning stopped");

    // [_data setLength:0];

    peripheral.delegate = self;

    [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }

    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
    }
    // Discover other characteristics
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }

    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}


- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    if (error) {
        NSLog(@"Error");
        return;
    }

    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    // Have we got everything we need?
    if ([stringFromData isEqualToString:@"EOM"]) {

        //[_textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];

        [peripheral setNotifyValue:NO forCharacteristic:characteristic];

        [_centralManager cancelPeripheralConnection:peripheral];
    }

    //[_data appendData:characteristic.value];
}


- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
        return;
    }

    if (characteristic.isNotifying) {
        NSLog(@"Notification began on %@", characteristic);
    } else {
        // Notification has stopped
        [_centralManager cancelPeripheralConnection:peripheral];
    }
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    _foundPeripherals = nil;

    [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}

- (void)cleanup {

    // See if we are subscribed to a characteristic on the peripheral
    if (_currentperipheral.services != nil) {
        for (CBService *service in _currentperipheral.services) {
            if (service.characteristics != nil) {
                for (CBCharacteristic *characteristic in service.characteristics) {
                    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
                        if (characteristic.isNotifying) {
                            [_currentperipheral setNotifyValue:NO forCharacteristic:characteristic];
                            return;
                        }
                    }
                }
            }
        }
    }

    [_centralManager cancelPeripheralConnection:_currentperipheral];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"reuseIdentifier";



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryNone;

    if (cell == nil) {
        cell =  [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"] ;

        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

    CBPeripheral *peripheral=[_foundPeripherals objectAtIndex:indexPath.row];
    NSString * macAddress =   [NSString stringWithFormat:@"%@%@" ,peripheral.name , peripheral.UUID  ];

    cell.textLabel.text = macAddress;
    // Configure the cell...
    // Configure the cell...

    return cell;
}

【问题讨论】:

  • 错误访问异常与核心蓝牙无关(特别是)。这意味着您正在访问不应该访问的内存 - 空指针或无效指针。异常断点或单步执行 didSelectRowAtIndexPath 应该可以帮助您隔离导致异常的行。

标签: ios bluetooth bluetooth-lowenergy


【解决方案1】:

您收到此信息是因为您的应用正在创建 CBPeripheral。所有 CBPeripheral 都应由 CoreBluetooth 框架创建。

在您的 CBCentralManager 上,使用以下方法获取 CBPeripheral:

  • - (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options

在您的委托上,这将调用 centralManager:didDiscoverPeripheral:advertisementData:RSSI: 并向您传递对 CBPeripheral 的引用。

  • - (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs

  • - (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)identifiers

这两个返回 CBPeripherals 数组(当前连接的或以前连接的)。

切勿使用[[CBPeripheral alloc] init] 或[CBPeripheral new] 创建 CBPeripheral,否则在释放 CBPeripheral 时会出现此错误。

【讨论】:

    【解决方案2】:

    为未知异常添加一点“注意事项” ...修改了代码..试试这个...不言自明

    - (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
    {
        if(_centralManager){
            if( _centralManager.state != CBCentralManagerStatePoweredOn)
            {
                NSLog(@"Bluetooth not turned on");
            }
            else
            {
                if ([_foundPeripherals count]> indexpath.row) {
                    _currentperipheral = [_foundPeripherals objectAtIndex:indexPath.row];
                    [_centralManager connectPeripheral: _currentperipheral options:nil]; // it's wrong
                    BTSentCommandViewController* sliderVC= [self.storyboard instantiateViewControllerWithIdentifier:@"BTSentCommandViewController"];
                    sliderVC.centralManager=_centralManager;
                    sliderVC.periperhal= _currentperipheral;
                    // sliderVC.delegate = self;
                    [self presentViewController:sliderVC animated:NO completion:nil];
                }
                else{
                    NSLog(@"Value doesn't exist in array");
                }
            }
        }
        else
        {
            NSLog(@"Bluetooth central manager is nil , cannot instantiate");
        }
    }
    

    【讨论】:

    • 我发誓所有变量都不为空,但它_centralManager 以某种方式被释放了
    • 而且错误仍然始于_currentperipheral = [_foundPeripherals objectAtIndex:indexPath.row];
    • @RajuGujarati 确保数组有值
    • 仍然失败,Lthiu。我在调试控制台的高亮行上方看到了[CBPeripheral release]
    • _foundPeripherals : 使其成为强参考
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2014-05-03
    相关资源
    最近更新 更多