【问题标题】:BLE characteristic is nil when the service is not nil running in iPhone当服务在 iPhone 中运行时不是 nil 时,BLE 特性为 nil
【发布时间】:2013-04-25 10:48:20
【问题描述】:

在我的项目中,我使用与 iPhone 连接的 BLE 外围设备。当我超过最大连接距离时,通信断开,然后我回来,通信连接。但重新连接后,特性无法写入数据!似乎特征是零,但服务不是零。我必须再次运行我的应用程序,以便特征可以正常写入数据。有没有人有同样的情况,或者有什么想法?等待帮助。

    -(void) writeCharacteristicValue:(int )value forCharacteristic:(CBCharacteristic *)charateristic  type:(CBCharacteristicWriteType ) type
{
    NSLog(@"writeCharacteristicValue");

    NSData  *data   = nil;
    u8 val = value;

    if (nil == servicePeripheral)
    {
        NSLog(@"Not connected to a peripheral");
        return ;
    }

    if (nil == charateristic)
    {
        NSLog(@"No valid characteristic");
        return;
    }

    data = [NSData dataWithBytes:&val length:sizeof (val)];
    [servicePeripheral writeValue:data forCharacteristic: charateristic type:type];
    NSLog(@"dataWithBytes value %d",val);
}
CoreBluetooth[WARNING] <CBConcreteCharacteristic: 0x1ed5f1c0> is not a valid characteristic for peripheral <CBConcretePeripheral: 0x20002520 UUID = <CFUUID 0x20078a60> BE196610-C2A1-5D22-3E71-6851718F9672, Name = "Finder", IsConnected = YES>

它有时 NSLog "No valid characteristic" ,所以我认为特征是 nil 。

【问题讨论】:

  • 一个小代码看看为什么特征为空?
  • 我在上面添加了警告。我现在更改了我的 CBCharacteristicWriteType,它是 CBCharacteristicWriteWithoutResponse,但它没有帮助,我不知道我能做什么....@Larme
  • 在调用方法时发布代码,以及在使用/设置这些参数时...
  • 问题解决了!谢谢大家一样! @Larme

标签: ios core-bluetooth


【解决方案1】:

问题解决了! 我将连接的服务放在 NSMutableArray 中,当外围设备断开连接时,我无法从连接的服务数组中删除服务。因此,下次我从数组中获取服务时,它会返回已发布的错误服务。 现在,我更改了我的代码并及时删除了断开的服务。它运作良好!

【讨论】:

  • 你能把代码贴出来吗?我在将数据从中心写入外围时遇到问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多