【问题标题】:iOS 8 Bluetooth LE Central can't write value for characteristic 2A06iOS 8 蓝牙 LE Central 无法写入特征 2A06 的值
【发布时间】:2015-01-26 06:30:58
【问题描述】:

我正在开发一个 iPhone 应用程序,需要在 iOS 7.0 和 8.0 上使用 CoreBluetooth 框架在支持Immediate Alert service 的蓝牙低功耗设备上写入值。与设备的连接工作正常,但每当我尝试在设备上写入值时,什么都没有发生(该值未保存在设备上。因此,设备没有响铃)。 下面是用于写入值的代码:

+ (void)writeValueForCharacteristic:(CBCharacteristic *)characteristic peripheral:(CBPeripheral *)peripheral alarm:(BOOL)alarm
{    
    NSData  * data = nil;
    if (alarm) {
        uint8_t value = 2;
        data = [NSData dataWithBytes:&value length:sizeof(value)];
    } else {
        uint8_t value = 0;
        data = [NSData dataWithBytes:&value length:sizeof(value)];
    }
    [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}

执行上述代码前设备的日志和状态:

- (void)beginAlarm
{
    NSLog(@"characteristic : %@",discAlertCharacteristic);
    NSLog(@"peripheral : %@",discPeripheral);
    NSLog(@"service : %@",service);

    [BluetoothUtility writeValueForCharacteristic:discAlertCharacteristic peripheral:discPeripheral alarm:YES];
}
特征:CBCharacteristic:0x146ea8d0,UUID = 2A06,属性 = 0x14,值 =(空),通知 = 否 服务:CBService:0x146ecc90,isPrimary = YES,UUID = 1802 外围设备:CBPeripheral:0x146dd110,标识符 = B0A195DC-7273-B3F5-BA70-0219A61F8904,名称 = LA-TAG a87,状态 = 已连接

我已经使用BLE Utility app 测试了我的蓝牙设备并且它工作正常。

【问题讨论】:

  • “什么都没有发生”是什么意思?值没有保存到外设?
  • 另外请显示打印您发布的日志的代码(使用哪些方法打印日志?)。
  • 谢谢米哈尔。我已经更新了问题。
  • 您的代码看起来不错。在调用beginAlarm 之前,您是否在peripheral 上调用discoverCharacteristics:forService:
  • 是的,我在 beginAlarm 之前调用了 discoverCharacteristics:forService 方法

标签: ios objective-c bluetooth bluetooth-lowenergy core-bluetooth


【解决方案1】:
if (getCharacteristic.properties.rawValue == 0xE){

              let sentValue:[UInt8] = [0x38, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x02]

              let sentData = Data(bytes: sentValue)

              self.peripheralDevice.writeValue(sentData, for: getCharacteristic, type: CBCharacteristicWriteType.withResponse)

 }

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多