ios – 通过UUID编写CBC特征

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

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

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

配置为具有正确的UUID的CBC特征对象.

我已经尝试使用正确的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
               }
           }
       }
 }

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...