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!
var currentCharacteristic: CBCharacteristic! = nil
var readRSSITimer: NSTimer!
var RSSIholder: NSNumber = 0
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?){
for characteristic in service.characteristics! {
let thisCharacteristic = characteristic as CBCharacteristic
if thisCharacteristic.UUID == txCharacteristic{
self.currentCharacteristic = thisCharacteristic
let openValue = "1".dataUsingEncoding(NSUTF8StringEncoding)!
self.peripheral.writeValue(openValue, forCharacteristic: self.currentCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}
}
if let error = error {
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?){
peripheral.discoverCharacteristics(nil, forService: peripheral.services![0])
}
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
import CoreBluetooth
import UIKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
//
// ViewController.swift
// BLESwift
//
// Created by Ryan Jones on 6/14/16.
// Copyright © 2016 Ryan Jones. All rights reserved.
//
import CoreBluetooth
import UIKit
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()
}
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, 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 centralManagerDidUpdateState(central: CBCentralManager) {
if (central.state == CBCentralManagerState.PoweredOn) {
self.centralManager?.scanForPeripheralsWithServices(nil, options: nil)
self.currentState.text = "Scanning"
} else {
print("BLE not on")
}
}