-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add filter for and services #24
base: master
Are you sure you want to change the base?
Changes from all commits
5ff9e1f
c719039
093e8fd
65fb2ef
3261f06
5072286
07663d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -80,6 +80,12 @@ public protocol RxPeripheral: AnyObject { | |||||
/// The data returned will be processed by the `Preprocessor` given when registering | ||||||
/// for notifications for this `characteristic`, if one was provided. | ||||||
func notificationData(for characteristic: CBUUID) -> Observable<Data> | ||||||
|
||||||
/// Validate if service exist | ||||||
func hasService(service: CBUUID) -> Observable<Bool> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
func setIndicateDescriptor(service: CBUUID, characteristic: CBUUID, preprocessor: Preprocessor?) -> Observable<Bool> | ||||||
|
||||||
} | ||||||
|
||||||
/// Aggregates Data for the purpose of demarcation. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -59,8 +59,9 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func read(service: CBUUID, characteristic: CBUUID) -> Single<Data?> { | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("READ RxPeripheralImpl") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return connectionState | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
.flatMapLatest { (state: ConnectionState) -> Observable<([CBService], Error?)> in | ||||||||||||||||||||||||||||||||||||
guard case let .connected(peripheral) = state, | ||||||||||||||||||||||||||||||||||||
let cbPeripheral = peripheral as? CBPeripheral, | ||||||||||||||||||||||||||||||||||||
|
@@ -70,6 +71,12 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
self.peripheral.discoverServices([service]) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return self.didDiscoverServicesSubject.asObservable() | ||||||||||||||||||||||||||||||||||||
}.filter { (services: [CBService], error: Error?) -> Bool in | ||||||||||||||||||||||||||||||||||||
if (services.count == 0){ | ||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+75
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.map { (services: [CBService], error: Error?) -> (CBService?, Error?) in | ||||||||||||||||||||||||||||||||||||
// check that given service exists on the peripheral | ||||||||||||||||||||||||||||||||||||
|
@@ -94,8 +101,10 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.map { (characteristics: [CBCharacteristic], error: Error?) -> (CBCharacteristic?, Error?) in | ||||||||||||||||||||||||||||||||||||
print("READ \(#line) ",characteristics.first?.uuid.uuidString,characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
// check that given characteristic exists on the peripheral | ||||||||||||||||||||||||||||||||||||
let characteristic = characteristics.first { $0.uuid.uuidString == characteristic.uuidString } | ||||||||||||||||||||||||||||||||||||
print("READ after \(#line) ",characteristic) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return (characteristic, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingCharacteristic: CBCharacteristic?, error: Error?) -> Observable<(CBCharacteristic, Error?)> in | ||||||||||||||||||||||||||||||||||||
|
@@ -189,11 +198,14 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||
didDiscoverServicesSubject | ||||||||||||||||||||||||||||||||||||
.map { (services: [CBService], error: Error?) -> (CBService?, Error?) in | ||||||||||||||||||||||||||||||||||||
print("RFN ",services.count, services) | ||||||||||||||||||||||||||||||||||||
print("RFN ",services.first?.uuid.uuidString, service.uuidString) | ||||||||||||||||||||||||||||||||||||
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Printing is helpful for debugging purposes but we don't want to ship the library with live print statements. Please remove or add these to the logger.
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
let matchingService = services.first { $0.uuid.uuidString == service.uuidString } | ||||||||||||||||||||||||||||||||||||
return (matchingService, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingService: CBService?, error: Error?) -> Observable<([CBCharacteristic], Error?)> in | ||||||||||||||||||||||||||||||||||||
print("MATCHINGSERVICE :",matchingService) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
guard let matchingService = matchingService else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: service not found") | ||||||||||||||||||||||||||||||||||||
return Observable.error(GattError.serviceNotFound) | ||||||||||||||||||||||||||||||||||||
|
@@ -204,14 +216,34 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
return Observable.error(error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("CHARACTERISTIC TO SEARCH: ",characteristic) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
self.peripheral.discoverCharacteristics([characteristic], for: matchingService) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return self.didDiscoverCharacteristicsSubject.asObservable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.filter { (characteristics: [CBCharacteristic], error: Error?) -> Bool in | ||||||||||||||||||||||||||||||||||||
for c in characteristics{ | ||||||||||||||||||||||||||||||||||||
print(c.uuid.uuidString, characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
if(c.uuid.uuidString == characteristic.uuidString){ | ||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+224
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.map { (characteristics: [CBCharacteristic], error: Error?) -> (CBCharacteristic?, Error?) in | ||||||||||||||||||||||||||||||||||||
let characteristic = characteristics.first { $0.uuid.uuidString == characteristic.uuidString } | ||||||||||||||||||||||||||||||||||||
return (characteristic, error) | ||||||||||||||||||||||||||||||||||||
print("RFN characteristics ",characteristics.count, characteristics) | ||||||||||||||||||||||||||||||||||||
for c in characteristics { | ||||||||||||||||||||||||||||||||||||
print(c.uuid.uuidString, characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
if(c.uuid.uuidString == characteristic.uuidString){ | ||||||||||||||||||||||||||||||||||||
return (c, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
print("not found , what to do, raise an error") | ||||||||||||||||||||||||||||||||||||
return (characteristics.first,GattError.characteristicNotFound) | ||||||||||||||||||||||||||||||||||||
// print("RFN ",characteristics.first?.uuid.uuidString , characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
// let characteristic = characteristics.first { $0.uuid.uuidString == characteristic.uuidString } | ||||||||||||||||||||||||||||||||||||
// return (characteristic, error) | ||||||||||||||||||||||||||||||||||||
Comment on lines
+235
to
+246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup required
Comment on lines
+235
to
+246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.do(onNext: { [weak self] (_, error: Error?) in | ||||||||||||||||||||||||||||||||||||
if let self = self, error == nil { | ||||||||||||||||||||||||||||||||||||
|
@@ -224,6 +256,7 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingCharacteristic: CBCharacteristic?, error: Error?) -> Observable<(CBCharacteristic, Error?)> in | ||||||||||||||||||||||||||||||||||||
print("RFN \(#line)") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
guard let matchingCharacteristic = matchingCharacteristic else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: characteristic not found") | ||||||||||||||||||||||||||||||||||||
return Observable.error(GattError.characteristicNotFound) | ||||||||||||||||||||||||||||||||||||
|
@@ -235,14 +268,15 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
// let CB give an error if property isn't notify-able | ||||||||||||||||||||||||||||||||||||
self.peripheral.setNotifyValue(true, for: matchingCharacteristic) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("RFN \(#line)") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return self.didUpdateValueForCharacteristicSubject.asObservable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (_, error: Error?) -> Completable in | ||||||||||||||||||||||||||||||||||||
if let error = error { | ||||||||||||||||||||||||||||||||||||
return Observable.error(error).asCompletable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
print("RFN completed") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return Observable.empty().asCompletable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
|
@@ -253,27 +287,170 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func notificationData(for characteristic: CBUUID) -> Observable<Data> { | ||||||||||||||||||||||||||||||||||||
print("notificationData Enabled for: ",characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return didUpdateValueForCharacteristicSubject | ||||||||||||||||||||||||||||||||||||
.filter { (arg: (CBCharacteristic, Error?)) -> Bool in | ||||||||||||||||||||||||||||||||||||
let (notifyCharacteristic, error) = arg | ||||||||||||||||||||||||||||||||||||
print("notificationData \(#line): ",notifyCharacteristic.uuid.uuidString, " match char: ",characteristic.uuidString, " error: ",error) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return characteristic.uuidString == notifyCharacteristic.uuid.uuidString && error == nil | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.flatMap { [weak self] (notifyCharacteristic: CBCharacteristic, _) -> Observable<Data?> in | ||||||||||||||||||||||||||||||||||||
var processedData: Data? = nil | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("notificationData \(#line): ",notifyCharacteristic) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
guard let `self` = self, let data = notifyCharacteristic.value else { return Observable.just(nil) } | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("notificationData Data\(#line): ",data) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
self.synchronized(self.processSync) { | ||||||||||||||||||||||||||||||||||||
if let preprocessor = self.preprocessorDict[characteristic] { | ||||||||||||||||||||||||||||||||||||
processedData = preprocessor.process(data: data) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("notificationData processedData\(#line): ",processedData) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return Observable.just(processedData) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.filterNil() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func hasService(service: CBUUID) -> Observable<Bool> { | ||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||
didDiscoverServicesSubject | ||||||||||||||||||||||||||||||||||||
.filter { (services: [CBService], error: Error?) -> Bool in | ||||||||||||||||||||||||||||||||||||
if (services.count == 0){ | ||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+317
to
+321
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.map { (services: [CBService], error: Error?) -> (CBService?, Error?) in | ||||||||||||||||||||||||||||||||||||
print("RFN ",services.count, services) | ||||||||||||||||||||||||||||||||||||
print("RFN ",services.first?.uuid.uuidString, service.uuidString) | ||||||||||||||||||||||||||||||||||||
Comment on lines
+324
to
+325
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
let matchingService = services.first { $0.uuid.uuidString == service.uuidString } | ||||||||||||||||||||||||||||||||||||
return (matchingService, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingService: CBService?, error: Error?) -> Observable<Bool> in | ||||||||||||||||||||||||||||||||||||
print("MATCHINGSERVICE :",matchingService) | ||||||||||||||||||||||||||||||||||||
guard let matchingService = matchingService else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: service not found") | ||||||||||||||||||||||||||||||||||||
return Observable.just(false) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if let error = error { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: \(error.localizedDescription)") | ||||||||||||||||||||||||||||||||||||
return Observable.just(false) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return Observable.just(true) | ||||||||||||||||||||||||||||||||||||
}.do(onSubscribe: { | ||||||||||||||||||||||||||||||||||||
self.peripheral.discoverServices([service]) | ||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func setIndicateDescriptor(service: CBUUID, characteristic: CBUUID, preprocessor: Preprocessor? = nil) -> Observable<Bool>{ | ||||||||||||||||||||||||||||||||||||
print("setIndicateDescriptor") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||
didDiscoverServicesSubject | ||||||||||||||||||||||||||||||||||||
.map { (services: [CBService], error: Error?) -> (CBService?, Error?) in | ||||||||||||||||||||||||||||||||||||
let matchingService = services.first { $0.uuid.uuidString == service.uuidString } | ||||||||||||||||||||||||||||||||||||
return (matchingService, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingService: CBService?, error: Error?) -> Observable<([CBCharacteristic], Error?)> in | ||||||||||||||||||||||||||||||||||||
print("MATCHINGSERVICE SID :",matchingService) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
guard let matchingService = matchingService else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: service not found") | ||||||||||||||||||||||||||||||||||||
return Observable.error(GattError.serviceNotFound) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if let error = error { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: \(error.localizedDescription)") | ||||||||||||||||||||||||||||||||||||
return Observable.error(error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("CHARACTERISTIC TO SEARCH: ",characteristic) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
self.peripheral.discoverCharacteristics([characteristic], for: matchingService) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return self.didDiscoverCharacteristicsSubject.asObservable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.filter | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
print($0,$1) | ||||||||||||||||||||||||||||||||||||
for c in $0{ | ||||||||||||||||||||||||||||||||||||
print(c.uuid.uuidString, characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
if(c.uuid.uuidString == characteristic.uuidString){ | ||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+375
to
+385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.map {(characteristics: [CBCharacteristic], error: Error?) -> (CBCharacteristic?, Error?) in | ||||||||||||||||||||||||||||||||||||
print("RFN characteristics ",characteristics.count, characteristics) | ||||||||||||||||||||||||||||||||||||
for c in characteristics { | ||||||||||||||||||||||||||||||||||||
print(c.uuid.uuidString, characteristic.uuidString) | ||||||||||||||||||||||||||||||||||||
if(c.uuid.uuidString == characteristic.uuidString){ | ||||||||||||||||||||||||||||||||||||
return (c, error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
print("not found , what to do, raise an error") | ||||||||||||||||||||||||||||||||||||
return (characteristics.first,GattError.characteristicNotFound) | ||||||||||||||||||||||||||||||||||||
Comment on lines
+388
to
+396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.do(onNext: { [weak self] (_, error: Error?) in | ||||||||||||||||||||||||||||||||||||
if let self = self, error == nil { | ||||||||||||||||||||||||||||||||||||
// if given a preprocessor, track it with the char UUID | ||||||||||||||||||||||||||||||||||||
if let preprocessor = preprocessor { | ||||||||||||||||||||||||||||||||||||
self.synchronized(self.processSync) { | ||||||||||||||||||||||||||||||||||||
self.preprocessorDict[characteristic] = preprocessor | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (matchingCharacteristic: CBCharacteristic?, error: Error?) -> Observable<(CBCharacteristic, [CBDescriptor], Error?)> in | ||||||||||||||||||||||||||||||||||||
print("RFN \(#line)") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
guard let matchingCharacteristic = matchingCharacteristic else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: characteristic not found") | ||||||||||||||||||||||||||||||||||||
return Observable.error(GattError.characteristicNotFound) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if let error = error { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: \(error.localizedDescription)") | ||||||||||||||||||||||||||||||||||||
return Observable.error(error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
// let CB give an error if property isn't notify-able | ||||||||||||||||||||||||||||||||||||
self.peripheral.discoverDescriptors(for: matchingCharacteristic) | ||||||||||||||||||||||||||||||||||||
print("DiscoverDiscriptors \(#line)") | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
return self.didDiscoverDescriptorsForSubject.asObservable() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.take(1) | ||||||||||||||||||||||||||||||||||||
.flatMapLatest { (characteristic: CBCharacteristic?, descriptors: [CBDescriptor], error: Error?) -> Observable<Bool> in | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
guard let matchingCharacteristic = characteristic else { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: characteristic not found") | ||||||||||||||||||||||||||||||||||||
return Observable.error(GattError.characteristicNotFound) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if let error = error { | ||||||||||||||||||||||||||||||||||||
RxCBLogger.sharedInstance.log("Error: \(error.localizedDescription)") | ||||||||||||||||||||||||||||||||||||
return Observable.error(error) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
print("DescriptorsForChar: ",matchingCharacteristic.uuid.uuidString, descriptors) | ||||||||||||||||||||||||||||||||||||
print("PropertiesForChat: ",matchingCharacteristic.properties) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
Comment on lines
+437
to
+439
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
for descriptor in descriptors{ | ||||||||||||||||||||||||||||||||||||
print(descriptor) | ||||||||||||||||||||||||||||||||||||
// self.peripheral.writeValue(Data(_:[UInt8(0x20)]), for: descriptor) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+440
to
+443
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please arrange here. |
||||||||||||||||||||||||||||||||||||
self.peripheral.setNotifyValue(true, for: matchingCharacteristic) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return Observable.just(true) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
.do(onSubscribe: { | ||||||||||||||||||||||||||||||||||||
self.peripheral.discoverServices([service]) | ||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// MARK: - CBPeripheralDelegate | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) { | ||||||||||||||||||||||||||||||||||||
|
@@ -294,9 +471,26 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
didDiscoverCharacteristicsSubject.onNext(characteristicsData) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) { | ||||||||||||||||||||||||||||||||||||
let discoveredDescriptors = characteristic.descriptors ?? [] | ||||||||||||||||||||||||||||||||||||
let descriptorsData = (characteristic, discoveredDescriptors, error) | ||||||||||||||||||||||||||||||||||||
for des in discoveredDescriptors { | ||||||||||||||||||||||||||||||||||||
print("SET INDICATE FOR:SPAKA DESCRIPTOR========>\(des)") | ||||||||||||||||||||||||||||||||||||
// peripheral.writeValue(Data(_:[0x20]), for: des) //RAISE ERROR | ||||||||||||||||||||||||||||||||||||
// 'NSInternalInconsistencyException', reason: 'Client Characteristic Configuration descriptors must be configured using setNotifyValue:forCharacteristic:' | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+477
to
+482
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please arrange here. |
||||||||||||||||||||||||||||||||||||
didDiscoverDescriptorsForSubject.onNext(descriptorsData) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/// Invoked when you retrieve a characteristic’s value, or when the peripheral notifies you the value has changed | ||||||||||||||||||||||||||||||||||||
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { | ||||||||||||||||||||||||||||||||||||
let valueData = (characteristic, error) | ||||||||||||||||||||||||||||||||||||
if characteristic.value != nil { | ||||||||||||||||||||||||||||||||||||
let cmd = [UInt8](characteristic.value ?? Data(_:[0x00])) | ||||||||||||||||||||||||||||||||||||
print("didUpdateValueFor UINT8: ",cmd) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
Comment on lines
+490
to
+493
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
didUpdateValueForCharacteristicSubject.onNext(valueData) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
@@ -328,6 +522,7 @@ class RxPeripheralImpl: NSObject, RxPeripheral, CBPeripheralDelegate { | |||||||||||||||||||||||||||||||||||
private let didDiscoverCharacteristicsSubject = PublishSubject<([CBCharacteristic], Error?)>() | ||||||||||||||||||||||||||||||||||||
private let didUpdateValueForCharacteristicSubject = PublishSubject<(CBCharacteristic, Error?)>() | ||||||||||||||||||||||||||||||||||||
private let didWriteToCharacteristicSubject = PublishSubject<Error?>() | ||||||||||||||||||||||||||||||||||||
private let didDiscoverDescriptorsForSubject = PublishSubject<(CBCharacteristic, [CBDescriptor], Error?)>() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
extension GattError: LocalizedError { | ||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -243,6 +243,10 @@ public class BluetoothDetectorTypeMock: BluetoothDetectorType { | |||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
public class CBPeripheralTypeMock: CBPeripheralType { | ||||||||||||||||||||||||||||||
public func discoverDescriptors(for characteristic: CBCharacteristic) { | ||||||||||||||||||||||||||||||
print("ok") | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please arrange here. |
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private var _doneInit = false | ||||||||||||||||||||||||||||||
public init(identifier: UUID = UUID(), state: CBPeripheralState = .disconnected) { | ||||||||||||||||||||||||||||||
|
@@ -566,6 +570,12 @@ public class RxPeripheralMock: RxPeripheral { | |||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return Observable.empty() | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
public func hasService(service: CBUUID) -> Observable<Bool>{ | ||||||||||||||||||||||||||||||
return Observable.just(true) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
public func setIndicateDescriptor(service: CBUUID, characteristic: CBUUID, preprocessor: Preprocessor? = nil) -> Observable<Bool>{ | ||||||||||||||||||||||||||||||
return Observable.just(true) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+573
to
+578
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
class CBCentralManagerTypeMock: CBCentralManagerType { | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition, can you use modify this interface to use more Swift style naming, like:
func hasService(_ service: CBUUID) -> Observable<Bool>