CoreBluetooth - writeValue。 Receiver无法从我的iPhone接收任何数据

时间:2015-05-16 02:24:18

标签: ios core-bluetooth

这是一个使用CoreBluetooth的项目。我用我的iPhone 5c来运行它。外围设备是单片机。 iPhone可以从单片机接收数据。但单片机无法通过[_peripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];

从iPhone接收数据

以下是我的代码的一部分。

#define kServiceUUID @"FFE0"
#define kCharacteristicUUID @"FFE1"
#import "BTManager.h"

@interface BTManager()<CBCentralManagerDelegate, CBPeripheralDelegate>{
CBCentralManager *manager;
}

@property (strong,nonatomic) NSMutableArray *peripherals;
@property (nonatomic, strong) CBCharacteristic *writeCharacteristic;
@property (nonatomic, strong) CBPeripheral *peripheral;
@end

@implementation BTManager

#pragma mark - Public Methods

- (void)writeToPeripheral:(NSString *)dataString {
    if(_writeCharacteristic == nil){
        NSLog(@"writeCharacteristic is nil");
        return;
    }
    NSData *value = [self dataWithHexstring:dataString];
    NSLog(@"hex data:%@",value);
    [_peripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
    NSLog(@"didWriteTo%@ characteristic:%@ data:%@",_peripheral.name,_writeCharacteristic.description,dataString);

}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    if (error) {
        NSLog(@"discoverCharacteristic:%@ Error:%@", service.UUID, [error localizedDescription]);
        return;
    }
    NSLog(@"Service:%@",service.UUID);
    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
            NSLog(@"setNotifyValuefor:%@",characteristic);
            _writeCharacteristic = characteristic;
            [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我没有看到您提供的源代码的任何直接问题。你试图写什么特定的外围设备?

我猜我会遇到的问题是

  1. 你确定该特性支持writeWithResponse吗?
  2. 您是否在peripheral: didWriteValueForCharacteristic: error:收到了任何类型的回复?
  3. 如果您在上述代理回调中没有看到响应,我会质疑外设是否正确响应写命令。