目标C语言 iOS.方法调用SIGABRT信号



我正在使用CoreBluetooth,但discoverCharacteristics方法有问题。

我初始化了我的服务和特征如下:

m_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
CBUUID *uuid = [CBUUID UUIDWithString:ServiceUUID];
m_services = [NSArray arrayWithObject:uuid];
CBMutableCharacteristic *characteristics = [CBMutableCharacteristic alloc];
characteristics = [characteristics initWithType:uuid
                                     properties:CBCharacteristicPropertyWriteWithoutResponse
                                          value:nil
                                    permissions:CBAttributePermissionsWriteable];
m_characteristics = [NSArray arrayWithObject:characteristics];

我的ServiceUUID@"ffe0"

- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
    m_scanLabel = [UILabel new];
    [self addLabel:m_scanLabel topPadding:TextFieldFirstPadding];
    if (central.state == CBCentralManagerStatePoweredOn)
    {
        m_scanLabel.text = @"Launch scan";
        m_timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(scan)
                                                 userInfo:nil
                                                  repeats:YES];
    }
    else
    {
        m_scanLabel.text = @"Error connection. Check bluetooth state";
        [m_timer invalidate];
    }
    [m_scanLabel sizeToFit];
}
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    // todo check right peripheral
    m_peripheralLabel = [UILabel new];
    [self addLabel:m_peripheralLabel topPadding:CGRectGetMaxY(m_scanLabel.frame)];
    [m_timer invalidate];
    if (m_peripheral == nil)
    {
        m_peripheral = peripheral;
        m_peripheralLabel.text = @"Connecting...";
        [m_peripheralLabel sizeToFit];
        [m_centralManager connectPeripheral:peripheral options:nil];
    }
    else
    {
        [m_centralManager retrieveConnectedPeripheralsWithServices:m_services];
    }
}
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [m_centralManager stopScan];
    peripheral.delegate = self;
    m_didConnectLabel = [UILabel new];
    [self addLabel:m_didConnectLabel topPadding:CGRectGetMaxY(m_peripheralLabel.frame)];
    m_didConnectLabel.text = @"Connected. Launch discover";
    [m_didConnectLabel sizeToFit];
    [peripheral discoverServices:m_services];
}
- (void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    m_timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                               target:self
                                             selector:@selector(scan)
                                             userInfo:nil
                                              repeats:YES];
}
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    for (CBService *service in peripheral.services)
    {
        [peripheral discoverCharacteristics:m_characteristics forService:service];
    }
}

应用程序在这个地方崩溃:[peripheral discoverCharacteristics:m_characteristics forService:service];

使用这样的日志(这里我使用1802服务,但结果相同):

* thread #1: tid = 0x1710ca, 0x0000000199e15d3c libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x0000000199e15d3c libobjc.A.dylib`objc_exception_throw
frame #1: 0x000000018d9befdc CoreFoundation`+[NSException raise:format:] + 128
frame #2: 0x000000018d688464 CoreBluetooth`-[CBXpcConnection allocXpcObjectWithNSObject:] + 556
frame #3: 0x000000018d688590 CoreBluetooth`-[CBXpcConnection allocXpcArrayWithNSArray:] + 252
frame #4: 0x000000018d6883e0 CoreBluetooth`-[CBXpcConnection allocXpcObjectWithNSObject:] + 424
frame #5: 0x000000018d6887b4 CoreBluetooth`-[CBXpcConnection allocXpcDictionaryWithNSDictionary:] + 360
frame #6: 0x000000018d687b48 CoreBluetooth`-[CBXpcConnection allocXpcMsg:args:] + 104
frame #7: 0x000000018d687bd4 CoreBluetooth`-[CBXpcConnection sendAsyncMsg:args:] + 32
frame #8: 0x000000018d687c20 CoreBluetooth`-[CBXpcConnection sendMsg:args:] + 36
frame #9: 0x000000010002daa0 SampleApp`-[ViewController peripheral:didDiscoverServices:](self=0x0000000126e04900, _cmd=0x00000001913f147c, peripheral=0x00000001700bb120, error=0x0000000000000000) + 364 at ViewController.m:121
frame #10: 0x000000018d682c08 CoreBluetooth`-[CBPeripheral handleServicesDiscovered:] + 756
frame #11: 0x000000018d67ed88 CoreBluetooth`-[CBCentralManager xpcConnection:didReceiveMsg:args:] + 136
frame #12: 0x000000018d687e38 CoreBluetooth`__34-[CBXpcConnection handleMsg:args:]_block_invoke + 84
frame #13: 0x000000019a3e4420 libdispatch.dylib`_dispatch_call_block_and_release + 24
frame #14: 0x000000019a3e43e0 libdispatch.dylib`_dispatch_client_callout + 16
frame #15: 0x000000019a3e756c libdispatch.dylib`_dispatch_main_queue_callback_4CF + 344
frame #16: 0x000000018d97ed64 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
frame #17: 0x000000018d97d0a4 CoreFoundation`__CFRunLoopRun + 1452
frame #18: 0x000000018d8bdb38 CoreFoundation`CFRunLoopRunSpecific + 452
frame #19: 0x000000019329f830 GraphicsServices`GSEventRunModal + 168
frame #20: 0x00000001908fc0e8 UIKit`UIApplicationMain + 1156
frame #21: 0x000000010002e7e0 SampleApp`main(argc=1, argv=0x000000016fdd7ca8) + 116 at main.m:16
frame #22: 0x000000019a3ffaa0 libdyld.dylib`start + 4

我做错了什么?

问题是您传递给peripheral discoverCharacteristics的m_characteristics是cbmutablecharatures的数组,当它应该是cbuuid的数组时。所以,你应该有

m_characteristics = [NSArray arrayWithObject:uuid];

其中uuid是从您的特征uuid字符串创建的CBUUID。