我一直在努力实现与BLE设备连接的蓝牙模块。当谈到执行时,它表明这里有

EXC_BREAKPOINT(EXC_ARM_BREAKPOINT ..那个)

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

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

停止线是

    _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;
}

最佳答案

之所以得到这个,是因为您的应用正在创建CBPeripheral。所有CBPeripherals应该由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时会出现此错误。

    关于ios - iOS在didSelectRowAtIndexPath连接到BLE设备EXC_BREAKPOINT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24749648/

    10-09 05:57