Skip to content

Commit

Permalink
day 242
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Feb 16, 2022
1 parent f337b86 commit d0beb17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 69 deletions.
4 changes: 0 additions & 4 deletions bluetooth-poc.xcodeproj/project.pbxproj
Expand Up @@ -11,7 +11,6 @@
E379386127BA804E0004A909 /* PeripheralListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E379386027BA804E0004A909 /* PeripheralListView.swift */; };
E379386327BA80500004A909 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E379386227BA80500004A909 /* Assets.xcassets */; };
E379386627BA80500004A909 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E379386527BA80500004A909 /* Preview Assets.xcassets */; };
E3794FE327BBE1E10004A909 /* PeripheralDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3794FE227BBE1E10004A909 /* PeripheralDetailView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -20,7 +19,6 @@
E379386027BA804E0004A909 /* PeripheralListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeripheralListView.swift; sourceTree = "<group>"; };
E379386227BA80500004A909 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
E379386527BA80500004A909 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
E3794FE227BBE1E10004A909 /* PeripheralDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeripheralDetailView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -55,7 +53,6 @@
children = (
E379385E27BA804E0004A909 /* bluetooth_pocApp.swift */,
E379386027BA804E0004A909 /* PeripheralListView.swift */,
E3794FE227BBE1E10004A909 /* PeripheralDetailView.swift */,
E379386227BA80500004A909 /* Assets.xcassets */,
E379386427BA80500004A909 /* Preview Content */,
);
Expand Down Expand Up @@ -141,7 +138,6 @@
buildActionMask = 2147483647;
files = (
E379386127BA804E0004A909 /* PeripheralListView.swift in Sources */,
E3794FE327BBE1E10004A909 /* PeripheralDetailView.swift in Sources */,
E379385F27BA804E0004A909 /* bluetooth_pocApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
37 changes: 0 additions & 37 deletions bluetooth-poc/PeripheralDetailView.swift

This file was deleted.

53 changes: 25 additions & 28 deletions bluetooth-poc/PeripheralListView.swift
Expand Up @@ -14,9 +14,9 @@ struct PeripheralListView: View {
var body: some View {
Group {
if (state.isBluetoothEnabled) {
List(state.discoveredPeripherals.sorted(by: { $0.key.uuidString > $1.key.uuidString }), id: \.key) { peripheral in
Button(peripheral.value.name ?? "?") {
state.connect(peripheral.value)
List(state.discoveredPeripherals.sorted(by: { $0.identifier.uuidString > $1.identifier.uuidString }), id: \.identifier) { peripheral in
Button(peripheral.name ?? "unknown") {
state.connect(peripheral)
}
}
Button("Tap to \(state.isScanning ? "stop" : "start") scanning") {
Expand All @@ -31,20 +31,6 @@ struct PeripheralListView: View {
Text("State: \(state.centralManagerState.rawValue)")
}
}
.sheet(item: $state.connectedPeripheral, onDismiss: nil) { peripheral in
if (state.isServicesDiscovered) {
let descriptorValues = peripheral.services?.flatMap { service in
service.characteristics?.flatMap { characteristic in
characteristic.descriptors?.map { descriptor in
descriptor.value as? String ?? "Not a description"
}
}
} ?? []
// ForEach(descriptorValues, id: \.self) { value in
// Text("hoge")
// }
}
}
}
}

Expand All @@ -55,18 +41,17 @@ struct ContentView_Previews: PreviewProvider {
}

final class PeripheralListViewState: NSObject, ObservableObject {
@Published var centralManagerState = CBManagerState.unknown {
@Published private(set) var centralManagerState = CBManagerState.unknown {
didSet {
isBluetoothEnabled = centralManagerState == .poweredOn
}
}
@Published var isBluetoothEnabled = false
@Published var discoveredPeripherals = [UUID:CBPeripheral]()
@Published var isScanning = false
@Published var connectedPeripheral = CBPeripheral?.none
@Published var isServicesDiscovered = false
@Published private(set) var isBluetoothEnabled = false
@Published private(set) var isScanning = false
@Published private(set) var discoveredPeripherals = Set<CBPeripheral>()

private let centralManager: CBCentralManager
private var connectedPeripheral = CBPeripheral?.none

override init() {
centralManager = CBCentralManager()
Expand All @@ -87,6 +72,10 @@ final class PeripheralListViewState: NSObject, ObservableObject {
}

func connect(_ peripheral: CBPeripheral) {
if let connected = connectedPeripheral {
centralManager.cancelPeripheralConnection(connected)
}

centralManager.connect(peripheral, options: nil)
}
}
Expand All @@ -97,23 +86,31 @@ extension PeripheralListViewState: CBCentralManagerDelegate {
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
discoveredPeripherals[peripheral.identifier] = peripheral
discoveredPeripherals.insert(peripheral)
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
connectedPeripheral = peripheral
peripheral.delegate = self
peripheral.discoverServices([.init(string: "180A")])
}
}

extension PeripheralListViewState: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
peripheral.services?.forEach { peripheral.discoverCharacteristics(nil, for: $0) }
peripheral.services?.forEach { peripheral.discoverCharacteristics([.init(string: "2A24")], for: $0) }
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
// servi
guard let characteristic = service.characteristics?.first(where: { $0.uuid.uuidString == "2A24" }),
characteristic.properties.contains(.read) else { return }

peripheral.readValue(for: characteristic)
}
}

extension CBPeripheral: Identifiable {}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let data = characteristic.value else { return }

print(String(decoding: data, as: UTF8.self))
}
}

0 comments on commit d0beb17

Please sign in to comment.