gpt4 book ai didi

iOS 核心蓝牙 :Sending data from central and getting response from peripheral

转载 作者:行者123 更新时间:2023-11-28 19:55:57 25 4
gpt4 key购买 nike

我正在开发一个蓝牙应用程序。我已连接到蓝牙设备并在其中找到了服务和特征。我在从中央发送写请求和从外围设备接收响应时遇到问题。我已经编写了如下代码来写入数据。我如何知道外围设备收到了该数据?我可以在外围设备中显示任何警报吗?而且,以同样的方式,我想从外设接收数据,以便中央应该显示一个警报,表明它已收到外设的响应。

下面是我写的代码

  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:  
(CBService *)service error:(NSError *)error {
if (!error) {
printf("Characteristics of service with UUID : %s found\r\n",[self
CBUUIDToString:service.UUID]);
for(int i=0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
printf("Found characteristic %s\r\n",[ self CBUUIDToString:c.UUID]);
CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
if([self compareCBUUID:service.UUID UUID2:s.UUID])
{
printf("Finished discovering characteristics");

break;
}
}

}
else {
printf("Characteristic discorvery unsuccessfull !\r\n");
}

}

用于写入值

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:  
(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *payloadMessage = @"Hello";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:payload forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];

}

我需要获取 ble 设备的版本,即 GET_VERSION = 0x4a;请帮助我解决这个问题,或者提供从这两种设备写入和读取数据的任何示例。提前致谢

最佳答案

Bluetooth central 可以通过两种方式从外围设备接收数据 -

  1. 它可以发出对特征的显式读取请求
  2. 它可以订阅有关特征的通知 - 在这种情况下,外围设备将在值发生变化或这些是新数据时通知中央设备。这样效率更高,因为它避免了中央必须不断地轮询外围设备以获取可能未更改的数据。

无论您使用哪种方法,数据都将通过调用您的 didUpdateValueForCharacteristic: 委托(delegate)方法来传送。

您似乎对这个方法的用途有点困惑,因为您的代码在那里发出写请求,这可能不是您想要的。

在写入数据时,您可以写入有响应或无响应的数据。您的代码指定了响应。在这种情况下,当外围设备确认写入时,您的 didWriteValueForCharacteristic 委托(delegate)方法将被调用 - 这就是您知道数据已收到的方式。

实现您在问题中描述的行为的一些简单代码是:

 -(void)sendData:(NSData *)data {

[self.connectedPeripheral writeValue:data forCharacteristic:self.someCharacteristic type:CBCharacteristicWriteWithResponse];

}


- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrote characteristic" message:@"Successfully wrote characteristic" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
[alert show];
}
else {
NSLog(@"Error writing characteristic %@",error);
}
}

-(void)readCharacteristic {
[self.connectedPeripheral readCharacteristic:self.someOtherCharacteristic];
}




- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Read characteristic" message:[NSString stringWithFormat:@"Successfully read characteristic %@",characteristic.value] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
[alert show];
}
else {
NSLog(@"Error reading characteristic %@",error);
}

}

关于iOS 核心蓝牙 :Sending data from central and getting response from peripheral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26397381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com