IOS Swift Service Characteristics - ios

I'm trying to connect to a Bluetooth device (Adafruit Flora BLE) and looking for characteristic data to attach notification callbacks to. Problem is, I'm not seeing any characteristics listed. All I see are properties of the service itself and no characteristics.
Here is some code that shows where the identification of device and looking for service is occurring. Pardon the spaghetti code, just debugging/hacking together a prototype right now.
Basically, I'm trying to set up a notifier to the BLE service on the arduino/flora module so that I can send via UART sensor data. I have all the connection working, but can't seem to get the listener and updating portion working. Thanks!
func startScanning() {
print("Started scanning.")
visiblePeripheralUUIDs.removeAllObjects()
visiblePeripherals.removeAll(keepCapacity: true)
tableView.reloadData()
manager.scanForPeripheralsWithServices(nil, options: nil)
scanTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("stopScanning"), userInfo: nil, repeats: false)
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
let UUID = "818ECB19-BB61-BE95-2D04-64E5D90845EF";
if (peripheral.identifier.UUIDString == UUID){
// print("Peripheral found with name: \(peripheral.name)\nUUID: \(peripheral.identifier.UUIDString)\nRSSI: \(RSSI)\nAdvertisement Data: \(advertisementData)")
visiblePeripheralUUIDs.addObject(peripheral.identifier.UUIDString)
visiblePeripherals[peripheral.identifier.UUIDString] = Peripheral(peripheral: peripheral, RSSI: RSSI.stringValue, advertisementDictionary: advertisementData)
tableView.reloadData()
//
connectedPeripheral = peripheral
connectionAttemptTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("timeoutPeripheralConnectionAttempt"), userInfo: nil, repeats: false)
manager.connectPeripheral(connectedPeripheral!, options: nil)
}
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
//print("Peripheral connected: \(peripheral.name ?? peripheral.identifier.UUIDString)")
connectionAttemptTimer?.invalidate()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let peripheralViewController = storyboard.instantiateViewControllerWithIdentifier("PeripheralViewController") as! PeripheralViewController
peripheralViewController.peripheral = peripheral
navigationController?.pushViewController(peripheralViewController, animated: true)
print("--------Starting service discovery")
// print("!!!!!!!!!!Services be present \(peripheral.services?.count)");
// for service in (peripheral.services)! {
// print("SERVICES: \(service)")
// }
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
print("GOT TO PERIPHERAL")
if (peripheral != self.peripheral) {
// Wrong Peripheral
print("WrONG PERIPHERAL")
return
}
if (error != nil) {
print(" GOTS US AN ERROR")
return
}
print("CHECKING ON CHARACTERISTICS \(service.UUID)")
// for characteristic in service.characteristics! {
// print("CHARACTERISTIC \(characteristic)")
// if characteristic.UUID == service2 {
// print("WHAT THE F*** BRO \(characteristic as CBCharacteristic)");
// peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
//
// // Send notification that Bluetooth is connected and all required characteristics are discovered
// //self.sendBTServiceNotificationWithIsBluetoothConnected(true)
// }
// }
}
Updated the didDiscoverCharacteristicsForService and it's still not returning anything for me to parse in my for loop:
`func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
print("GOT TO PERIPHERAL")
if (peripheral != self.peripheral) {
// Wrong Peripheral
print("WrONG PERIPHERAL")
return
}
if (error != nil) {
print(" GOTS US AN ERROR")
return
}
print("What is service: \(service)")
for characteristic in service.characteristics! {
print("CHARACTERISTIC \(characteristic)")
if characteristic.UUID == service2 {
print("WHAT THE F*** BRO \(characteristic as CBCharacteristic)");
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
// Send notification that Bluetooth is connected and all required characteristics are discovered
//self.sendBTServiceNotificationWithIsBluetoothConnected(true)
}
}
}
Am I missing something stupid here?
Christian

Related

BLE shows connected even when 'didDisconnectPeripheral' is already called

I am trying to disconnect from a BLE device (raspberry-pi3), the delegates for disconnection are being triggered and upon checking explicitly if the peripheral is disconnected it shows that the peripheral is indeed disconnected. But, when I see in the settings app, I still see the device is connected.
Following is what I am trying:
Setup CBCentralManager as I come to the page.
func setupBLE() {
lblStatus.text = "Establising connection to the Scrubport"
self.manager = CBCentralManager(delegate: self, queue: .main)
}
Handle any state change if any
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
self.dismissAlert()
self.showLoader()
self.timer = Timer.scheduledTimer(withTimeInterval: 30, repeats: false, block: { _ in
// No device found. Stop scanning.
})
self.manager?.scanForPeripherals(withServices: [SERVICE_UUID])
return
}
else if central.state == .unauthorized {
// Permission handlers here:
return
}
// Handle all other cases.
}
Upon find the device try connecting
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
self.peripheral = peripheral
connect()
}
func connect() {
guard let peripheral = peripheral else {
// Making sure we got the peripheral.
return
}
lblStatus.text = "Connecting..."
self.manager?.connect(peripheral)
}
Upon connection discover the service
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
lblStatus.text = "Connected, preparing device for transfer..."
self.timer.invalidate()
central.stopScan()
peripheral.delegate = self
peripheral.discoverServices([SERVICE_UUID])
}
Find the characteristics next
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let services = peripheral.services {
for service in services {
if service.uuid == SERVICE_UUID {
peripheral.discoverCharacteristics([CHARACTERISTICS_UUID], for: service)
}
}
}
}
Request notification
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.uuid == self.CHARACTERISTICS_UUID {
self.characteristic = characteristic
peripheral.setNotifyValue(true, for: characteristic)
}
}
}
}
Upon getting an update for notification change I do the write and it is all successful.
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if characteristic.isNotifying {
self.stopLoader()
lblStatus.text = "Notifying now.."
} else {
print("Un-notified")
print("Trying disconnection")
manager?.cancelPeripheralConnection(peripheral)
}
}
At the end I want to disconnect, or handle if app is closed or pressed back mid way
Trying clean up using following code:
fileprivate func cleanup() {
print("CLEANING")
guard let manager = manager else {
return
}
print("Check scan.")
if manager.isScanning {
print("Stopping scan.")
manager.stopScan()
}
// Don't do anything if we're not connected
// self.discoveredPeripheral.isConnected is deprecated
print("Checking peripheral connection")
guard peripheral?.state == .connected else {
print("No peripheral connected.")
return
}
// See if we are subscribed to a characteristic on the peripheral
print("Checking services")
guard let services = peripheral?.services else {
print("No services connection found.")
cancelPeripheralConnection()
return
}
print("Looping services")
for service in services {
print("Checking characteristics")
guard let characteristics = service.characteristics else {
print("No characteristics")
continue
}
print("Looping characteristics")
for characteristic in characteristics {
print("Comparing characteristics UUID is notifying")
if characteristic.uuid.isEqual(CHARACTERISTICS_UUID) && characteristic.isNotifying {
print("Un-notifying")
peripheral?.setNotifyValue(false, for: characteristic)
} else {
print("Nope not the one.", characteristic.isNotifying)
}
}
}
}
fileprivate func cancelPeripheralConnection() {
print("Remove peripheral connection")
guard let manager = manager, let peripheral = peripheral else {
print("Manager or peripheral not found!")
return
}
print("Cancelling peripheral connection.")
// If we've got this far, we're connected, but we're not subscribed, so we just disconnect
manager.cancelPeripheralConnection(peripheral)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("SERVICE INFO: didDisconnectPeripheral", peripheral.name ?? "Unknown")
print("Was there an error?", error ?? "No error")
self.peripheral = nil
let peripherals = central.retrieveConnectedPeripherals(withServices: [])
let _peri = central.retrievePeripherals(withIdentifiers: [peripheral.identifier])
_peri.forEach { per in
print(per.state == .connected, "connected")
print(per.state == .connecting, "connecting")
print(per.state == .disconnected, "disconnected")
print(per.state == .disconnecting, "disconnecting")
}
print(peripherals)
}
Following are the logs that get printed on cleanup():
CLEANING
Check scan.
Checking peripheral connection
Checking services
Looping services
Checking characteristics
Looping characteristics
Comparing characteristics UUID is notifying
Un-notifying
Un-notified
Trying disconnection
SERVICE INFO: didDisconnectPeripheral raspberrypi-cm3
Was there an error? No error
false connected
false connecting
true disconnected
false disconnecting
[]
But this what I see in the settings app after clean up.

have to initiate a BLE peripheral every time i use it in Swift

with CBCentralManager i succesully scan multiple BLE UART devices and connect to them, de devices are stored in the array periphirals[], but when i want to send data to them one by one, only the last one connected i can send succesfully send data to, i solved it by calling blePeripheral?.discoverServices([ParticlePeripheral.BLEService_UUID])before writing data again, but i think this is not the right solution, can someone explain what i am doing wrong?
Below the code i use, the scanning and connecting start by didload
and startCyclus starts the first device, after the data recieved "didUpdateValueFor" is entered , getting the data and direct send data to the next peripheral
import Foundation
import UIKit
import CoreBluetooth
var txCharacteristic : CBCharacteristic?
var rxCharacteristic : CBCharacteristic?
var blePeripheral : CBPeripheral?
var characteristicASCIIValue = NSString()
var Running = false
var currentNode = 0;
var updateService = false
var maxNodes = Int()
class ViewController : UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate, UITableViewDelegate{
//Data
var centralManager : CBCentralManager!
var RSSIs = [NSNumber]()
var data = NSMutableData()
var peripherals: [CBPeripheral] = []
var characteristicValue = [CBUUID: NSData]()
var timer = Timer()
var characteristics = [String : CBCharacteristic]()
var teller = 0
//UI
#IBOutlet weak var baseTableView: UITableView!
#IBOutlet weak var refreshButton: UIBarButtonItem!
#IBAction func naarRun(_ sender: Any) {
print("naarRun")
self.performSegue(withIdentifier:"naarRunView" , sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "naarRunView") {
let vc = segue.destination as! ViewRun
vc.peripherals = peripherals
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//print("View Cleared")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("Stop Scanning")
centralManager?.stopScan()
}
/*Okay, now that we have our CBCentalManager up and running, it's time to start searching for devices. You can do this by calling the "scanForPeripherals" method.*/
func startScan() {
peripherals = []
print("Now Scanning...")
self.timer.invalidate()
centralManager?.scanForPeripherals(withServices: [ParticlePeripheral.BLEService_UUID] , options: [CBCentralManagerScanOptionAllowDuplicatesKey:false])
Timer.scheduledTimer(withTimeInterval: 3, repeats: false) {_ in
self.cancelScan()
self.connectToAllDevices()
}
}
/*We also need to stop scanning at some point so we'll also create a function that calls "stopScan"*/
func cancelScan() {
self.centralManager?.stopScan()
print("Scan Stopped")
print("Number of Peripherals Found: \(peripherals.count)")
}
func disconnectFromDevice () {
if blePeripheral != nil {
// We have a connection to the device but we are not subscribed to the Transfer Characteristic for some reason.
// Therefore, we will just disconnect from the peripheral
centralManager?.cancelPeripheralConnection(blePeripheral!)
}
}
func restoreCentralManager() {
//Restores Central Manager delegate if something went wrong
centralManager?.delegate = self
}
/*
Called when the central manager discovers a peripheral while scanning. Also, once peripheral is connected, cancel scanning.
*/
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,advertisementData: [String : Any], rssi RSSI: NSNumber) {
blePeripheral = peripheral
teller+=1
self.peripherals.append(peripheral)
self.RSSIs.append(RSSI)
peripheral.delegate = self
//self.baseTableView.reloadData()
//if blePeripheral == nil {
print("Found new pheripheral devices with services")
print("Peripheral name: \(String(describing: peripheral.name))")
print("**********************************")
print ("Advertisement Data : \(advertisementData)")
//}
}
//Peripheral Connections: Connecting, Connected, Disconnected
//-Connection
func connectToDevice () {
centralManager?.connect(blePeripheral!, options: nil)
}
func connectToAllDevices(){
var seconds = 0.0
for per in peripherals
{
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
blePeripheral = per
print("Connecting naar " + (blePeripheral?.name!)!)
self.connectToDevice ()
}
seconds = seconds + 0.5;
}
}
/*
Invoked when a connection is successfully created with a peripheral.
This method is invoked when a call to connect(_:options:) is successful. You typically implement this method to set the peripheral’s delegate and to discover its services.
*/
//-Connected
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("*****************************")
print("Connection complete")
print("Peripheral info: \(String(describing: blePeripheral))")
//Erase data that we might have
data.length = 0
//Discovery callback
peripheral.delegate = self
//Only look for services that matches transmit uuid
peripheral.discoverServices([ParticlePeripheral.BLEService_UUID])
/*
//Once connected, move to new view controller to manager incoming and outgoing data
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let uartViewController = storyboard.instantiateViewController(withIdentifier: "UartModuleViewController") as! UartModuleViewController
uartViewController.peripheral = peripheral
navigationController?.pushViewController(uartViewController, animated: true)
*/
}
/*
Invoked when the central manager fails to create a connection with a peripheral.
*/
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
if error != nil {
print("Failed to connect to peripheral")
return
}
}
func disconnectAllConnection() {
centralManager.cancelPeripheralConnection(blePeripheral!)
}
/*
Invoked when you discover the peripheral’s available services.
This method is invoked when your app calls the discoverServices(_:) method. If the services of the peripheral are successfully discovered, you can access them through the peripheral’s services property. If successful, the error parameter is nil. If unsuccessful, the error parameter returns the cause of the failure.
*/
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("*******************************************************")
if ((error) != nil) {
print("Error discovering services: \(error!.localizedDescription)")
return
}
guard let services = peripheral.services else {
return
}
//We need to discover the all characteristic
for service in services {
peripheral.discoverCharacteristics(nil, for: service)
// bleService = service
}
print("Discovered Services: \(services)")
}
/*
Invoked when you discover the characteristics of a specified service.
This method is invoked when your app calls the discoverCharacteristics(_:for:) method. If the characteristics of the specified service are successfully discovered, you can access them through the service's characteristics property. If successful, the error parameter is nil. If unsuccessful, the error parameter returns the cause of the failure.
*/
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print("*******************************************************")
if ((error) != nil) {
print("Error discovering services: \(error!.localizedDescription)")
return
}
guard let characteristics = service.characteristics else {
return
}
print("Found \(characteristics.count) characteristics!")
for characteristic in characteristics {
//looks for the right characteristic
if characteristic.uuid.isEqual(ParticlePeripheral.BLE_Characteristic_uuid_Rx) {
rxCharacteristic = characteristic
//Once found, subscribe to the this particular characteristic...
peripheral.setNotifyValue(true, for: rxCharacteristic!)
// We can return after calling CBPeripheral.setNotifyValue because CBPeripheralDelegate's
// didUpdateNotificationStateForCharacteristic method will be called automatically
peripheral.readValue(for: characteristic)
print("Rx Characteristic: \(characteristic.uuid)")
}
if characteristic.uuid.isEqual(ParticlePeripheral.BLE_Characteristic_uuid_Tx){
txCharacteristic = characteristic
print("Tx Characteristic: \(characteristic.uuid)")
}
peripheral.discoverDescriptors(for: characteristic)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
print("*******************************************************")
if error != nil {
print("\(error.debugDescription)")
return
}
guard let descriptors = characteristic.descriptors else { return }
descriptors.forEach { descript in
print("function name: DidDiscoverDescriptorForChar \(String(describing: descript.description))")
print("Rx Value \(String(describing: rxCharacteristic?.value))")
print("Tx Value \(String(describing: txCharacteristic?.value))")
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
print("*******************************************************")
if (error != nil) {
print("Error changing notification state:\(String(describing: error?.localizedDescription))")
} else {
print("Characteristic's value subscribed")
}
if (characteristic.isNotifying) {
print ("Subscribed. Notification has begun for: \(characteristic.uuid)")
}
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("Disconnected")
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
guard error == nil else {
print("Error discovering services: error")
return
}
print("Message sent")
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
guard error == nil else {
print("Error discovering services: error")
return
}
print("Succeeded!")
}
// MARK: - Getting Values From Characteristic
/** After you've found a characteristic of a service that you are interested in, you can read the characteristic's value by calling the peripheral "readValueForCharacteristic" method within the "didDiscoverCharacteristicsFor service" delegate.
*/
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard characteristic == rxCharacteristic,
let characteristicValue = characteristic.value,
let ASCIIstring = NSString(data: characteristicValue,
encoding: String.Encoding.utf8.rawValue)
else { return }
characteristicASCIIValue = ASCIIstring
let ontvangen = String(characteristicASCIIValue)
let waarde = haalwaarde(recieved: ontvangen)
print("Value Recieved: " + String(characteristicASCIIValue) )
NotificationCenter.default.post(name:NSNotification.Name(rawValue: "Notify"), object: self)
if(updateService){updateService=false; return}
if(!waarde.isNumber){print("waarde is no number");return}
if(Running ){Vervolg()}
}
func haalNode(recieved:String)->String{
//nodenr
if(recieved.count<10){return "fout";}
let temp = recieved
let lengte = recieved.count;
let start = temp.index(temp.startIndex, offsetBy: 7)
let end = temp.index(temp.endIndex, offsetBy: -(lengte-8))
let range = start..<end
let nodeNr = String(temp[range])
//print("nodeNr=", nodeNr)
return nodeNr
}
func haalwaarde(recieved:String)->String{
//Reactietijd
if(recieved.count<10){return "fout"};
let temp = recieved
let lengte = recieved.count;
let start = temp.index(temp.startIndex, offsetBy: 1)
let end = temp.index(temp.endIndex, offsetBy: -(lengte-7))
let range = start..<end
let Reactietijd = String(temp[range])
//print("\nreactietijd=", Reactietijd)
return Reactietijd
}
func startCyclus(){
currentNode = 0
if maxNodes == 0 {return}
Running = true
blePeripheral = peripherals[0]
updateService = true
blePeripheral?.discoverServices([ParticlePeripheral.BLEService_UUID])
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.writeValue(data: "N 5000G55")
currentNode += 1
}
func Vervolg(){
print("vervolg")
if currentNode == maxNodes {Running = false;return}
blePeripheral = peripherals[currentNode]
updateService = true
blePeripheral?.discoverServices([ParticlePeripheral.BLEService_UUID])
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) {
self.writeValue(data: "N 5000G55")
currentNode += 1
}
}
// Write functions
func writeValue(data: String){
print("schrijfdata " + String(currentNode))
let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue)
//change the "data" to valueString
if let blePer = blePeripheral{
if let txCharacteristic = txCharacteristic {
blePer.writeValue(valueString!, for: txCharacteristic, type: CBCharacteristicWriteType.withoutResponse)
}
}
}
/*
Invoked when the central manager’s state is updated.
This is where we kick off the scan if Bluetooth is turned on.
*/
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == CBManagerState.poweredOn {
// We will just handle it the easy way here: if Bluetooth is on, proceed...start scan!
print("Bluetooth Enabled")
startScan()
} else {
//If Bluetooth is off, display a UI alert message saying "Bluetooth is not enable" and "Make sure that your bluetooth is turned on"
print("Bluetooth Disabled- Make sure your Bluetooth is turned on")
let alertVC = UIAlertController(title: "Bluetooth is not enabled", message: "Make sure that your bluetooth is turned on", preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) -> Void in
self.dismiss(animated: true, completion: nil)
})
alertVC.addAction(action)
self.present(alertVC, animated: true, completion: nil)
}
}
}
extension String {
var isNumber: Bool {
let characters = CharacterSet.decimalDigits.inverted
return !self.isEmpty && rangeOfCharacter(from: characters) == nil
}
}
I think it's your logging is hiding what's really happening.
You reassign blePeripheral before each connection, but if any of them take longer than 0.5 seconds, by the time didConnect is called, the device that has connected won't be the one that's referred to by blePeripheral.
I would suggest removing blePeripheral entirely, and explicitly pass the CBPeripheral object into connectToDevice. Also, in didConnect, use the peripheral parameter that's passed in.

iOS 11 Core Bluetooth restoration not working

I have an iOS app that uses Core Bluetooth to connect to a V.BTTN smart button. Everything worked perfectly fine in iOS 10, but since iOS 11 has come out the restoration process seems to break.
When I launch my app, with a previously paired button, the OS does in fact call the centralManager:willRestoreState, and the accompanying dictionary includes a pointer to a CBPeripheral object that has a status of connected. Just as it did back in iOS 10. However, the problem I am running into is when I call the discoverServices on the peripheral I am never returned any services in the peripheral:didDiscoverServices method. In fact that method, nor any other method, is called at all.
I have been searching all of the internet and have found people having similar issues with button pairing an connections, but those have generally been issues with improper lifecycle management of the peripheral object. I believe I have everything setup correct and at a loss. Does anyone have any idea what could be going on here?
import Foundation
import CoreBluetooth
class SmartButtonManager: NSObject {
//MARK: - Singleton
static let sharedManager = SmartButtonManager()
//MARK: - Properties
var discoveredDevices: [CBPeripheral] {
get {
return perfs
}
}
fileprivate(set) var isBluetoothOn = false
//MARK: - Private Constants & Variables
fileprivate var connectedButtonUUID: String?
fileprivate let queue = DispatchQueue(label: "V.BTTN", qos: .background, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)
fileprivate var manager: CBCentralManager?
fileprivate var perfs = [CBPeripheral]()
fileprivate var connectedPerf: CBPeripheral?
fileprivate var isButtonReady = false
//MARK: - Initialization
override init() {
super.init()
}
//MARK: - Configure
func configure(withLaunchOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
// create new central manager
manager = CBCentralManager(delegate: self, queue: queue, options: [CBCentralManagerOptionRestoreIdentifierKey: managerIdentifier])
}
}
//MARK: - V.BTTN
extension SmartButtonManager: CBCentralManagerDelegate, CBPeripheralDelegate {
func scanForVbttn() {
perfs.removeAll()
manager?.scanForPeripherals(withServices: services, options: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOff:
print("[centralManagerDidUpdateState] CB BLE hardware is powered off")
perfs.removeAll()
isBluetoothOn = false
case .poweredOn:
print("[centralManagerDidUpdateState] CB BLE hardware is powered on. Start scanning for peripherals")
isBluetoothOn = true
case .unauthorized:
print("[centralManagerDidUpdateState] CB BLE hardware is not authorized")
case .unsupported:
print("[centralManagerDidUpdateState] CB BLE hardware is not supported")
isBluetoothOn = false
default:
print("[centralManagerDidUpdateState] CB BLE hardware state is unknown")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("[centralManager:didDiscover peripheral:advertisementData] CB BLE did discover peripheral with advertisement data: \(advertisementData)")
guard let perfName = advertisementData[CBAdvertisementDataLocalNameKey] as? String else {
print("[centralManager:didDiscover peripheral:advertisementData] peripheral name is unknown")
return
}
if perfName.contains("V.ALRT") {
peripheral.delegate = self
perfs.append(peripheral)
if connectedButtonUUID == perfName {
connect(peripheral: peripheral)
}
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("[central:didConnect peripheral:] CB BLE hardware did connect")
handleDidConnect(toPeripheral: peripheral)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("[central:didFailToConnect error:] CB BLE peripheral did failed to connect with error: \(String(describing: error))")
connectedPerf = nil
isButtonReady = false
connectedButtonUUID = nil
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("[central:didDisconnectPeripheral peripheral:] CB BLE peripheral did disconnect")
connectedPerf = nil
isButtonReady = false
connectedButtonUUID = nil
}
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
print("[central:willRestoreState dict:] CB BLE hardware will restore state")
print("\(dict)")
guard let ps = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] else {
print("[central:willRestoreState dict:] No perfs to restore")
return
}
print("[central:willRestoreState dict:] Will restore perfs")
perfs = ps
print("[central:willRestoreState dict:] Attempt to reconnect to V.BTTN")
print("[central:willRestoreState dict:] perfs \(perfs)")
for p in perfs {
if p.name == connectedButtonUUID {
print("[central:willRestoreState dict:] Connect to perf \(p)")
handleDidConnect(toPeripheral: p)
break
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else {
print("[peripheral:didDiscoverServices error:] CB BLE peripheral unable to discover services")
return
}
print("[peripheral:didDiscoverServices error:] BLE peripheral did discover services")
for s in services {
print("[peripheral:didDiscoverServices error:] CB BLE Service \(s.description)")
peripheral.discoverCharacteristics(nil, for: s)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print("[peripheral:didDiscoverCharacteristicsFor service:] CB BLE did discover characteristics for service \(service.uuid.description)")
if compareCBUUID(uuid1: service.uuid, uuid2: CBUUID(string: BLE_VSN_GATT_SERVICE_UUID)) {
guard let characteristics = service.characteristics else {
return
}
for aChar in characteristics {
// write the verification key
if aChar.uuid.isEqual(CBUUID(string: BLE_VERIFICATION_SERVICE_UUID)) {
self.writeVerificationKey(forPeripheral: peripheral, characteristic: aChar)
self.enable(peripheral: peripheral)
break
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
//NOTE: This code has been omitted as there is little need to see this
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
print("[peripheral:didWriteValueFor characteristic:] characteristic: \(characteristic.uuid)")
if let e = error {
print("[peripheral:didWriteValueFor characteristic:] error: \(e)")
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if !characteristic.isNotifying {
print("[peripheral:didUpdateNotificationStateFor:][Characteristic is not notifiying so cancel the connection]")
manager?.cancelPeripheralConnection(peripheral)
}
}
//MARK: - Helpers
fileprivate func writeVerificationKey(forPeripheral peripheral: CBPeripheral, characteristic: CBCharacteristic) {
print("[peripheral:didDiscoverCharacteristicsFor service:] Write verification key")
let data = NSData(bytes: [0x80,0xBE,0xF5,0xAC,0xFF] as [UInt8], length: 5)
peripheral.writeValue(data as Data, for: characteristic, type: CBCharacteristicWriteType.withResponse)
}
fileprivate func handleDidConnect(toPeripheral peripheral: CBPeripheral) {
if let n = peripheral.name {
connectedButtonUUID = n
}
connectedPerf = peripheral
peripheral.delegate = self
peripheral.discoverServices(nil)
isButtonReady = true
}
fileprivate func enable(peripheral: CBPeripheral) {
guard let services = peripheral.services else {
return
}
for service: CBService in services {
if compareCBUUID(uuid1: service.uuid, uuid2: CBUUID(string: BLE_VSN_GATT_SERVICE_UUID)) {
guard let characteristics = service.characteristics else {
return
}
for aChar in characteristics {
if aChar.uuid.isEqual(CBUUID(string: BLE_KEYPRESS_DETECTION_UUID)) {
// enable button
print("[peripheral:didDiscoverCharacteristicsFor service:] Enable short press")
let data = NSData(bytes: [0x02] as [UInt8], length: 1)
peripheral.writeValue(data as Data, for: aChar, type: CBCharacteristicWriteType.withResponse);
} else if aChar.uuid.isEqual(CBUUID(string: BLE_SILENT_NORMAL_MODE)) {
// enable button
print("[peripheral:didDiscoverCharacteristicsFor service:] Enable normal mode")
let data = NSData(bytes: [0x00] as [UInt8], length: 1)
peripheral.writeValue(data as Data, for: aChar, type: CBCharacteristicWriteType.withResponse);
} else if aChar.uuid.isEqual(CBUUID(string: BLE_FALL_KEYPRESS_DETECTION_UUID)) {
// enable fall detection
print("[peripheral:didDiscoverCharacteristicsFor service:] Enable fall detection")
peripheral.setNotifyValue(true, for: aChar)
}
}
}
}
checkBatteryLevel()
}
func disconnectVbttn() {
guard let peripheral = connectedPerf else {
return
}
setVbttnToStealthMode()
manager?.cancelPeripheralConnection(peripheral)
isButtonReady = false
}
fileprivate func compareCBUUID(uuid1: CBUUID, uuid2: CBUUID) -> Bool {
if (uuid1.data as NSData).isEqual(to: uuid2.data) {
return true
}
return false
}
func stopScanning() {
manager?.stopScan()
}
}

iOS BluetoothLE: CBCentralManager Unsubscribes From Updates

I have a mac set up as a bluetooth accessory using CoreBluetooth as a CBPeripheralManager. The Mac is advertising on a set characteristic CBUUID, and once it has a subscriber, I click a button to stream a UTF-8-encoded time stamp every half second.
I have an iPhone set up as a CBCentralManager subscribing to the appropriate characteristic. I update the iPhone's UI with the decoded timestamp every time it receives the data and the app is active. The bluetooth-central background mode is set in the .plist file.
The iPhone continues to debug print and update the UI for the timestamps for about 25 seconds, then just stops. This happens whether the app is in the foreground or the background. EDIT: The CBPeripheralManager receives an didUnsubscribeFrom characteristic callback at this time. I don't see any reason didUnsubscribeFrom would be called, no idea why it's always after 25 seconds.
The CBPeripheralManager continues to merrily send its time stamps. The return value from the CBPeripheralManager's updateData(_:for:onSubscribedCentrals:) call is always true, indicating that the queue never is full.
There's a lot of code involved here, showing the most relevant.
From the CBCentralManager app:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print(central.state.rawValue)
centralManager.scanForPeripherals(withServices: [timeUUID], options: nil)
print("scanning")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
print("connected")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print(peripheral.name as Any)
print(advertisementData[CBAdvertisementDataServiceUUIDsKey] as! Array<CBUUID>)
self.peripheralController = peripheral
self.centralManager.connect(peripheral, options: nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if service.uuid.uuidString == timeUUID.uuidString {
peripheralController.setNotifyValue(true, for: service.characteristics!.first!)
peripheralController.readValue(for: service.characteristics!.first!) // EDIT: This was the offending line
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic.uuid.uuidString == timeUUID.uuidString {
if let valueFrom = characteristic.value {
if let this = String(data: valueFrom, encoding: .utf8) {
if UIApplication.shared.applicationState == .active {
label.text = this
print("ACTIVE \(this)")
} else if UIApplication.shared.applicationState == .background {
print("BACKGROUND \(this)")
} else if UIApplication.shared.applicationState == .inactive {
print("INACTIVE \(this)")
}
}
}
}
}
From the CBPeripheralManager app:
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
myCharacteristic = CBMutableCharacteristic(type: myServiceUUID, properties: [CBCharacteristicProperties.read, CBCharacteristicProperties.notify], value: nil, permissions: CBAttributePermissions.readable)
let myService = CBMutableService(type: myServiceUUID, primary: true)
myService.characteristics = [myCharacteristic]
bluetoothController.add(myService)
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
print(characteristic.uuid)
subscriber = central
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
print(characteristic)
print("unsubscribe")
}
func repeatAdvertisement() {
timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [unowned self] (timerRef) in
guard let maybeTimer = self.timer, maybeTimer.isValid else { return }
let datum = Date()
let stringFromDate = self.dateFormatter.string(from: datum)
let data = stringFromDate.data(using: .utf8)
print(data!.count)
// myCharacteristic.value = data
let myService = CBMutableService(type: self.myServiceUUID, primary: true)
myService.characteristics = [self.myCharacteristic]
let did = self.bluetoothController.updateValue(data!, for: self.myCharacteristic as! CBMutableCharacteristic, onSubscribedCentrals: [self.subscriber])
print("timed \(stringFromDate) \(did)")
}
}
func advertise() {
if timer == nil {
repeatAdvertisement()
} else {
timer?.invalidate()
timer = nil
}
}
Let me know anything else you need.
Okay, for heaven's sake. The issue was the line peripheralController.readValue(for: service.characteristics!.first!) I had that line in the app based on some sample code and, well, it was unnecessary.
Apparently the call to readValue(for:) causes some sort of timeout. I edited that line out of the app and it happily updates on and on.
Leaving the question up and adding this answer in case anyone ends up facing the same thing someday.

Sending data to Bluno from iOS

I've recently bought a Bluno and am trying to create an iPhone app to talk to it. The Bluno makers include source code but it's in objective-c and I'm trying to port it to swift. Currently I can discover the Bluno, connect to it and see it's services. However I seem to see no characteristics, and get a nil when I print them. I've setup the Bluno so that it flashes once I send it the character "5" but I can't seem to find the correct characteristic in order to do so. Any help would be much appreciated. Here is my current code, the Obj-C code can be found here:
//
// ViewController.swift
// Bluetooth-Interaction
//
// Created by Frederik Lohner on 26/Oct/15.
// Copyright © 2015 JeongGroup. All rights reserved.
//
import UIKit
import CoreBluetooth
import SnapKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
let backgroundView = UIView()
let scanButton = UIButton()
var DFRobotServiceId = "0000dfb0-0000-1000-8000-00805f9b34fb"
var kBlunoService = "DFB1"
var kBlunoDataCharacteristic = "dfb1"
var DFRobotCharacteristicsNameSerialPortId = "0000dfb1-0000-1000-8000-00805f9b34fb"
var NameCommandId = "0000dfb2-0000-1000-8000-00805f9b34fb"
// BLE
var centralManager: CBCentralManager!
var sensorTagPeripheral: CBPeripheral!
override func viewDidLoad() {
scanButton.setTitle("Scan", forState: UIControlState.Normal)
scanButton.addTarget(self, action: "startScanning", forControlEvents: UIControlEvents.TouchUpInside)
scanButton.backgroundColor = UIColor.blackColor()
backgroundView.addSubview(scanButton)
self.view.addSubview(backgroundView)
backgroundView.snp_makeConstraints { (make) -> Void in
make.left.right.top.bottom.equalTo(self.view)
}
scanButton.snp_makeConstraints { (make) -> Void in
make.left.bottom.equalTo(backgroundView)
make.width.height.equalTo(60)
}
// Initialize central manager on load
centralManager = CBCentralManager(delegate: self, queue: nil)
// self.centralManager.stopScan()
}
func startScanning() {
// print("Started Scanning!")
// //Could add service UUID here to scan for only relevant services
// self.centralManager.scanForPeripheralsWithServices(nil, options: nil)
let one = "1"
let data = one.dataUsingEncoding(NSUTF8StringEncoding)
// self.sensorTagPeripheral.writeValue(data!, forCharacteristic: , type: .WithoutResponse)
// self.sensorTagPeripheral.writeValue(data!, forCharacteristic: CBCharacteristic., type: .WithoutResponse)
// self.centralManager.
}
// Check status of BLE hardware
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
print("Bluetooth is ON")
central.scanForPeripheralsWithServices(nil, options: nil)
} else if central.state == CBCentralManagerState.Resetting {
print("RESETTING")
} else if central.state == CBCentralManagerState.Unauthorized {
print("Not Authorized")
} else {
print("Bluetooth switched off or not initialized")
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
// print(peripheral)
let deviceName = "Bluno"
if let nameOfDeviceFound = peripheral.name {
if (nameOfDeviceFound == deviceName) {
print("Name was found")
print("")
print("")
print(peripheral)
// for (key, value) in advertisementData {
// print("\(key) -> \(value)")
// }
// Stop scanning
self.centralManager.stopScan()
print("Stopped Scanning")
// Set as the peripheral to use and establish connection
self.sensorTagPeripheral = peripheral
self.sensorTagPeripheral.delegate = self
self.centralManager.connectPeripheral(peripheral, options: nil)
print("")
print("")
print("Connected")
print("")
}
else {
print("NOPE.EXE")
}
}
}
// // Check if the service discovered is a valid IR Temperature Service
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
if(error != nil) {
print(error?.description)
}
for service in peripheral.services! {
let thisService = service as CBService
print("Discovered Service: \(thisService.description)")
print("Discovered Characteristic: \(thisService.characteristics)")
}
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
if(error != nil) {
print(error?.description)
}
for characteristic in service.characteristics! {
print("Characteristic found: \(characteristic)")
let one = "1"
let data = one.dataUsingEncoding(NSUTF8StringEncoding)
peripheral.writeValue(data!, forCharacteristic: characteristic, type: .WithoutResponse)
if(String(characteristic.UUID) == kBlunoService) {
print("Found")
}
}
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("Did connect to peripheral.", terminator:"")
peripheral.delegate = self
peripheral.discoverServices(nil)
print(peripheral)
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("Failed to connect to peripheral.")
}
// func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
// print("CONNECTION FAILED")
// }
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("CONNECTION WAS DISCONNECTED")
}
}
Managed to get this to work. Code can be found here.

Resources