Last active
February 24, 2023 12:35
-
-
Save tempire/ec5e586700ee6602f1a76bc63f02ff13 to your computer and use it in GitHub Desktop.
bluetooth LE advertise/scan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Check.swift | |
// Points | |
// | |
// Created by Glen Hinkle on 10/2/16. | |
// Copyright © 2016 Zombie Dolphin. All rights reserved. | |
// | |
import Foundation | |
import CoreBluetooth | |
class Detect: NSObject { | |
var deviceId = UIDevice.current.identifierForVendor!.uuidString | |
let serviceId = CBUUID(string: "28B282ED-E609-4582-9224-9E0E93174079") | |
let charId = CBUUID(string: "B1428E10-9894-4D5E-BB39-9136E124CA10") | |
var peripheral: CBPeripheral? | |
var centralManager: CBCentralManager! | |
var peripheralManager: CBPeripheralManager! | |
override init() { | |
super.init() | |
centralManager = CBCentralManager(delegate: self, queue: .none, options: .none) | |
//peripheralManager = CBPeripheralManager(delegate: self, queue: .none, options: .none) | |
} | |
func advertise() { | |
let char = CBMutableCharacteristic(type: charId, properties: [.read], value: "G".data(using: .utf8), permissions: [.readable]) | |
/* | |
let descId = CBUUID(string: CBUUIDCharacteristicUserDescriptionString) | |
let descriptor = CBMutableDescriptor(type: descId, value: "MYDESCRIPTOROMG") | |
char.descriptors = [descriptor] | |
*/ | |
let service = CBMutableService(type: serviceId, primary: true) | |
service.characteristics = [char] | |
peripheralManager.add(service) | |
print("advertising service: \(service.uuid), char value: \(char.value?.utf8string)") | |
peripheralManager.startAdvertising([ | |
CBAdvertisementDataServiceUUIDsKey: [serviceId] | |
//CBAdvertisementDataLocalNameKey: "whatevs" | |
]) | |
} | |
func scan() { | |
centralManager.scanForPeripherals(withServices: [serviceId], options: .none) | |
} | |
} | |
extension Detect: CBPeripheralManagerDelegate { | |
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) { | |
if let error = error { | |
print("Advertising ERROR: \(error.localizedDescription)") | |
} | |
} | |
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { | |
switch peripheral.state { | |
case .poweredOn: | |
advertise() | |
case .poweredOff, .unknown, .unsupported, .resetting, .unauthorized: | |
break | |
} | |
} | |
} | |
extension Detect: CBCentralManagerDelegate { | |
// Successfully connected a BLE peripheral | |
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { | |
//print(#function) | |
peripheral.discoverServices(.none) | |
} | |
// BLE peripheral discovered | |
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
if peripheral.name == "Glen's iPhone 7" { | |
centralManager.stopScan() | |
peripheral.delegate = self | |
self.peripheral = peripheral | |
centralManager.connect(peripheral, options: .none) | |
} | |
if let name = advertisementData[CBAdvertisementDataLocalNameKey] { | |
print("\(peripheral) \(name)") | |
} | |
} | |
// Whenever device state changes | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
switch central.state { | |
case .poweredOn: | |
scan() | |
case .poweredOff, .unknown, .unsupported, .resetting, .unauthorized: | |
break | |
} | |
} | |
} | |
extension Detect: CBPeripheralDelegate { | |
// Peripherals services discovered | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | |
peripheral.services?.filter { $0.uuid == serviceId }.forEach { service in | |
peripheral.discoverCharacteristics(.none, for: service) | |
} | |
} | |
// Peripheral characteristics discovered | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { | |
service.characteristics?.forEach { char in | |
print("CHAR id: \(char.uuid.uuidString) VALUE: \(char.value?.utf8string)") | |
peripheral.readValue(for: char) | |
//peripheral.discoverDescriptors(for: char) | |
} | |
} | |
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { | |
print(characteristic.value?.utf8string) | |
} | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) { | |
characteristic.descriptors?.forEach { desc in | |
print("SERVICE \(desc.characteristic.service.uuid.uuidString), DESC: \(desc)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment