Skip to content

Instantly share code, notes, and snippets.

@UniversalSuperBox
Created August 30, 2019 23:12
Show Gist options
  • Save UniversalSuperBox/892dcd8d3804d01048a027fe6a0b916f to your computer and use it in GitHub Desktop.
Save UniversalSuperBox/892dcd8d3804d01048a027fe6a0b916f to your computer and use it in GitHub Desktop.
The minimal stuff you need to search for BLE devices in macOS
//Copyright 2019 Dalton Durst
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
import Cocoa
import CoreBluetooth
class BLEFinder: NSObject, CBCentralManagerDelegate {
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func begin() {
centralManager.scanForPeripherals(withServices: nil, options: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print("state: \(central.state.rawValue)")
begin()
}
func connectTo(peripheral: CBPeripheral) {
self.peripheral = peripheral
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Found peripheral! \(peripheral)")
connectTo(peripheral: peripheral)
}
var centralManager: CBCentralManager!
var peripheral: CBPeripheral?
}
var stuff = BLEFinder()
@UniversalSuperBox
Copy link
Author

You may be more interested in https://gist.github.com/UniversalSuperBox/4240dca31984afda7028a7c3d522cde5, which expands on this file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment