目前我正在开发一个通过BLE(ios)上传文件的项目
目前我有一个循环来发送文件中的所有数据,一旦文件中的指针到达结束,我就关闭循环。
因此,在固件方面,我写入的特性将从0更新为100,作为固件接收到的字节数的进度指示。
我的didUpdateValueForCharacteristic
仅在我的循环结束后被调用,因为我相信它们是同步的并且都在主线程上工作。
我试图使我的循环异步,所以我仍然可以在发送时听取方法didUpdateValueForCharacteristic
dispatch_async(_sendFileQueue, ^{
while (_isFileSending) {
// File is still sending
// Upload file from offset
[self uploadFile:_offset];
}
});
所以我尝试了这个,但我仍然在循环结束时获得更新,但我不知道是不是因为我在iOS端没有得到响应的连接间隔。我知道固件是在发送文件时更新UUID。
uploadFile的来源
- (void)uploadFile:(int)from {
// Look at file from index from
[_filePath seekToFileOffset:from];
NSData *soundData = [_filePath readDataOfLength:_packetSize];
// data it should be greater than 0 bytes
if (soundData.length > 0) {
[self connectToPeripherals:soundData];
// Find new pointer
_offset += _packetSize;
}
}
- (void)connectToPeripherals:(NSData *)data {
// Using NSNumber since I want to check if I get a nil returned
NSNumber *writeWithResponse = nil;
CBPeripheral *firstPeripheral = self.peripherals[0];
CBCharacteristic *firstCharacteristics = self.characteristics[0];
// Check if properties have a response, if so write with response
if((self.characteristicManager.properties & CBCharacteristicPropertyWrite)
== CBCharacteristicPropertyWrite) {
DLog(@"Write with response");
writeWithResponse = [NSNumber numberWithBool:YES];
} else if ((self.characteristicManager.properties &
CBCharacteristicPropertyWriteWithoutResponse) ==
CBCharacteristicPropertyWriteWithoutResponse) {
DLog(@"Write without response");
writeWithResponse = [NSNumber numberWithBool:NO];
}
// There is no write property
if (!writeWithResponse) {
DLog(@"ERROR! Could not write to characteristics");
return;
}
// Write to the first peripheral
[firstPeripheral writeValue:data forCharacteristic:firstCharacteristics
type:[writeWithResponse boolValue] ?
CBCharacteristicWriteWithResponse : CBCharacteristicWriteWithResponse];
}
tl; dr 在所有内容中调用didUpdateValueForCharacteristic