Search code examples
iosobjective-cbluetoothcbcentralmanagercbperipheral

Central writing characteristic to Peripheral (iOS Core Bluetooth)


Okay, so I've looked through a thousand tutorials and other Stack Overflow threads (so please don't list as duplicate without answering the question) and I cannot work out how to use this functionality.

I have followed this tutorial: http://code.tutsplus.com/tutorials/ios-7-sdk-core-bluetooth-practical-lesson--mobile-20741

I have a system where a central can connect to a peripheral and read a characteristic from it.

I am now trying to get my central to rewrite the data within the characteristic but am finding the write line that I have called just being ignored.

I have declared my characteristic within my peripheral class as such:

self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyWriteWithoutResponse|CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];

And in my centralManager I have called

 [peripheral writeValue:[@"rewritten!" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];

But the line is ignored. Can anyone talk me through what might be wrong? Do I need to add a method to my peripheral class?

Also, I've tried doing it WithResponse but it still doesn't even call that method from the peripheral either.


Solution

  • Are you still stuck with the problem?

    I guess your problem is persisting due to the fact that you have not implemented didReceiveWriteRequests method.

    // Processes write command received from a central.
    
    - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
    {
    
        CBATTRequest       *request = [requests  objectAtIndex:0];
        NSData             *request_data = request.value;
        CBCharacteristic   *write_char = request.characteristic;
        //CBCentral*            write_central = request.central;
        //NSUInteger            multi_message_offset = request.offset;
    
        // Face commands this PWR RX to advertise serno UUID?
        int total_write_requests = 0;
        if ([ write_char.UUID isEqual:[CBUUID UUIDWithString:YOUR_CHARACTERISTIC_UUID]] )
        {
    
    
            // Read desired new_state data from central:
            unsigned char *new_state = (unsigned char *)[request_data   bytes];
            my_new_state = new_state[0];
            #endif
            NSLog(@"- advertise serno UUID: %s", my_new_state ? "TRUE" : "FALSE" );
    
            // Select UUID that includes serno of PWR RX, for advertisements:
    
            ++total_write_requests;
        }
    
        if ( total_write_requests )
        {
            [peripheral respondToRequest:request    withResult:CBATTErrorSuccess];  // result = success
        }
        else
        {
            NSLog(@"_no_write_request_FAULT !!");
        }
    }
    

    Comment out the [peripheral respondToRequest:request withResult:CBATTErrorSuccess];, as in your case you are using CBCharacteristicWriteWithoutResponse.

    Code taken from: where is example of iOS Bluetooth LE peripheralManager didReceiveWriteRequests