1

I’m currently trying to build a swift playground that can communicate with a HM-18 BLE module.

I’m new to swift. I just don’t get the delegate thing and what objects I need to assign there (???).

Let me break down one line and explain what parts I think I understand:

let managerDelegate: PlaygroundBluetoothCentralManagerDelegate = ???

Let declares a constant with the name managerDelegate which must be of type PlaygroundBluetoothCentralManagerDelegate

I just have no idea what comes after the =

This is my code so far:

import CoreBluetooth
import PlaygroundBluetooth
import PlaygroundSupport

// Create State enum (shape)
enum Shape: String, CaseIterable {
    case on, off
}

struct ContentView: View {
    
    // Get all cases
    let LEDStates = Shape.allCases
    
    // State Variable (state binding observables -> learn)
    @State private var selectedStateIndex = 0
    
    // I dont get this part from: https://developer.apple.com/documentation/playgroundbluetooth/connecting_to_bluetooth_peripherals_in_swift_playgrounds
    
    let managerDelegate: PlaygroundBluetoothCentralManagerDelegate = ???
    
    let manager = PlaygroundBluetoothCentralManager(services: nil)
    manager.delegate = managerDelegate
    
    
    let viewDelegate: PlaygroundBluetoothConnectionViewDelegate = ???
    let connectionView = PlaygroundBluetoothConnectionView(centralManager: manager, delegate: viewDelegate)
    connectionView.dataSource = viewDelegate
    
    // Place the connection view within the rest of your page's content.
    let page: UIViewController & PlaygroundLiveViewSafeAreaContainer = ???
    page.view.addSubview(connectionView)
    
    // Position the connection view in the top right corner.
    connectionView.topAnchor.constraint(equalTo: page.liveViewSafeAreaGuide.topAnchor, constant: 20).isActive = true
    connectionView.trailingAnchor.constraint(equalTo: page.liveViewSafeAreaGuide.trailingAnchor, constant: -20).isActive = true
    
    // This I understand 
    PlaygroundPage.current.liveView = page
    var body: some View {
        
        ZStack(alignment: .bottomTrailing) {
            
            Picker("LED State", selection: $selectedStateIndex) {
                ForEach(0..<LEDStates.count) { index in
                    Text(self.LEDStates[index].rawValue).tag(index)
                }
            }.pickerStyle(SegmentedPickerStyle())
            .padding(10)
            .background(Color.black.opacity(0.5))
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

larsinka
  • 21
  • 4
  • You need to declare a class that conforms to `PlaygroundBluetoothCentralMananagerDelegate` and assign an instance of that class. – Paulw11 Jun 29 '20 at 22:31
  • Where do I have to declare it? And what has to go inside the class? – larsinka Jun 30 '20 at 12:58
  • Create the class in your playground. The protocol is documented [here](https://developer.apple.com/documentation/playgroundbluetooth/playgroundbluetoothcentralmanagerdelegate). Your class needs to implement the functions that you need to handle the Bluetooth events. – Paulw11 Jun 30 '20 at 21:15
  • Okay I got this BluetoothSerial class from GitHub https://github.com/hoiberg/swiftBluetoothSerial and I think this is what you mean, right? – larsinka Jun 30 '20 at 22:34
  • You can work from that; It is built for CoreBluetooth; You would need to modify it to work with Playground bluetooth – Paulw11 Jun 30 '20 at 23:19
  • Is it okay to have it in a different module? I have my main code in the page module. What tutorial or keyword would you recommend to learn about this stuff in the first place? – larsinka Jun 30 '20 at 23:40
  • You can put it in a different module.I have never used Bluetooth in a playground, but it is very similar to Core Bluetooth, so searching for that should get you started – Paulw11 Jul 01 '20 at 01:42
  • Okay if I declare a managerDelegate to be a CBCentralManagerDelegate, do I need to give it a value? Like this: – larsinka Jul 02 '20 at 11:07
  • let managerDelegate: CBCentralManagerDelegate = something – larsinka Jul 02 '20 at 11:08
  • You won’t use `CBCentralManagerDelegate` because you are in a playground so you need to use the playground equivalent, so `managerDelegate = YourDelegateClass()` – Paulw11 Jul 02 '20 at 11:10
  • This drives me literal crazy I’m stuck here since 3 days and I just can’t find any tutorials. I’ve watched 20 hours swift tutorials so I know how to declare a variable or how to use control flow FOR DAYS. But I just don’t get this. WTF IM SO MAD RN – larsinka Jul 02 '20 at 11:11
  • Thank you so much for your help though I feel like such a fool – larsinka Jul 02 '20 at 11:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217079/discussion-between-larsinka-and-paulw11). – larsinka Jul 02 '20 at 11:13

0 Answers0