iOS在didSelectRowAtIndexPath连接到BLE设备EXC_BREAKPOINT

Raju yourPepe

我一直在努力实现与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,否则[CBPeripheral new]释放CBPeripheral时会出现此错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS 8中的EXC_BREAKPOINT(SIGTRAP)

来自分类Dev

iOS-将BLE设备连接到应用和iOS

来自分类Dev

iOS如何在后台重新连接到BLE设备?

来自分类Dev

NSSearchPathForDirectoriesInDomains提供EXC_BREAKPOINT

来自分类Dev

iOS 13.4 CoreData SwiftUI应用程序在设备上崩溃并显示“ EXC_BREAKPOINT(code = 1,subcode = 0x1f3751f08)”

来自分类Dev

Android BLE按名称连接到设备

来自分类Dev

Android BLE按名称连接到设备

来自分类Dev

_CFRunLoopServiceMachPort中的EXC_BREAKPOINT代码= 1

来自分类Dev

使用onesignal时为EXC_BREAKPOINT

来自分类Dev

iOS崩溃,EXC_BREAKPOINT,Xcode 6.1毫无头绪

来自分类Dev

传递int EXC_BREAKPOINT(code = 1 Swift Xcode iOS时出错

来自分类Dev

SwiftUI-WKWebView的iOS 13 UIViewRepresentable获取线程1:EXC_BREAKPOINT崩溃

来自分类Dev

我应该如何将多个BLE外设连接到iOS设备?

来自分类Dev

连接到BLE设备并通过iOS 7上的NSNetService发送/读取数据

来自分类Dev

.hash给EXC_BREAKPOINT(代码= EXC_ARM_BREAKBOINT

来自分类Dev

连接到BLE设备后如何获取电池电量?

来自分类Dev

拔出电池后重新连接到BLE设备

来自分类Dev

发现后Windows UWP连接到BLE设备

来自分类Dev

在Windows 10上连接到BLE外围设备

来自分类Dev

连接到ble设备时是否可以启动服务/活动?

来自分类Dev

将BLE设备连接到Android发送数据(nUART)

来自分类Dev

仅在Asus标签中无法发现并连接到ble设备

来自分类Dev

Swift数组访问触发器EXC_BREAKPOINT

来自分类Dev

应用程序崩溃,出现EXC_BREAKPOINT错误

来自分类Dev

尝试在SwiftUI中加载视图时,exc_breakpoint

来自分类Dev

Swift数组访问触发器EXC_BREAKPOINT

来自分类Dev

什么可能导致Swift sharedInstance返回EXC_BREAKPOINT

来自分类Dev

Xcode:EXC_BREAKPOINT(EXC_ARM_BREAKPOINT,子代码= 0xe7ffdefe)

来自分类Dev

从ios设备连接到本地主机

Related 相关文章

  1. 1

    iOS 8中的EXC_BREAKPOINT(SIGTRAP)

  2. 2

    iOS-将BLE设备连接到应用和iOS

  3. 3

    iOS如何在后台重新连接到BLE设备?

  4. 4

    NSSearchPathForDirectoriesInDomains提供EXC_BREAKPOINT

  5. 5

    iOS 13.4 CoreData SwiftUI应用程序在设备上崩溃并显示“ EXC_BREAKPOINT(code = 1,subcode = 0x1f3751f08)”

  6. 6

    Android BLE按名称连接到设备

  7. 7

    Android BLE按名称连接到设备

  8. 8

    _CFRunLoopServiceMachPort中的EXC_BREAKPOINT代码= 1

  9. 9

    使用onesignal时为EXC_BREAKPOINT

  10. 10

    iOS崩溃,EXC_BREAKPOINT,Xcode 6.1毫无头绪

  11. 11

    传递int EXC_BREAKPOINT(code = 1 Swift Xcode iOS时出错

  12. 12

    SwiftUI-WKWebView的iOS 13 UIViewRepresentable获取线程1:EXC_BREAKPOINT崩溃

  13. 13

    我应该如何将多个BLE外设连接到iOS设备?

  14. 14

    连接到BLE设备并通过iOS 7上的NSNetService发送/读取数据

  15. 15

    .hash给EXC_BREAKPOINT(代码= EXC_ARM_BREAKBOINT

  16. 16

    连接到BLE设备后如何获取电池电量?

  17. 17

    拔出电池后重新连接到BLE设备

  18. 18

    发现后Windows UWP连接到BLE设备

  19. 19

    在Windows 10上连接到BLE外围设备

  20. 20

    连接到ble设备时是否可以启动服务/活动?

  21. 21

    将BLE设备连接到Android发送数据(nUART)

  22. 22

    仅在Asus标签中无法发现并连接到ble设备

  23. 23

    Swift数组访问触发器EXC_BREAKPOINT

  24. 24

    应用程序崩溃,出现EXC_BREAKPOINT错误

  25. 25

    尝试在SwiftUI中加载视图时,exc_breakpoint

  26. 26

    Swift数组访问触发器EXC_BREAKPOINT

  27. 27

    什么可能导致Swift sharedInstance返回EXC_BREAKPOINT

  28. 28

    Xcode:EXC_BREAKPOINT(EXC_ARM_BREAKPOINT,子代码= 0xe7ffdefe)

  29. 29

    从ios设备连接到本地主机

热门标签

归档