Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.72 KB | None | 0 0
  1. // MARK: - CBPeripheralDelegate
  2.  
  3. extension BluetoothManager {
  4.    
  5.     func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
  6.         guard let services = peripheral.services else { return }
  7.        
  8.         for service in services {
  9.             print(service)
  10.             peripheral.discoverCharacteristics(nil, for: service)
  11.         }
  12.     }
  13.    
  14.     func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
  15.         guard let characteristics = service.characteristics else { return }
  16.        
  17.         for characteristic in characteristics {
  18.             if characteristic.properties.contains(.read) {
  19.                 // check for .read type characteristics
  20.                 print("\(characteristic.uuid): properties contains .read")
  21.                 peripheral.readValue(for: characteristic)
  22.             }
  23.             if characteristic.properties.contains(.notify) {
  24.                 // check for .notify type characteristics
  25.                 print("\(characteristic.uuid): properties contains .notify")
  26.             }
  27.         }
  28.     }
  29.    
  30.     func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
  31.         switch characteristic.uuid {
  32.             case bluetoothHM10ServiceCBUUID:
  33.                 guard let safeData = characteristic.value else { return }
  34.                 characteristicsData = safeData
  35.                 print(characteristicsData) // I HAVE 6 bytes of Data but I don't know how to decode that
  36.                 print([UInt8](characteristicsData)) // ???
  37.             default:
  38.                 print("Unhandled Characteristic UUID: \(characteristic.uuid)")
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement