在我的项目中,我使用与我的 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 “没有有效的特征”,所以我认为特征是 nil 。