Skip to content

Instantly share code, notes, and snippets.

View ryanjjones10's full-sized avatar

Ryan Jones ryanjjones10

  • San Francisco, CA
View GitHub Profile
import CoreBluetooth
import UIKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
func startManager(){
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if (central.state == CBCentralManagerState.PoweredOn) {
self.centralManager?.scanForPeripheralsWithServices(nil, options: nil)
self.currentState.text = "Scanning"
} else {
print("BLE not on")
}
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if (central.state == CBCentralManagerState.PoweredOn) {
self.centralManager?.scanForPeripheralsWithServices(nil, options: nil)
self.currentState.text = "Scanning"
} else {
print("BLE not on")
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
if (peripheral.name != nil && peripheral.name! == "Device Name"){
self.peripheral = peripheral
self.centralManager.connectPeripheral(self.peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
}
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
print("connected to \(peripheral)")
self.stopScan()
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?){
print("connection failed", error)
}
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?){
if self.peripheral != nil {
self.peripheral.delegate = nil
self.peripheral = nil
}
print("did disconnect", error)
self.startManager()
}
//
// ViewController.swift
// BLESwift
//
// Created by Ryan Jones on 6/14/16.
// Copyright © 2016 Ryan Jones. All rights reserved.
//
import CoreBluetooth
import UIKit
import CoreBluetooth
import UIKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
import CoreBluetooth
import UIKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
var currentCharacteristic: CBCharacteristic! = nil
var readRSSITimer: NSTimer!
var RSSIholder: NSNumber = 0