gpt4 book ai didi

ios - 核心蓝牙 : Getting error even if data is written into writable characteristics

转载 作者:可可西里 更新时间:2023-11-01 03:37:49 26 4
gpt4 key购买 nike

我正在使用 CoreBlueTooth 框架写入 Peripheral 的可写特性之一。我在中央实现“didWriteValueForCharacteristic:错误:”委托(delegate),它总是在错误之下返回我。尽管我已经在我的外围设备上收到了数据。

Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo=0x166762e0 {NSLocalizedDescription=Unknown error.}

在我的代码中,我的 self.data 是一个具有 3 个键和值的 NSDictionary。

// Central

- (void)centralManagerDidUpdateState:(CBCentralManager *)iCentral {
if (iCentral.state != CBCentralManagerStatePoweredOn) {
return;
}

[self.centralManager scanForPeripheralsWithServices:self.peripheralServices options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)iPeripheral advertisementData:(NSDictionary *)iAdvertisementData RSSI:(NSNumber *)iRSSI {
if (self.discoveredPeripheral != iPeripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
self.discoveredPeripheral = iPeripheral;

// Connect to the discovered peripheral
[self.centralManager connectPeripheral:iPeripheral options:nil];
}
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)iPeripheral advertisementData:(NSDictionary *)iAdvertisementData RSSI:(NSNumber *)iRSSI {
if (self.discoveredPeripheral != iPeripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
self.discoveredPeripheral = iPeripheral;

// Connect to the discovered peripheral
[self.centralManager connectPeripheral:iPeripheral options:nil];
}
}


// We've connected to the peripheral, now we need to discover the services and characteristics to find the 'writeable' characteristic.
- (void)centralManager:(CBCentralManager *)iCentral didConnectPeripheral:(CBPeripheral *)iPeripheral {
// Stop scanning
[self.centralManager stopScan];

// Make sure we get the discovery callbacks
iPeripheral.delegate = self;

// Search only for services that match our UUID
[iPeripheral discoverServices:self.peripheralServices];
}


- (void)peripheral:(CBPeripheral *)iPeripheral didDiscoverServices:(NSError *)iError {
if (iError) {
[self cleanup];
return;
}

// Loop through the newly filled peripheral.services array, just in case there's more than one.
for (CBService *service in iPeripheral.services) {
[iPeripheral discoverCharacteristics:@[self.writeableCharactersticsUUID] forService:service];
}
}


// Write the data into peripheral's characterstics
- (void)peripheral:(CBPeripheral *)iPeripheral didDiscoverCharacteristicsForService:(CBService *)iService error:(NSError *)iError {
if (iError) {
[self cleanup];

return;
}

// Find out the writable characterstics
for (CBCharacteristic *characteristic in iService.characteristics) {
if ([characteristic.UUID isEqual:self.writeableCharactersticsUUID]) {
NSData *dataToWrite = [NSJSONSerialization dataWithJSONObject:self.data options:0 error:nil];
NSInteger dataSize = [[NSByteCountFormatter stringFromByteCount:dataToWrite.length countStyle:NSByteCountFormatterCountStyleFile] integerValue];
if (dataSize > 130) {
NSLog(@"Cannot send more than 130 bytes");
return;
}

[self.discoveredPeripheral writeValue:dataToWrite forCharacteristic:self.centralWriteableCharacteristic type:CBCharacteristicWriteWithResponse];

break;
}
}
}


- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError {
NSLog(@"Error = %@", iError);
}


- (void)cleanup {
// Don't do anything if we're not connected
if (self.discoveredPeripheral.state != CBPeripheralStateConnected) {
return;
}

// If we've got this far, we're connected, but we're not subscribed, so we just disconnect
[self.centralManager cancelPeripheralConnection:self.discoveredPeripheral];
}


// Peripheral

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)iPeripheral {
if (iPeripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}

CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:iCID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];

CBMutableService *writableService = [[CBMutableService alloc] initWithType:iServiceId primary:YES];
writableService.characteristics = @[characteristic];

//[self.peripheralManager removeAllServices];
[self.peripheralManager addService:writableService];
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[iServiceId]}];
}

- (void)peripheralManager:(CBPeripheralManager *)iPeripheral didReceiveWriteRequests:(NSArray *)iRequests {
CBATTRequest *aRequest = iRequests[0];
NSData *aData = aRequest.value;
NSDictionary *aResponse = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:nil];

NSLog(@"Received Data = %@", aResponse);
}

最佳答案

我想通了。问题在于特征类型。我使用了“CBCharacteristicWriteWithoutResponse”而不是“CBCharacteristicWriteWithResponse”并且它起作用了。

我是在读完这篇文章后这样做的:

writeValue forCharacteristic writeType,此函数是写入设备上的特征的主要函数。 writeType 属性设置为无响应写入或有响应写入。使用 write with response 时,在 iOS 设备等待接收 ok 响应和回调时,所有对外围设备的写入都会被缓存。当使用write no response时,数据不会被缓存。这在使用需要低延迟的东西时很重要,例如 RC 汽车或直升机等。当使用带响应的写入时,iOS 设备有时可能会滞后,这不会产生很好的响应……对于每次写入,都会调用 didWriteCharacteristic 回调。

关于ios - 核心蓝牙 : Getting error even if data is written into writable characteristics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846261/

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