繁体   English   中英

iOS:将命令发送到外围设备并从中接收响应

[英]iOS:Send Command To peripheral and Receive response From it

我正在开发iOS中的核心蓝牙应用程序。 我有智能手表设备,正在连接到我开发的应用程序,目的是能够从其中发现服务和特征,当我尝试从手表中读取信息时,会出现以下错误

 Bluetooth_iph[2688:614669] Did discover peripheral. peripheral: <CBPeripheral: 0x17daf4d0,   
  identifier = D675EA0B-1342-0C74-9D3D-98CAFA478985, name = BLEDEVICE, state = connecting> 
   rssi: -51, UUID: <CFUUID 0x17d96e80> D675EA0B-1342-0C74-9D3D-98CAFA478985     
  advertisementData: {
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = "BLEDEVICE";
kCBAdvDataServiceUUIDs =     (
    8880
);
kCBAdvDataTxPowerLevel = 7;

  } 
 2014-10-17 10:06:13.695 Bluetooth_iph[2688:614669] WARNING: No service found
 2014-10-17 10:06:18.353 Bluetooth_iph[2688:614669] Peripheral Connected
 2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669]  started  time is 10:06:10 17-10-14
 2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669] Scanning stopped
 2014-10-17 10:06:18.485 Bluetooth_iph[2688:614669] Found a Device Manufacturer Name 
 2014-10-17 10:06:18.541 Bluetooth_iph[2688:614669] Error updating value for characteristic   
 8881 error: Reading is not permitted.

 2014-10-17 10:06:18.601 Bluetooth_iph[2688:614669] Error updating value for characteristic 
 8881 error: Reading is not permitted.

 2014-10-17 10:06:18.662 Bluetooth_iph[2688:614669] Error updating value for characteristic 
 8881 error: Reading is not permitted.

为了发现服务的特性,我编写如下。

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:
  (CBService   
 *)service error:(NSError *)error

  {
  if (error)
  {
    NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error 
     localizedDescription]);
    return;
  }


   if([service.UUID isEqual:[CBUUID UUIDWithString:@"8880"]])
  {
    for (CBCharacteristic * characteristic in service.characteristics)
    {
        /* Read manufacturer name */
        if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"8881"]])
        {
            [_discoveredPeripheral readValueForCharacteristic:characteristic];
            NSLog(@"Found a Device Manufacturer Name Characteristic - Read manufacturer 
      name");
        }
    }
  }

   if ( [service.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]] )
    {
    for (CBCharacteristic *characteristic in service.characteristics)
    {
        /* Read device name */
        if([characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDDeviceNameString]])
        {
            [_discoveredPeripheral readValueForCharacteristic:characteristic];
            NSLog(@"Found a Device Name Characteristic - Read device name");
        }
     }
  }
  }

并为特征更新值

 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:  
  (CBCharacteristic *)characteristic error:(NSError *)error
 {
 if (error)
  {
     NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID,   
   [error localizedDescription]);
    return;
  }
}

作为iOS开发的新目标,我的要求是我必须向ble设备发出一些请求,以便它能够响应,并且应该在iOS应用程序中得到该响应。 以上代码代表我的知识。 请通过代码或其他示例帮助我。

您应该检查CBCharacteristicproperties属性-它会告诉您可以使用该特性做什么。 从此功能不支持您正在阅读的消息-要么只能写它,或者如果它是向中央发送信息,则可能必须使用通知或指示( CBCharacteristicPropertyNotify和/或在properties设置的CBCharacteristicPropertyIndicate )。

如果是这种情况,则需要使用[peripheral setNotifyValue:YES forCharacteristic:characteristic]; 当设备具有针对该特性的新数据时,它将调用peripheral:didUpdateNotificationStateForCharacteristic:error:委托方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM