1
votes

I am dealing with BluetoothLow Energy problem. I find my peripheral BLE device with iPhone, connect to it, also find service and the characteristic which is in write mode. But when I try to send some data, nothing happens - device doesn't receive anything and also - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error is not called.

My writeValue method is here:

-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {

    CBUUID *UUIDService = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_SERVICE_UUID]];
    CBUUID *UUIDCharacteristic = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_CHARACTERISTIC_UUID]];

    NSLog(@"ALERT");
    CBService *service = [self findServiceFromUUID:UUIDService p:p];
    if (!service)
        {
            NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@",
                  [self CBUUIDToString:UUIDService],
                  p.identifier.UUIDString);

            return;
        }

    CBCharacteristic *characteristic = [self findCharacteristicFromUUID:UUIDCharacteristic service:service];
    if (!characteristic)
    {
        NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
              [self CBUUIDToString: UUIDCharacteristic],
              [self CBUUIDToString:UUIDService],
              p.identifier.UUIDString);

        return;
    }

    NSString *message = @"AB";
    NSData *dataMsg= [message dataUsingEncoding:NSUTF8StringEncoding];

    [p writeValue:dataMsg forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

}

What could be the problem?

1
Is peripheral:didWriteValueForCharacteristic:error: called? What does findCharacteristicFromUUID:?Larme
Nope, peripheral:didWriteValueForCharacteristic:error: is NOT called either. findCharacteristicFromUUID: simply checks if UUID which I wrote is the same as one of the service's characteristics: ` -(CBCharacteristic ) findCharacteristicFromUUID:(CBUUID *)UUID service:(CBService)service { for(int i=0; i < service.characteristics.count; i++) { CBCharacteristic *c = [service.characteristics objectAtIndex:i]; if ([self compareCBUUID:c.UUID UUID2:UUID]) return c; } return nil; //Characteristic not found on this service } `JInn
What logs characteristic when you found it?Larme
<CBCharacteristic: 0x14d5f280, UUID = 00035B03-58E6-07DD-021A-08123A0003FF, properties = 0x8, value = (null), notifying = NO>JInn
Its property is "write". Tried it from other iOS app which communicated through this characteristic. My code just ignores that line and I don't get my written value in peripheral side.JInn

1 Answers

0
votes

Try to this. swift 3 This code for enable notify for CBCharacteristic(0x12 for notify property).

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    for newChar: CBCharacteristic in service.characteristics!{

        peripheral.readValue(for: newChar)

        if newChar.properties.rawValue == 0x12{

            peripheral.setNotifyValue(true, for: newChar)
        }
    }
}