Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created March 9, 2021 09:14
Show Gist options
  • Save kyungpyoda/7ea31b9177fc5ad3c9b3020473fa62b7 to your computer and use it in GitHub Desktop.
Save kyungpyoda/7ea31b9177fc5ad3c9b3020473fa62b7 to your computer and use it in GitHub Desktop.
temp
//연결 재시도? 하면서 오류나는 부분입니다.
/*
* We will first check if we are already connected to our counterpart
* Otherwise, scan for peripherals - specifically for our service's 128bit CBUUID
*/
private func retrievePeripheral() {
print("[hoonki] test=\(TransferService.serviceUUID)")
//cleanup()
let connectedPeripherals: [CBPeripheral] = (centralManager.retrieveConnectedPeripherals(withServices: [TransferService.serviceUUID]))
os_log("Found connected Peripherals with transfer service: %@", connectedPeripherals)
if let connectedPeripheral = connectedPeripherals.last {
os_log("Connecting to peripheral %@", connectedPeripheral)
self.discoveredPeripheral = connectedPeripheral
centralManager.connect(connectedPeripheral, options: nil)
} else {
// We were not connected to our counterpart, so start scanning
centralManager.scanForPeripherals(withServices: [TransferService.serviceUUID],
options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
}
}
// 전송 받은 데이터를 전부 받았는지 확인하고 Label로 보여주기 위한 부분입니다.
/*
* This callback lets us know more data has arrived via notification on the characteristic
*/
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
// Deal with errors (if any)
if let error = error {
os_log("Error discovering characteristics: %s", error.localizedDescription)
cleanup()
return
}
guard let characteristicData = characteristic.value,
let stringFromData = String(data: characteristicData, encoding: .utf8) else { return }
os_log("Received %d bytes: %s", characteristicData.count, stringFromData)
// Have we received the end-of-message token?
if stringFromData == "EOM" {
// End-of-message case: show the data.
// Dispatch the text view update to the main queue for updating the UI, because
// we don't know which thread this method will be called back on.
DispatchQueue.main.asyncAfter(deadline: .now()) { //[weak self] in
//guard let self = self else { return }
// self.textView.text = String(data: self.data, encoding: .utf8)
print("TEST", self.data)
print(self.label.text)
print("wtf", String(data: self.data, encoding: .utf8))
// 계속 오류나는 부분
self.label.text = "wtf"
}
// Write test data
writeData()
} else {
// Otherwise, just append the data to what we have previously received.
data.append(characteristicData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment