gpt4 book ai didi

ios - 核心蓝牙 : unable to discover characteristics for discovered peripheral

转载 作者:行者123 更新时间:2023-11-28 18:56:05 26 4
gpt4 key购买 nike

我已经编写了一些 CoreBluetooth 代码,我可以发现设备,但我似乎无法发现我发现的外围设备的特征。谁有好的示例代码可供我用来验证我的代码?

这是我写的:

#import "ViewController.h"
@property(nonatomic, strong) CBCentralManager* centralmanager;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.centralmanager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
CBCentralManagerState currentState = central.state;
NSLog(@"state %i", currentState);

switch (central.state)
{
case CBCentralManagerStatePoweredOn:
{
NSDictionary *options = @{
CBCentralManagerScanOptionAllowDuplicatesKey: @YES
};
[self.centralmanager scanForPeripheralsWithServices:nil options:options];
NSLog(@"I just started scanning for peripherals");
break;
}
}
}
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"connected!");
}

- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
[self.centralmanager connectPeripheral:peripheral options:nil];

if ([[advertisementData description] containsString:@“keyword”]) {


NSLog(@"peripheral count %lu", (unsigned long)[peripheral.services count]);
[peripheral.services count];
for (int i=0; [peripheral.services count]; i++) {
for (CBService *s in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:s];
}
}
}
}


- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
NSLog(@"did discover characteristic for service");
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSArray *)serviceUuids{
NSLog(@"discovered a peripheral's services: %@", serviceUuids);
}

最佳答案

看这里: Sample Code

基本上流程是:

  1. 扫描设备
  2. 发现设备
  3. 连接到设备
  4. 向设备请求服务
  5. 向服务询问特征
  6. 写入、读取或接收特征通知

随后您不必搜索和发现设备,因为可以从缓存中检索并直接连接之前发现的设备。

编辑:--------------------

阅读范例:[aPeripheral readValueForCharacteristic:aChar];

通知示例:[aPeripheral setNotifyValue:YES forCharacteristic:aChar];

这两个调用都会导致 - (void) peripheral:(CBPeripheral *)aPeripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error- 当BLE 设备返回一个值。使用通知时,每次设备更新该特征的值时都会自动调用此回调。

关于ios - 核心蓝牙 : unable to discover characteristics for discovered peripheral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32566749/

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