Skip to content

Instantly share code, notes, and snippets.

@paulw11
Created July 15, 2017 04:17
Show Gist options
  • Save paulw11/5d734957ddd575f9d00c758926984e76 to your computer and use it in GitHub Desktop.
Save paulw11/5d734957ddd575f9d00c758926984e76 to your computer and use it in GitHub Desktop.
//
// FirstViewController.swift
// AnotherBT
//
// Created by Paul Wilkinson on 15/7/17.
// Copyright © 2017 Paul Wilkinson. All rights reserved.
//
import UIKit
import CoreBluetooth
class FirstViewController: UIViewController {
var central: CBCentralManager!
var connectedPeripheral: CBPeripheral?
@IBOutlet weak var scanSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.central = CBCentralManager(delegate: self, queue: nil)
self.scanSwitch.isOn = false
self.scanSwitch.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func scanSwitched(_ sender: UISwitch) {
if sender.isOn {
self.central.scanForPeripherals(withServices: [CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f")], options: nil)
} else {
self.central.stopScan()
}
}
}
extension FirstViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
self.scanSwitch.isEnabled = true
} else {
self.scanSwitch.isEnabled = false
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered peripheral \(peripheral)")
self.connectedPeripheral = peripheral
central.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected peripheral \(peripheral)")
peripheral.discoverServices(nil)
peripheral.delegate = self
}
}
extension FirstViewController: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print(error.debugDescription)
if service.characteristics != nil {
for characteristic in service.characteristics! {
if characteristic.uuid == CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1") {
print("characteristic found")
peripheral.readValue(for: characteristic)
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if peripheral.services != nil {
for service in peripheral.services! {
if service.uuid == CBUUID(string: "dc495108-adce-4915-942d-bfc19cea923f") {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
}
}
//
// SecondViewController.swift
// AnotherBT
//
// Created by Paul Wilkinson on 15/7/17.
// Copyright © 2017 Paul Wilkinson. All rights reserved.
//
import UIKit
import CoreBluetooth
class SecondViewController: UIViewController {
@IBOutlet weak var advertiseSwitch: UISwitch!
var peripheralManager: CBPeripheralManager!
var service: CBMutableService!
var characteristic: CBMutableCharacteristic!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
self.advertiseSwitch.isOn = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupBT() {
self.service = CBMutableService(type: CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f"), primary: true)
self.characteristic = CBMutableCharacteristic(type: CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1"), properties: [.write, .read], value: nil, permissions: [.writeable, .readable])
self.service.characteristics = [characteristic]
self.peripheralManager.add(self.service)
}
@IBAction func advertiseSwitched(_ sender: UISwitch) {
if sender.isOn {
self.peripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey:[self.service.uuid]])
} else {
self.peripheralManager.stopAdvertising()
}
}
}
extension SecondViewController: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state {
case .poweredOn:
self.advertiseSwitch.isEnabled = true
if (self.service == nil) {
self.setupBT()
}
default:
self.advertiseSwitch.isEnabled = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment