Hello everyone,
Like so many others, I am a very newbie App developer. To my own detriment, I acknowledge I am trying to shortcut things a bit by using and modifying other pieces of code. Always easier to modify something that is already working, rather than staring at a blank screen not knowing how to begin.
So this leads me to this problem I am having. This is a Bluetooth application. And the Bluetooth runs just fine in both of my views. However, when the Bluetooth is linked and active, and I am receiving data, a Timer will not run. As soon as I stop the Bluetooth source (using an Arduino nano), the time begins to count again. There are no errors flagged in Xcode, just does not work properly. I was hoping someone could take a look and find the issue, and let me know how to fix it. Hopefully it is something easy, but without any errors being thrown, hard to know what to look for. Thanks...
So this is my Bluetooth Code:
import Foundation
import CoreBluetooth
enum ConnectionStatus: String {
case connected
case disconnected
case scanning
case connecting
case error
}
let BarrelBaristaService: CBUUID = CBUUID(string: "4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
//Temperature F
let TemperatureCharacteristic: CBUUID = CBUUID(string: "5D54D470-8B08-4368-9E8F-03191A0314A5")
//Humidity %
let HumidityCharacteristic: CBUUID = CBUUID(string: "B2F5E988-C50F-4200-A1D9-5884F9417DEF")
//Weight %
let WeightCharacteristic: CBUUID = CBUUID(string: "BEB5483E-36E1-4688-B7F5-EA07361B26A8")
class BluetoothService: NSObject, ObservableObject {
private var centralManager: CBCentralManager!
var BarrelBarristaPeripheral: CBPeripheral?
@Published var peripheralStatus: ConnectionStatus = .disconnected
@Published var TempValue: Float = 0
@Published var HumidValue: Float = 0
@Published var WeightValue: Float = 0
@Published var Connected: Bool = false
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func scanForPeripherals() {
peripheralStatus = .scanning
centralManager.scanForPeripherals(withServices: nil)
}
}
extension BluetoothService: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
print("CB Powered On")
scanForPeripherals()
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if peripheral.name == "Barrel Barista" {
print("Discovered \(peripheral.name ?? "no name")")
BarrelBarristaPeripheral = peripheral
centralManager.connect(BarrelBarristaPeripheral!)
peripheralStatus = .connecting
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheralStatus = .connected
Connected = true
peripheral.delegate = self
peripheral.discoverServices([BarrelBaristaService])
centralManager.stopScan()
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
peripheralStatus = .disconnected
Connected = false
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
peripheralStatus = .error
print(error?.localizedDescription ?? "no error")
}
}
extension BluetoothService: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services ?? [] {
if service.uuid == BarrelBaristaService {
print("found service for \(BarrelBaristaService)")
peripheral.discoverCharacteristics([TemperatureCharacteristic], for: service)
peripheral.discoverCharacteristics([HumidityCharacteristic], for: service)
peripheral.discoverCharacteristics([WeightCharacteristic], for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for characteristic in service.characteristics ?? [] {
peripheral.setNotifyValue(true, for: characteristic)
print("found characteristic, waiting on values.")
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic.uuid == TemperatureCharacteristic {
guard let data = characteristic.value else {
print("No data received for \(characteristic.uuid.uuidString)")
return
}
let TempData: Float = data.withUnsafeBytes { $0.pointee }
TempValue = TempData
}
if characteristic.uuid == HumidityCharacteristic {
guard let data = characteristic.value else {
print("No data received for \(characteristic.uuid.uuidString)")
return
}
let HumidData: Float = data.withUnsafeBytes { $0.pointee }
HumidValue = HumidData
}
if characteristic.uuid == WeightCharacteristic {
guard let data = characteristic.value else {
print("No data received for \(characteristic.uuid.uuidString)")
return
}
let WeightData: Float = data.withUnsafeBytes { $0.pointee }
WeightValue = WeightData
}
}
}
Post
Replies
Boosts
Views
Activity
Hello,
First time poster. Was wondering if someone might know the answer to this. So I wanted to use a fancy font in my iPhone app for a title. The following code works fine, but the font I really want is “Apple Chancery” but it will not show up on the iPhone, even though this font is default on all Mac books. Any ideas why that font will not work? Thanks…
Text("(whiskyname)")
.frame(width: 350, height:56)
.background(Color.blue.opacity(0.3)).border(Color.black, width: 3)
.cornerRadius(5)
.font(.custom("Snell Roundhand", fixedSize: 45))
.foregroundColor(.black)
.bold()