where is example of iOS Bluetooth LE peripheralManager didReceiveWriteRequests

后端 未结 1 1706
离开以前
离开以前 2021-02-15 18:02

I need an example implementation of function didReceiveWriteRequests which runs at a Bluetooth iOS peripheral when data is written to it by the central iOS device doing a \'writ

1条回答
  •  再見小時候
    2021-02-15 18:40

    I got it working. Here is my working code:

    // 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 !!");
        }
    }
    

    0 讨论(0)
提交回复
热议问题