Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write CBCharacteristic by UUID

I am attempting to use CoreBluetooth to write to a specific, known characteristic. I feel this should be possible as I have used a Texas Instruments BLE utility that you can select a "write value" action on a connected peripheral and simply enter the characteristic UUID and the value you want written and it executes no problem.

By my understanding, in order to do this I have to make a call to

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

with a CBCharacteristic object configured to have the correct UUID.

I have tried making a CBMutableCharacteristic with the correct UUID, and even correct permissions for what I know is an existing characteristic in this peripheral's profile, and I get the following crash, when attempting to perform any read/write operation with that characteristic on the peripheral:

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

And here is the code I use to set up and write to the characteristic:

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];
like image 670
Dan F Avatar asked Aug 06 '13 19:08

Dan F


1 Answers

While a characteristic like that may present inside the peripheral, that's not the actual characteristic it knows it contains. You need to discover all your services and characteristics and then write directly from those services and chars.

//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
               }
           }
       }
 }
like image 54
Tommy Devoy Avatar answered Oct 21 '22 03:10

Tommy Devoy