Skip to content

Commit

Permalink
Merge branch 'master' into feature/services
Browse files Browse the repository at this point in the history
  • Loading branch information
decanus authored Oct 22, 2019
2 parents cb42af8 + ecc84ef commit 848365b
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions Sources/UB/Transports/CoreBluetoothTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public class CoreBluetoothTransport: NSObject {
)

private static let ubServiceUUID = CBUUID(string: "BEA3B031-76FB-4889-B3C7-000000000000")
private static let receiveCharacteristicUUID = CBUUID(string: "BEA3B031-76FB-4889-B3C7-000000000001")

private static let characteristic = CBMutableCharacteristic(
type: CoreBluetoothTransport.receiveCharacteristicUUID,
private static let receiveCharacteristic = CBMutableCharacteristic(
type: CBUUID(string: "BEA3B031-76FB-4889-B3C7-000000000001"),
properties: [.read, .writeWithoutResponse, .notify],
value: nil,
permissions: [.writeable, .readable]
Expand Down Expand Up @@ -94,7 +93,7 @@ extension CoreBluetoothTransport: Transport {
if let central = centrals[to] {
peripheralManager.updateValue(
message,
for: CoreBluetoothTransport.characteristic,
for: CoreBluetoothTransport.receiveCharacteristic,
onSubscribedCentrals: [central]
)
}
Expand All @@ -111,7 +110,7 @@ extension CoreBluetoothTransport: CBPeripheralManagerDelegate {
if peripheral.state == .poweredOn {
let service = CBMutableService(type: CoreBluetoothTransport.ubServiceUUID, primary: true)

service.characteristics = [CoreBluetoothTransport.characteristic]
service.characteristics = [CoreBluetoothTransport.receiveCharacteristic]
peripheral.add(service)

peripheral.startAdvertising([
Expand Down Expand Up @@ -186,7 +185,7 @@ extension CoreBluetoothTransport: CBCentralManagerDelegate {
extension CoreBluetoothTransport: CBPeripheralDelegate {
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices _: Error?) {
if let service = peripheral.services?.first(where: { $0.uuid == CoreBluetoothTransport.ubServiceUUID }) {
peripheral.discoverCharacteristics([CoreBluetoothTransport.receiveCharacteristicUUID], for: service)
peripheral.discoverCharacteristics([CoreBluetoothTransport.receiveCharacteristic.uuid], for: service)
}
}

Expand All @@ -201,7 +200,7 @@ extension CoreBluetoothTransport: CBPeripheralDelegate {
}

let characteristics = service.characteristics
if let char = characteristics?.first(where: { $0.uuid == CoreBluetoothTransport.receiveCharacteristicUUID }) {
if let char = characteristics?.first(where: { $0.uuid == CoreBluetoothTransport.receiveCharacteristic.uuid }) {
peripherals[id] = (peripheral, char)
peripherals[id]?.peripheral.setNotifyValue(true, for: char)
// @todo we may need to do some handshake to obtain services from a peer.
Expand All @@ -215,7 +214,7 @@ extension CoreBluetoothTransport: CBPeripheralDelegate {
}

public func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
if invalidatedServices.contains(where: { $0.uuid == CoreBluetoothTransport.ubServiceUUID }) {
if invalidatedServices.contains(where: { $0.uuid == CoreBluetoothTransport.receiveCharacteristic.uuid }) {
remove(peer: Addr(peripheral.identifier.bytes))
}
}
Expand Down

0 comments on commit 848365b

Please sign in to comment.