【问题标题】:multiple BLE devices which have same services and characteristics in ios在 ios 中具有相同服务和特性的多个 BLE 设备
【发布时间】:2014-07-08 05:19:21
【问题描述】:

我有多个具有相同服务和特性的 BLE 设备。我可以扫描和连接多个设备。连接后,当我尝试通过发送命令来区分每一个时,它不起作用。它与单个设备完美配合。它类似于套接字连接吗?就像服务器产生子线程,每个客户端都可以通过线程保持连接。

请提供一些提示,说明如何在其他设备从设备读取数据时扫描每个设备。

-(void) scanDevice {

  centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

  [centralManager scanForPeripheralsWithServices:nil options:0];

 [AppDelegate app].cbCentral = centralManager;

}

-(void) stopScan {

    [[AppDelegate app].cbCentral stopScan];
}

-(void)connectToDevice:(CBPeripheral *)peripheral{


  [[AppDelegate app] cbCentral].delegate = self;
  [[[AppDelegate app] cbCentral] connectPeripheral:peripheral options:nil];

}

 -(void)calldiscoverServicesForPeripheral:(CBPeripheral *)peripheral{

  [peripheral setDelegate:self];
  [peripheral discoverServices:nil];

}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral     *)peripheral {

NSLog(@"Connected PERIPHERAL %d",peripheral.state);

  [delegate getConnectedPeripheral:peripheral];

  NSLog(@"Connected peripheral %@",peripheral);
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

   NSLog(@"Discovered servicea: %@", peripheral.services);

  for (CBService *service in peripheral.services) {
    NSLog(@"Discovered service: %@", [service.UUID data]);
    [peripheral discoverCharacteristics:nil forService:service];
  }
}

我会详细解释,

我有包含 BLE 设备的表格视图。第一次它是空的,所以我将通过调用“扫描设备”类来搜索设备。

此“扫描设备”类包含所有 Corebluetooth 方法,例如 CBCentralManager 分配、CBperipheral 委托方法。

搜索后,我会在表格视图中显示设备并连接到 BLE 设备。我正在从“扫描设备”类中获取一些数据。

现在,我想搜索更多设备来连接并获取数据。为此,我将在“ScanDevices”类中调用 [[CBCentralManager alloc] initWithDelegate:self]。此时,对于以前的设备(已连接和读取)显示警告“不是有效的外围设备”,新设备正在连接并从设备读取数据。

但我想同时从两个设备读取数据

请帮帮我... 谢谢

【问题讨论】:

  • 您应该向特定的 CBPeripheral 对象发送读写请求。你能显示那个代码吗?
  • 请检查问题,我已编辑问题的代码。
  • 哪一行给你警告? [delegate getConnectedPeripheral:] 是做什么的?您是否正在阅读或通知该特性?你在哪里做的?
  • 我从 viewcontroller 调用这些方法,当可用设备从核心蓝牙返回时,我正在从这个扫描类创建自己的 [delegate getConnectedPeripheral:] 到 viewcontroller。
  • 而且,我能够连接到一台设备并在 didDiscoverCharacteristics 中获取数据。但是,在读取数据时,我会再次搜索设备,我会调用 Scanperipherals,那时 coreblutooth 会发出警告“不是有效的外围设备”。

标签: ios core-bluetooth


【解决方案1】:

您不应该继续创建新的 CBCentral - 这样做会导致您之前的 CBCentral 被释放,从而使现有外围设备无效。

您应该激活一次扫描,例如在viewWillAppear 中,然后在viewWillDisappear 中停用它。

一旦您开始扫描并将您的代理设置到您的ScanDevices 类中,它就会在每次找到并连接新外围设备时调用[delegate getConnectedPeripheral:]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-08
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多