gpt4 book ai didi

ios - 通过 UUID 写入 CBCharacteristic

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:53 28 4
gpt4 key购买 nike

我正在尝试使用 CoreBluetooth 写入特定的已知特征。我觉得这应该是可能的,因为我使用了 Texas Instruments 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];

最佳答案

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

//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
}
}
}
}

关于ios - 通过 UUID 写入 CBCharacteristic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18088721/

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