I am building an app in SwiftUI that communicates over BLE with a custom device that has an LED ring light. I can't figure out how to code the write functions correctly using data/variables from other views.
On ringLight.swift
I have a color wheel with rgbColour
variable which consists of three floats for the rgb values. I also have a toggle that when tapped should do two things (turn the light on):
- Write the rbgColour variable (3 floats) to the
RGBCharacteristicUUID
- Send the integer 1 to the
ConfigCharacteristicUUID
to turn the light on.
I am attempting to keep the UI views containing the characteristic's data separate from the BLEManager. Any insight on how to properly structure this/ how to do the write function? Thanks!!
ringLight.swift
struct ringLight: View {
@State var rgbColour = RGB(r: 0, g: 1, b: 1)
@State var brightness: CGFloat = 1
@State var turnHubLEDON = false
@ObservedObject var bleManager = BLEManager()
var body: some View {
VStack {
/// The text at the top.
HStack {
Text("Choose your hub color")
.font(.largeTitle)
.fontWeight(.light)
.padding(.bottom, 50)
}
/// The actual colour wheel.
ColourWheel(radius: 300, rgbColour: $rgbColour, brightness: $brightness)
.padding(.bottom, 30)
/// The slider shows the selected colour and allows control of the brightness/value. Cannot have value at 0 otherwise we lose the RGB value.
CustomSlider(rgbColour: $rgbColour, value: $brightness, range: 0.001...1)
.padding()
Text("R: (CGFloat(rgbColour.r)) G: (CGFloat(rgbColour.g)) B: (CGFloat(rgbColour.b))")
VStack{
Toggle("Light", isOn: $turnHubLEDON).labelsHidden().padding()
if turnHubLEDON {
// **Function to send the rgbColour variable & Config Integer here**
}
}
} // End VStack
}
}
BLEManager.swift (relevant parts)
class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {
var myCentral: CBCentralManager!
var myPeripheral: CBPeripheral!
//LED Light
var ConfigChar: CBCharacteristic?
var RGBChar: CBCharacteristic?
var BrightnessChar: CBCharacteristic?
@Published var isSwitchedOn = false
@Published var hubPeripheral = [Peripheral]()
@Published var hubIsConnected = false
override init() {
super.init()
myCentral = CBCentralManager(delegate: self, queue: nil)
myCentral.delegate = self
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
//LED Light
if characteristic.uuid == HubPeripheral.ConfigCharacteristicUUID {
print("LED Configuration Characteristic Found")
ConfigChar = characteristic
} else if characteristic.uuid == HubPeripheral.RGBCharacteristicUUID {
print("LED RGB Characteristic Found")
RGBChar = characteristic
} else if characteristic.uuid == HubPeripheral.BrightnessCharacteristicUUID {
print("LED Brightness Characteristic Found")
BrightnessChar = characteristic
}
} // End Characteristics
} // End If statement
} // End DidDiscoverCharacteristics
// **How do I structure these write functions to include data from ringLight.swift?**
func writeConfigurationValue() {
var parameter = NSInteger(1)
let data = NSData(bytes: ¶meter, length: 1)
myPeripheral.writeValue(data as Data, for: ConfigChar!, type: .withoutResponse)
}
} // End Delegate
Characteristics
class HubPeripheral: NSObject {
/// MARK: - Hub LED services and characteristics
public static let RingLightServiceUUID = CBUUID.init(string: "020013ac-4202-93ae-eb11-7d58e021f8f1")
public static let RGBCharacteristicUUID = CBUUID.init(string: "fdefb439-68de-1a88-5345-5cc9f9ff5605")
public static let BrightnessCharacteristicUUID = CBUUID.init(string: "4960d639-1407-9cbb-6846-5360eb5e6346")
public static let ConfigCharacteristicUUID = CBUUID.init(string: "cc0a6373-dc51-c1af-0140-b0322225ed9b")
}
question from:
https://stackoverflow.com/questions/65874309/swiftui-function-for-writing-characteristic-to-ble-peripheral-using-data-from-d 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…