使用CoreBluetooth编写操作后无法读取数据
Am Am New to Objective C Am试图从Arduino外围读取具有特定服务和特征的Arduino外围的数据,
两种不同的服务,
//arduino code
BLEService dataService("938548e6-c655-11ea-87d0-0242ac130003"); //Data reading
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); //LED control service
// Initialize dataService characteristics
BLECharacteristic dataArray("77539407-6493-4b89-985f-baaf4c0f8d86", BLERead | BLENotify, 114); // standard 16-bit characteristic UUID
// Initialize ledService characteristics
BLECharacteristic eventChar("19B10001-E8F2-537E-4F6C-D104768A1213", BLERead | BLEWrite, 1);
我能够通知 未从外围收到数据
for (CBCharacteristic *characteristic in service. characteristics) {
NSLog(@"Charecteristics %@", characteristic.UUID);
if ([characteristic.UUID.UUIDString containsString: @"77539407"]) {
NSLog(@"Specific Characteristics %@", characteristic.UUID);
//Working........ Characteristic
[peripheral setNotifyValue: YES forCharacteristic: characteristic];
}
if ([characteristic.UUID.UUIDString containsString: @"19B10001"]) {
NSLog(@"Specific Charecteristics %@", characteristic.UUID);
NSData *data = [@"01" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"WRITING DATA");
NSLog(@"%@ : dataaaaaa",data);
[peripheral writeValue: data forCharacteristic: characteristic
type: CBCharacteristicWriteWithResponse];
}
}
并获得了第一个特征的读取值,以便在尝试写作时的第一个特征第二个特征在编写数据后,
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"didWrite");
NSString *key = [self keyForPeripheral: peripheral andCharacteristic:characteristic];
NSLog(@" writeCallback : %@ ",key);
RCTResponseSenderBlock writeCallback = [writeCallbacks objectForKey:key];
NSLog(@" writeCallback : %@ ",writeCallback);
if (writeCallback) {
if (error) {
NSLog(@"%@", error);
[writeCallbacks removeObjectForKey:key];
writeCallback(@[error.localizedDescription]);
} else {
if ([writeQueue count] == 0) {
NSLog(@"NO 1");
[writeCallbacks removeObjectForKey:key];
writeCallback(@[]);
}else{
// Remove and write the queud message
NSLog(@"NO 2");
NSData *message = [writeQueue objectAtIndex:0];
[writeQueue removeObjectAtIndex:0];
[peripheral writeValue:message forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
}
}
}
,它将转到 didwritevalueforchacteristic ,我为WriteCallback null for WriteCallback而言,有人可以向我解释我在
Am new to Objective C Am trying to read data from an arduino peripheral with a specific service and characteristic
there are two characteristics with for two different services
//arduino code
BLEService dataService("938548e6-c655-11ea-87d0-0242ac130003"); //Data reading
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); //LED control service
// Initialize dataService characteristics
BLECharacteristic dataArray("77539407-6493-4b89-985f-baaf4c0f8d86", BLERead | BLENotify, 114); // standard 16-bit characteristic UUID
// Initialize ledService characteristics
BLECharacteristic eventChar("19B10001-E8F2-537E-4F6C-D104768A1213", BLERead | BLEWrite, 1);
I was able to notify and got the read value for the first characteristics for when am trying to write for second characteristic am not receiving data from peripheral
for (CBCharacteristic *characteristic in service. characteristics) {
NSLog(@"Charecteristics %@", characteristic.UUID);
if ([characteristic.UUID.UUIDString containsString: @"77539407"]) {
NSLog(@"Specific Characteristics %@", characteristic.UUID);
//Working........ Characteristic
[peripheral setNotifyValue: YES forCharacteristic: characteristic];
}
if ([characteristic.UUID.UUIDString containsString: @"19B10001"]) {
NSLog(@"Specific Charecteristics %@", characteristic.UUID);
NSData *data = [@"01" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"WRITING DATA");
NSLog(@"%@ : dataaaaaa",data);
[peripheral writeValue: data forCharacteristic: characteristic
type: CBCharacteristicWriteWithResponse];
}
}
after writing the data it goes to the didWriteValueForCharacteristic where I get null for the writeCallBack can someone explain me what mistake am doing
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"didWrite");
NSString *key = [self keyForPeripheral: peripheral andCharacteristic:characteristic];
NSLog(@" writeCallback : %@ ",key);
RCTResponseSenderBlock writeCallback = [writeCallbacks objectForKey:key];
NSLog(@" writeCallback : %@ ",writeCallback);
if (writeCallback) {
if (error) {
NSLog(@"%@", error);
[writeCallbacks removeObjectForKey:key];
writeCallback(@[error.localizedDescription]);
} else {
if ([writeQueue count] == 0) {
NSLog(@"NO 1");
[writeCallbacks removeObjectForKey:key];
writeCallback(@[]);
}else{
// Remove and write the queud message
NSLog(@"NO 2");
NSData *message = [writeQueue objectAtIndex:0];
[writeQueue removeObjectAtIndex:0];
[peripheral writeValue:message forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
}
}
}
Below image is the log
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论