我需要开发一个可以与外围设备通信的应用程序,并使用BlueGigaModul。基本上我发送命令并等待回答。我的问题是响应不正确。错误的字符随机出现。我每秒向我的设备发送一个命令。蓝牙模块获取正确的数据,但我在iPhone上的数据包含错误的字符?我得到的数据大于20个字节,但模块应该拆分它。
1
How could this be? Is it because i memcopy to early?
The method didUpdateValueForCharacteristic is called if the whole data arrived?
2
Is it better to writeValue or updateCharacteristic?
3
I only find one characteristic? Do i need one to send and one to receive?
Or do i need to unsubscribe after writeValue and resubscribe for receive data?
4
Does CoreBluetooth check if the data is corrupt??
这就是我写的方式:
NSString * test = @"\r";
NSData *data = [test dataUsingEncoding:NSASCIIStringEncoding];
if(hr_characteristic != nil){
[_discoveredPeripheral writeValue:data forCharacteristic: hr_characteristic type:CBCharacteristicWriteWithResponse];
我用以下方式检查回复:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if(error != nil)
return;
int len=characteristic.value.length;
if(len != 0)
{
memcpy(pbuffer,[characteristic.value bytes],len);
pbuffer += len;
}
}
Atm我用我收到的所有数据填充缓冲区。我发了10次。我收到后,将缓冲区与我预期的答案进行比较。
我发送了600个命令,其中包含34个错误字符。缓冲区填充5230字节。
由于