本文介绍了外围设备的中央写入特性(iOS核心蓝牙)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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];

在我的centralManager中,我已经打电话给了

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?

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

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

推荐答案

你还遇到这个问题吗?

I猜你的问题是持久的,因为你没有实现 didReceiveWriteRequests 方法。

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

注释 [外围设备] respondToRequest:request withResult:CBATTErrorSuccess]; ,就像你使用CBCharacteristicWriteWithoutResponse一样。

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

代码取自: iOS蓝牙LE peripheralManager的示例在哪里确认接收写入请求

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

这篇关于外围设备的中央写入特性(iOS核心蓝牙)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多