0
votes

Hi I am trying to create a write characteristic for my app

My characteristic creation code is this

 // Start with the Read CBMutableCharacteristic
self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]
                                                                  properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyRead                                                                       value:nil
                                                                 permissions:CBAttributePermissionsReadable];

self.writeCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:WRITE_CHARACTERISTIC_UUID]
                                                                 properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyWriteWithoutResponse                                                                       value:nil
                                                                permissions:CBAttributePermissionsWriteable];
// Then the service
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]
                                                                    primary:YES];

// Add the characteristic to the service
transferService.characteristics = @[self.transferCharacteristic, self.writeCharacteristic];

// And add it to the peripheral manager
[self.peripheralManager addService:transferService];

I have verified that two characteristics are present when adding the service. Although when I scan for this peripheral it only shows one characteristic. Scanning code:

[self.centralManager scanForPeripheralsWithServices:nil
                                            options:nil];

I checked various links and saw that my creation of write characteristic is correct, can anyone point out what am I doing wrong here and why is my service only showing one characteristic?

Note: This is an iPhone to iPhone app. Lightblue app shows two characteristics with their accurate properties

EDIT:

Characteristics fetch:

[peripheral discoverCharacteristics:nil forService:service];
1
Can you show the code where you fetch the characteristics, not the service? - Paulw11
I have added the code in the question @Paulw11 - Amogh Shettigar
And do you get any call to the didDiscoverCharacteristics peripheral delegate method? What does the characteristics property of the service contain? - Paulw11
I do get a call to didDiscoverCharacteristics, it contains only my transferCharacteristic and not my writeCharacteristic - Amogh Shettigar
If LightBlue can see both characteristics then there is something wrong with your code, since it uses CoreBluetooth too. Show more code. - Paulw11

1 Answers

0
votes

I can give you a code in swift for making your work done you have to convert it to objective c.

Before we can write data to our external peripheral, we need to know how we want to write that data. There are two types of CBCharacteristic write types that we need to be aware of. The CBCharacteristic write type can be either .withResponse or .withoutResponse. The .withResponse property type gets a response from the peripheral to indicate whether the write was successful. The .withoutResponse doesn't send any response back from the peripheral. To write to a characteristic we’ll need to write a value with an instance NSData and we’ll do that by calling writeValue(for: , type: CBCharacteristicWriteType.withResponse) method:

blePeripheral.writeValue(data!, for: txCharacteristic, type: CBCharacteristicWriteType.withResponse)

Now you’ll be able to write to a characteristic with a writeable attribute.

 func writeValue(data: String){
  let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue)
    if let blePeripheral = blePeripheral{
       if let txCharacteristic = txCharacteristic {
          blePeripheral.writeValue(valueString!, for: txCharacteristic, type: CBCharacteristicWriteType.withResponse)
        }
    }
}

In the writeValue(data) function above, we format the outgoing string as NSData and then check to see if the BLE peripheral and txCharacteristic variables are set. If they are, we then call the writeValue() function as explained previously. Because we wrote the value with a type of CBCharacteristicWriteType.withResponse, we can be notified of the response by implementing the following function in the delegate:

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
    guard error == nil else {
        print("Error discovering services: error")
      return
    }
     print("Message sent")
}