外围设备的中央写入特性(iOS核心蓝牙)

时间:2014-11-28 16:50:47

标签: ios objective-c bluetooth cbcentralmanager cbperipheral

好的,所以我已经浏览了一千个教程和其他Stack Overflow线程(所以请不要在没有回答问题的情况下列出重复内容),我无法弄清楚如何使用这个功能。

我已经按照本教程: http://code.tutsplus.com/tutorials/ios-7-sdk-core-bluetooth-practical-lesson--mobile-20741

我有一个系统,中央可以连接到外围设备并从中读取特征。

我现在正试图让我的中心重写特征中的数据,但我发现我所称的写行只是被忽略了。

我已经在我的外围课程中声明了我的特点:

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

在我的centralManager中,我称之为

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

但这条线被忽略了。任何人都可以通过可能出错的方式与我交谈?我是否需要在外设类中添加方法?

另外,我已经尝试过使用响应,但它仍然无法从外设调用该方法。

1 个答案:

答案 0 :(得分:5)

你还在坚持这个问题吗?

我认为您的问题仍然存在,因为您尚未实施didReceiveWriteRequests方法。

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

注释掉[peripheral respondToRequest:request withResult:CBATTErrorSuccess];,就像你使用的是CBCharacteristicWriteWithoutResponse一样。

代码取自:where is example of iOS Bluetooth LE peripheralManager didReceiveWriteRequests