gpt4 book ai didi

iOS 核心蓝牙 : centralManager:didConnectPeripheral/didFailToConnectPeripheral: not getting called

转载 作者:IT王子 更新时间:2023-10-29 08:00:13 27 4
gpt4 key购买 nike

我正在努力解决这个问题。我正在尝试连接到 BLE 设备,但在下面的代码中看不到我做错了什么。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}


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

+ (NSString*)UUIDString:(CFUUIDRef)uuid {
CFStringRef string = CFUUIDCreateString(NULL, uuid);
return (__bridge_transfer NSString*)string;
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals];
}
}

- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
// NSLog(@"Received peripheral : \n%@", peripheral);
// NSLog(@"Adv data : %@", advertisementData);

[peripheral setDelegate:self];
[central connectPeripheral:peripheral options:nil];
[peripheral readRSSI];
}

- (int)scanForPeripherals {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];

[_cm scanForPeripheralsWithServices:nil options:options];
return 0;
}

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

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"failed to connect");
}

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(@"didReadRSSI");
}

这些设备不是我自己的。我不知道它的邻近 UUID,但据我所知,通过 CoreBluetooth 连接不需要它,对吗?

所有设备都在 didDiscoverPeripheral: 中发现,在我尝试连接它们的选择器中。但是之后什么也没有。

当我调用 didDiscoverPeripheral: 时,我是否期待与配对密码请求的对话?如果是这样,我没有看到任何对话框,这是为什么?

从 apple 文档中,它清楚地指出,在尝试连接到设备后,您应该调用 didConnectPeripheraldidFailToConnectPeripher 但我没有收到。

有什么想法吗?我已经尝试了将近一个星期了。感谢您的帮助,谢谢。

最佳答案

如果您不以某种方式保留传递给 didDiscoverPeripheralperipheral 对象,那么一旦此委托(delegate)方法退出,它就会被释放,您将无法获得连接。

我建议添加一个属性来跟踪发现的外围设备

@property (strong,nonatomic) NSMutableArray *peripherals;

viewDidLoadinit 中初始化

self.peripherals=[NSMutableArray new];

然后在didDiscoverPeripheral中添加外设到它

-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
[self.peripherals addObject:peripheral];
[central connectPeripheral:peripheral options:nil];
}

关于iOS 核心蓝牙 : centralManager:didConnectPeripheral/didFailToConnectPeripheral: not getting called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26377470/

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