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

时间:2014-10-17 04:50:29

标签: ios objective-c core-bluetooth

我正在开发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应用程序中得到的响应。以上代码代表我的知识。如果可能,请通过代码或任何其他示例帮助我。

1 个答案:

答案 0 :(得分:0)

您应该检查properties的{​​{1}}属性 - 它会告诉您如何处理该特征。从这个特征不支持您正在阅读的消息 - 您只能写入它,或者,如果它是要向您的中心发送信息,那么您可能必须使用通知或指示({{1} }和/或CBCharacteristic设置在CBCharacteristicPropertyNotify)。

如果是这种情况,那么您需要使用CBCharacteristicPropertyIndicate当设备有此特征的新数据时,它将调用properties委托方法。