【问题标题】:Write CBCharacteristic by UUID通过 UUID 写入 CBCharacteristic
【发布时间】:2013-08-07 23:08:05
【问题描述】:

我正在尝试使用 CoreBluetooth 写入特定的已知特征。我觉得这应该是可能的,因为我使用了德州仪器 BLE 实用程序,您可以在连接的外围设备上选择“写入值”操作,只需输入特征 UUID 和您要写入的值,它执行没有问题。

据我了解,为了做到这一点,我必须打电话给

[peripheral writeValue:data forCharacteristic:...];

将 CBCharacteristic 对象配置为具有正确的 UUID。

我尝试使用正确的 UUID 制作 CBMutableCharacteristic,甚至对我所知道的是此外围配置文件中的现有特征的正确权限,并且在尝试使用以下命令执行任何读/写操作时出现以下崩溃外设上的那个特性:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CBMutableCharacteristic peripheral]: unrecognized selector sent to instance 0x1ed57690'

这是我用来设置和写入特征的代码:

NSString *characteristicUUIDstring = @"fff1";
CBUUID *characteristicUUID = [CBUUID UUIDWithString:characteristicUUIDstring];

char dataByte = 0x10;
NSData *data = [NSData dataWithBytes:&dataByte length:1];

CBMutableCharacteristic *testCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite value:data permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];

[peripheral writeValue:data forCharacteristic:testCharacteristic type:CBCharacteristicWriteWithResponse];

【问题讨论】:

    标签: ios core-bluetooth


    【解决方案1】:

    虽然这样的特征可能存在于外围设备中,但这并不是它所知道的实际特征。您需要发现所有服务和特征,然后直接从这些服务和字符中写入。

    //Assuming you've already discovered all your services and characteristics, do something like this:
    
    for(CBService *service in peripheral.services)
    {
          if([service.UUID isEqual:theServiceCBUUIDYouAreLookingFor])
          {
               for(CBCharacteristic *charac in service.characteristics)
               {
                   if([charac.UUID isEqual:theCharCBUUIDYoureLookingFor])
                   {
                       //NOW DO YOUR WRITING/READING AND YOU'LL BE GOOD TO GO
                   }
               }
           }
     }
    

    【讨论】:

    • 即使使用其他 BLE 实用程序也可以实现这种行为,但我必须真正发现它的特性吗?如果不能以这种方式使用CBMutableCharacteristic 和CBMutableService 的接口,那么它们的意义何在?
    • CBMutableCharacteristics 和 CBMutableServices 在那里,您可以在 iOS 设备上模拟外围设备。它们仅存在于 peripheralManager 中使用。您指的是什么 ble 实用程序?
    • Texas Instruments BTool 具有“使用特征 UUID 读取”和使用特征句柄写入特征的能力。
    • 特征句柄是什么意思?就像他们用 CBMutableCharacteristic 写的一样?你有这个代码的链接吗?
    • Characteristics 有句柄,我没有任何代码,因为它是从德州仪器 (Texas Instruments) 下载的用于在 Windows 上运行的实用程序
    【解决方案2】:

    请注意,如果您在 GATT 特征中使用“authenticated_read”或“authenticated_write”属性,您可能无法发现 CBCharacteristic

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2014-03-17
      • 1970-01-01
      相关资源
      最近更新 更多