So I am following this iOS App code example from http://www.raywenderlich.com/85900/arduino-tutorial-integrating-bluetooth-le-ios-swift
The code below, taken from the link above, registers one characteristic with the service. How would you register multiple characteristics for one service so that they can be read and written to, using Swift?
/* Services & Characteristics UUIDs */
let BLEServiceUUID = CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let PositionCharUUID = CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")
let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"
.
.
.
.
.
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
let uuidsForBTService: [CBUUID] = [PositionCharUUID]
if (peripheral != self.peripheral) {
// Wrong Peripheral
return
}
if (error != nil) {
return
}
if ((peripheral.services == nil) || (peripheral.services.count == 0)) {
// No Services
return
}
for service in peripheral.services {
if service.UUID == BLEServiceUUID {
peripheral.discoverCharacteristics(uuidsForBTService, forService: service as CBService)
}
}
}
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
if (peripheral != self.peripheral) {
// Wrong Peripheral
return
}
if (error != nil) {
return
}
for characteristic in service.characteristics {
if characteristic.UUID == PositionCharUUID {
self.positionCharacteristic = (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)
}
}
Thanks in advance!
I think its as simple as just adding more if statements in your for loop. Like this:
let characteristicOneUUID= CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let characteristicTwoUUID= CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")
for characteristic in service.characteristics {
//Characteristic One
if characteristic.UUID == characteristicOneUUID{
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
}
//Characteristic Two
if characteristic.UUID == characteristicTwoUUID{
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
}
}
Related
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.
I created my custom peripheral to write data to peripheral and tried to write data from my central.
when write value function is executed i am getting request is not supported error.
Here is my code.Hope you understand my problem.
Looking for the solution to fix.
Thanks in advance.
connectedPeripheral?.writeValue(data, for: characteristic, type: .withResponse)
Setup up my custom BLEPeripheral and startAdvertising in Peripheral
// MARK: CBPeripheralManagerDelegate
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
setup()
} else {
print("peripheral is not available: \(peripheral.state.rawValue)")
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
if let error = error {
print("Could not add service: \(error.localizedDescription)")
} else {
print("peripheral added service. Start advertising")
let advertisementData: [String: Any] = [
CBAdvertisementDataServiceUUIDsKey: [CBUUID(string: BLEIdentifiers.serviceIdentifier)],
CBAdvertisementDataLocalNameKey: "BLE Sensor" // This key will not be transmitted when app is backgrounded
]
manager.startAdvertising(advertisementData)
}
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
if let error = error {
print("Could not start advertising: \(error.localizedDescription)")
} else {
print("peripheral started advertising")
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
print("Did receive read request: \(request)")
if !request.characteristic.uuid.isEqual(characteristic.uuid) {
peripheral.respond(to: request, withResult: .requestNotSupported)
} else {
guard let value = characteristic.value else {
peripheral.respond(to: request, withResult: .invalidAttributeValueLength)
return
}
if request.offset > value.count {
peripheral.respond(to: request, withResult: .invalidOffset)
} else {
request.value = value.subdata(in: request.offset..<value.count-request.offset)
peripheral.respond(to: request, withResult: .success)
}
}
}
func setup() {
let characteristicUUID = CBUUID(string: BLEIdentifiers.characteristicIdentifier)
characteristic = CBMutableCharacteristic(type: characteristicUUID, properties: [.read, .write,.notify], value: nil, permissions: [.readable,.writeable])
let descriptor = CBMutableDescriptor(type: CBUUID(string: CBUUIDCharacteristicUserDescriptionString), value: "BLESensor prototype")
characteristic.descriptors = [descriptor]
let serviceUUID = CBUUID(string: BLEIdentifiers.serviceIdentifier)
let service = CBMutableService(type: serviceUUID, primary: true)
service.characteristics = [characteristic]
manager.add(service)
}
Writing data to Peripheral in Central
func writeDataToPeripheral(data: Data){
if let characteristics = ctService?.characteristics {
for characteristic in characteristics {
if characteristic.uuid == CBUUID(string: BLEIdentifiers.characteristicIdentifier) {
if characteristic.properties.contains(.write) {
connectedPeripheral?.writeValue(data, for: characteristic, type: .withResponse)
}
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
guard error == nil else {
print("Error discovering didWriteValueFor: error", error.debugDescription)
//Getting Error Domain=CBATTErrorDomain Code=6 "The request is not supported."
return
}
print("Message sent")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
print("peripheral failed to discover services: \(error.localizedDescription)")
} else {
peripheral.services?.forEach({ (service) in
print("service discovered: \(service)")
peripheral.discoverCharacteristics([CBUUID(string: BLEIdentifiers.characteristicIdentifier)], for: service)
})
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let error = error {
print("NSA A peripheral failed to discover characteristics: \(error.localizedDescription)")
} else {
ctService = service
service.characteristics?.forEach({ (characteristic) in
print("NSA A characteristic discovered: \(characteristic)")
if characteristic.uuid == CBUUID(string: BLEIdentifiers.characteristicIdentifier) {
// keep a reference to this characteristic so we can write to it
writeCharacteristic = characteristic
}
if characteristic.properties.contains(.read) {
peripheral.readValue(for: characteristic)
}
peripheral.discoverDescriptors(for: characteristic)
})
}
}
In order to support writes, you must implement the didReceiveWrite method in your CBPeripheralManagerDelegate.
Since you don't have this method, you get a "not supported" response to your write request.
I am using an an arduino feather BLE board and trying to create an iOS app that can send data to the board over BLE. I can connect but I can't get access to the txCharacteristic
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
for characteristic in characteristics {
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
writableCharacteristic = characteristic
}
// if(characteristic.uuid == CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e"))
// {
// txCharacteristic = characteristic
// }
var txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
let temp = characteristic.uuid.uuidString;
switch characteristic.uuid {
case txUUID:
txCharacteristic = characteristic;
break;
default:
break;
}
peripheral.setNotifyValue(true, for: characteristic)
}
}
This code works, but only discovers the following UUIDs:
temp String "00001532-1212-EFDE-1523-785FEABCD123"
temp String "00001531-1212-EFDE-1523-785FEABCD123"
temp String "00001534-1212-EFDE-1523-785FEABCD123"
I have figured out that these UUID's are DFU UUIDs. How can I discover the txCharacteristic instead?
Added for more information how I am calling discoverCharacteristics():
extension SimpleBluetoothIO: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else {
return
}
targetService = services.first
if let service = services.first {
targetService = service
peripheral.discoverCharacteristics(nil, for: service)
}
}
I was able to figure this out, by reading info at this website:
https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support
Particularly this part (notice that UART is the service and TX and RX are characteristics of the service):
UART Service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
RX Characteristic UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
So I needed to call peripheral.discoverServices() with the UART Service ID. In my example below self.serviceUUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
//peripheral.discoverServices(nil)
//peripheral.discoverServices([CBUUID(string: "6e400001-b5a3-f393-e0a9-e50e24dcca9e")])
// Disover peripheral with service UUID that was given to us during initialization (self.serviceUUID)
peripheral.discoverServices([CBUUID(string: self.serviceUUID)])
}
Then in didDiscoverCharacteristicsFor service I needed to look for the TX Characteristic ID:
// Did Discover Characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
for characteristic in characteristics {
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
writableCharacteristic = characteristic
}
// TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
// https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support
let txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
switch characteristic.uuid {
case txUUID:
txCharacteristic = characteristic;
break;
default:
break;
}
peripheral.setNotifyValue(true, for: characteristic)
}
}
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.
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