gpt4 book ai didi

iOS/objective-C : BLE in Peripheral mode throws `libsystem_kernel.dylib` __abort_with_payload`

转载 作者:行者123 更新时间:2023-11-29 00:26:56 25 4
gpt4 key购买 nike

我正尝试在 Central 中启动 BLE和 Peripheral模式。为了简单起见,现在使用硬编码变量。
我想我已经根据文档实现了所有内容。我无法检查中央模式是否有效,但是当代码仅包含与中央模式相关的代码时,该应用程序要求启用 BT,并且没有崩溃,所以我认为它正在正确扫描 :) 我会检查稍后。

在我在那里添加外设模式后,一旦我启动 start 方法,应用程序就开始卡住。

Xcode 显示非常有用的日志:

libsystem_kernel.dylib`__abort_with_payload:
0x18acc5d6c <+0>: movz x16, #0x209
0x18acc5d70 <+4>: svc #0x80
-> 0x18acc5d74 <+8>: b.lo 0x18acc5d8c ; <+32>
0x18acc5d78 <+12>: stp x29, x30, [sp, #-16]!
0x18acc5d7c <+16>: mov x29, sp
0x18acc5d80 <+20>: bl 0x18acaa7d0 ; cerror_nocancel
0x18acc5d84 <+24>: mov sp, x29
0x18acc5d88 <+28>: ldp x29, x30, [sp], #16
0x18acc5d8c <+32>: ret

没有别的,没有错误,没有警告等。这与中央模式无关,因为如果我删除它,没有任何变化。
那么,这是什么意思呢?我用谷歌搜索了一下,发现了关于权限描述、iOS 7 等的杂项假设。

我对 objective-c 几乎不熟悉,很抱歉,如果我问的是一个非常简单的问题 :)

下面代码中的

sendEventWithName 就像日志 atm 一样工作。我收到的唯一日志是 started(在 start 方法的末尾)

这是.h:

#import <RCTBridgeModule.h>
#import <RCTEventEmitter.h>
@import CoreBluetooth;
@import QuartzCore;

@interface BTManager : RCTEventEmitter <RCTBridgeModule, CBCentralManagerDelegate, CBPeripheralManagerDelegate, CBPeripheralDelegate>

@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@property (nonatomic, strong) CBMutableCharacteristic *transferCharacteristic;

@end

.m:

#import "BTManager.h"

@implementation BTManager

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents
{
return @[@"BTManagerDeviceFound", @"BTManagerStatus"];
}

- (void)viewDidLoad
{
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.centralManager = centralManager;
CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
self.peripheralManager = peripheralManager;
}

RCT_EXPORT_METHOD(start:(NSDictionary *)options)
{

[self.centralManager scanForPeripheralsWithServices:nil options:nil];

self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"EB6727C4-F184-497A-A656-76B0CDAC633A"] properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];

CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"EB6727C4-F184-497A-A656-76B0CDAC633A"] primary:YES];

transferService.characteristics = @[self.transferCharacteristic];

[self.peripheralManager addService:transferService];

[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:@"FB694B90-F49E-4597-8306-171BBA78F846"]] }];

[self sendEventWithName:@"BTManagerStatus" body:@"started"];
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
// log peripheralManager state
NSLog(@"peripheralManagerDidUpdateState peripheral %@", peripheral);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
{
// log centralManager state
NSLog(@"peripheralManager didAddService peripheral %@", peripheral);
NSLog(@"peripheralManager didAddService service %@", service);
NSLog(@"peripheralManager didAddService error %@", error);
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
{
NSLog(@"peripheralManagerDidStartAdvertising peripheral %@", peripheral);
NSLog(@"peripheralManagerDidStartAdvertising error %@", error);
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
[self sendEventWithName:@"BTManagerDeviceFound" body:advertisementData];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"centralManagerDidUpdateState central %@", central);
}

RCT_EXPORT_METHOD(stop:(NSDictionary *)options)
{
// remove all related processes, send event to js
[self sendEventWithName:@"BTManagerStatus" body:@"details here"];
// [self.myCentralManager stopScan];
}

@end

最佳答案

一些想法:

  1. 代码声明 CBCentralManager *centralManagerCBPeripheralManager *peripheralManager作为 start 方法内的局部变量。 当方法结束时,这些变量将超出范围并不再存在,从而阻止蓝牙操作继续。您需要将它们声明为类级变量,然后在 start 方法中初始化它们。在相关主题上,请确保任何代码创建并执行您的 BTManager允许对象存活足够长的时间以执行蓝牙操作。如果它超出范围或被垃圾收集,您也会遇到同样的问题。

  2. 在收到开机回调之前,代码不应在中央设备或外围设备上执行操作:peripheralManagerDidUpdateStatecentralManagerDidUpdateState , 分别。我会将扫描或设置广告的代码移动到外围设备/中央设备的新初始化方法,并在您收到对这些方法的回调,其值为 CBPeripheralManagerStatePoweredOn 时调用它们。或 CBCentalManagerStatePoweredOn , 分别。即使在您开始编码时接口(interface)已开机,CoreBluetooth 也不会让您成功执行任何操作,直到它完全初始化并发送开机回调,因此您必须始终等待这一点。

  3. 确保您信任您的自定义日志记录实现。如有疑问,NSLog(@"XXXX") 是一个方便的健全性检查工具。

关于iOS/objective-C : BLE in Peripheral mode throws `libsystem_kernel.dylib` __abort_with_payload`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com