Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Created Oct 4, 2022
Embed
What would you like to do?
エアロバイクをスキャン、コネクト、Notifyするサンプル
import SwiftUI
import CoreBluetooth
struct ContentView: View {
let centralManager = CBCentralManager()
let centralManagerDelegate = CentralManagerDelegate()
var body: some View {
VStack {
Button("Scan") {
if centralManager.state == .poweredOn {
centralManager.scanForPeripherals(withServices: nil, options: nil)
}else{
print("Bad State!")
}
}
}
.padding().onAppear{
centralManager.delegate = centralManagerDelegate
centralManagerDelegate.onScanCallback = { (peripheral: CBPeripheral) -> Void in
centralManager.stopScan()
centralManager.connect(peripheral)
}
}
}
}
class CentralManagerDelegate: NSObject, CBCentralManagerDelegate {
let peripheralDelegate = PeripheralDelegate()
var onScanCallback: ((_ peripheral: CBPeripheral) -> Void)?
var hitPeripheral: CBPeripheral?
var hitNotifyCharacteristic: CBCharacteristic?
func centralManagerDidUpdateState(_ central: CBCentralManager) {
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if peripheral.name == "TY" || peripheral.identifier.uuidString == "0BCB1Bc8-914E-97F3-546A-301DA2F23F2A" {
if hitPeripheral == nil {
hitPeripheral = peripheral
hitPeripheral?.delegate = peripheralDelegate
central.stopScan()
central.connect(hitPeripheral!)
}
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
if peripheral.state != .connected {
return
}
// ServiceからCharacteristicを読み込む
peripheralDelegate.onLoadServiceCallback = { (cbService: CBService) -> Void in
self.hitPeripheral?.discoverCharacteristics(nil, for: cbService)
}
// Notify CharacteristicがあればNotifyを有効にする
peripheralDelegate.onLoadNotifyCharacteristicCallback = { (characteristic: CBCharacteristic) -> Void in
self.hitNotifyCharacteristic = characteristic
self.hitPeripheral?.setNotifyValue(true, for: self.hitNotifyCharacteristic!)
}
peripheral.discoverServices(nil)
}
}
class PeripheralDelegate: NSObject, CBPeripheralDelegate {
var onLoadServiceCallback: ((_ cbService: CBService) -> Void)?
var onLoadNotifyCharacteristicCallback: ((_ characteristic: CBCharacteristic) -> Void)?
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let onLoadNotifyCharacteristicCallback = onLoadNotifyCharacteristicCallback else{
return
}
service.characteristics?.forEach({ characteristic in
if characteristic.properties == .notify {
onLoadNotifyCharacteristicCallback(characteristic)
}
})
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
print(error)
return
}
if let services = peripheral.services {
guard let service = services.first else {
print("@empty Service")
return
}
if let onLoadServiceCallback = onLoadServiceCallback {
onLoadServiceCallback(service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let data = characteristic.value else {
return
}
print(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment