Skip to content

Commit 4573673

Browse files
authored
Merge pull request #12 from SomaticLabs/upstream
Merge upstream changes
2 parents 19289da + a659717 commit 4573673

File tree

12 files changed

+220
-110
lines changed

12 files changed

+220
-110
lines changed

README.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ SwiftyBluetooth tries to address these concerns by providing a clear, closure ba
1212
- Supports Swift 3 ~> v0.2.0 and Swift 2 = v0.1.1
1313
- Synthaxic sugar and helper functions for common CoreBluetooth tasks
1414
- Closure based CBCentralManager peripheral scanning with a timeout
15-
- NSNotification based event for CBCentralManager state changes and state restoration
15+
- Notification based event for CBCentralManager state changes and state restoration
1616
- Closure based calls for every CBPeripheral operations
17-
- NSNotification based event for CBPeripheral name updates, characteristic value updates and services updates
17+
- Notification based event for CBPeripheral name updates, characteristic value updates and services updates
1818
- Precise errors and guaranteed timeout for every Bluetooth operation
1919
- [Full documentation for all public interfaces](http://cocoadocs.org/docsets/SwiftyBluetooth/)
2020

@@ -29,7 +29,7 @@ Note: The library is currently not thread safe, make sure to run your `Central`
2929
Below are a couple examples of operations that might be of interest to you.
3030

3131
### Scanning for Peripherals
32-
You can scan for peripherals by calling `scanWithTimeout(...)` while passing a `timeout` in seconds and a `callback` closure to receive `Peripheral` result callbacks as well as update on the status of your scan:
32+
Scan for peripherals by calling `scanWithTimeout(...)` while passing a `timeout` in seconds and a `callback` closure to receive `Peripheral` result callbacks as well as update on the status of your scan:
3333
```swift
3434
// You can pass in nil if you want to discover all Peripherals
3535
SwiftyBluetooth.scanForPeripherals(withServiceUUIDs: nil, timeoutAfter: 15) { scanResult in
@@ -42,8 +42,7 @@ SwiftyBluetooth.scanForPeripherals(withServiceUUIDs: nil, timeoutAfter: 15) { sc
4242
case .scanStopped(let error):
4343
// The scan stopped, an error is passed if the scan stopped unexpectedly
4444
}
45-
}
46-
45+
}
4746
```
4847
Note that the callback closure can be called multiple times, but always start and finish with a callback containing a `.scanStarted` and `.scanStopped` result respectively. Your callback will be called with a `.scanResult` for every unique peripheral found during the scan.
4948

@@ -70,7 +69,7 @@ peripheral.disconnect { result in
7069
}
7170
```
7271
### Reading from a peripheral's service's characteristic
73-
If you already know the characteristic and service UUIDs you want to read from, once you've found a peripheral you can read from it right away like this:
72+
If you already know the characteristic and service UUIDs you want to read from, once a peripheral has been found you can read from it right away like this:
7473

7574
```swift
7675
peripheral.readValue(ofCharacWithUUID: "2A29", fromServiceWithUUID: "180A") { result in
@@ -82,21 +81,21 @@ peripheral.readValue(ofCharacWithUUID: "2A29", fromServiceWithUUID: "180A") { re
8281
}
8382
}
8483
```
85-
This will connect to the peripheral if necessary and ensure the characteristic and service needed are discovered before reading from the characteristic matching `characteristicUUID`. If the charac/service cannot be retrieved you will receive an error specifying which charac/service could not be found.
84+
This will connect to the peripheral if necessary and ensure that the characteristic and service needed are discovered before reading from the characteristic matching `characteristicUUID`. If the charac/service cannot be retrieved you will receive an error specifying which charac/service could not be found.
8685

8786
If you have a reference to a `CBCharacteristic`, you can read using the characteristic directly:
8887
```swift
8988
peripheral.readValue(ofCharac: charac) { result in
9089
switch result {
9190
case .success(let data):
92-
break // The data was read and is returned as an NSData instance
91+
break // The data was read and is returned as a Data instance
9392
case .failure(let error):
9493
break // An error happened while attempting to read the data
9594
}
9695
}
9796
```
9897
### Writing to a Peripheral's service's characteristic
99-
If you already know the characteristic and service UUID you want to write to, once you've found a peripheral, you can write to that characteristic right away like this:
98+
If you already know the characteristic and service UUID you want to write to, once a peripheral has been found, you can write to that characteristic right away like this:
10099
```swift
101100
let exampleBinaryData = String(0b1010).dataUsingEncoding(NSUTF8StringEncoding)!
102101
peripheral.writeValue(ofCharacWithUUID: "1d5bc11d-e28c-4157-a7be-d8b742a013d8",
@@ -110,12 +109,17 @@ peripheral.writeValue(ofCharacWithUUID: "1d5bc11d-e28c-4157-a7be-d8b742a013d8",
110109
}
111110
}
112111
```
112+
<<<<<<< HEAD
113113
### Listening to and receiving Characteristic update notifications
114+
=======
115+
### Receiving Characteristic update notifications
116+
>>>>>>> e38dfdfb7686e99d983675988bc6228705919464
114117
Receiving characteristic value updates is done through notifications on the default `NotificationCenter`. All supported `Peripheral` notifications are part of the `PeripheralEvent` enum. Use this enum's raw values as the notification string when registering for notifications:
115118
```swift
116119
// First we prepare ourselves to receive update notifications
117120
let peripheral = somePeripheral
118121

122+
<<<<<<< HEAD
119123
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: PeripheralEvent.characteristicValueUpdate.rawValue),
120124
object: peripheral,
121125
queue: nil) { notification in
@@ -131,6 +135,20 @@ peripheral.setNotifyValue(toEnabled: true, forCharacWithUUID: "2A29", ofServiceW
131135
case .failure(let error):
132136
break // An error happened setting the characteristic to notify.
133137
}
138+
=======
139+
NotificationCenter.default.addObserver(forName: Peripheral.PeripheralCharacteristicValueUpdate,
140+
object: peripheral,
141+
queue: nil) { (notification) in
142+
let charac = notification.userInfo!["characteristic"] as! CBCharacteristic
143+
if let error = notification.userInfo?["error"] as? SBError {
144+
// Deal with error
145+
}
146+
}
147+
148+
// We can then set a characteristic's notification value to true and start receiving updates to that characteristic
149+
peripheral.setNotifyValue(toEnabled: true, forCharacWithUUID: "2A29", ofServiceWithUUID: "180A") { (isNotifying, error) in
150+
// If there were no errors, you will now receive Notifications when that characteristic value gets updated.
151+
>>>>>>> e38dfdfb7686e99d983675988bc6228705919464
134152
}
135153
```
136154
### Discovering services
@@ -160,6 +178,28 @@ peripheral.discoverCharacteristics(withUUIDs: nil, ofServiceWithUUID: "180A") {
160178
}
161179
```
162180
Like the CBPeripheral discoverCharacteristics(...) function, passing nil instead of an array of service UUIDs will discover all of this service's characteristics.
181+
### State preservation
182+
SwiftyBluetooth is backed by a CBCentralManager singleton wrapper and does not give you direct access to the underlying CBCentralManager.
183+
184+
But, you can still setup the underlying CBCentralManager for state restoration by calling `setSharedCentralInstanceWith(restoreIdentifier: )` and use the restoreIdentifier of your choice.
185+
186+
Take note that this method can only be called once and must be called before anything else in the library otherwise the Central sharedInstance will be lazily initiated the first time you access it.
187+
188+
As such, it is recommended to call it in your App Delegate's `didFinishLaunchingWithOptions(:)`
189+
```swift
190+
SwiftyBluetooth.setSharedCentralInstanceWith(restoreIdentifier: "MY_APP_BLUETOOTH_STATE_RESTORE_IDENTIFIER")
191+
```
192+
193+
Register for state preservation notifications on the default NotificationCenter. Those notifications will contain an array of restored `Peripheral`.
194+
```swift
195+
NotificationCenter.default.addObserver(forName: Central.CentralManagerWillRestoreStateNotification,
196+
object: Central.sharedInstance,
197+
queue: nil) { (notification) in
198+
if let restoredPeripherals = notification.userInfo?["peripherals"] as? [Peripheral] {
199+
200+
}
201+
}
202+
```
163203
## Installation
164204

165205

SwiftyBluetooth.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Pod::Spec.new do |s|
22
s.name = 'SwiftyBluetooth'
3-
s.version = '0.2.0'
3+
s.version = '0.4.0'
44
s.license = 'MIT'
55
s.homepage = 'https://github.com/tehjord/SwiftyBluetooth'
66
s.authors = { 'Jordan Belanger' => 'jordane.belanger@gmail.com' }
7-
s.summary = 'Swift 2/3 compatible fully featured closures based libray for CoreBluetooth on iOS 9+ devices'
7+
s.summary = 'Fully featured closures based library for CoreBluetooth'
88
s.source = { :git => 'https://github.com/tehjord/SwiftyBluetooth.git', :tag => s.version }
99
s.source_files = 'SwiftyBluetooth/Source/*.swift'
1010
s.requires_arc = true

SwiftyBluetooth.xcodeproj/project.pbxproj

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
090622BD1D11380200368A28 /* CBExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 090622BC1D11380200368A28 /* CBExtensions.swift */; };
11-
0950A66B1D2101DE00512F59 /* SwiftyBluetooth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0950A66A1D2101DE00512F59 /* SwiftyBluetooth.swift */; };
1210
096409B21CC6103D00ADD4D9 /* Supporting Files in Headers */ = {isa = PBXBuildFile; fileRef = 096409B11CC6103D00ADD4D9 /* Supporting Files */; settings = {ATTRIBUTES = (Public, ); }; };
1311
096409BD1CC610CD00ADD4D9 /* CoreBluetooth.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 096409BC1CC610CD00ADD4D9 /* CoreBluetooth.framework */; };
14-
096A03CA1D1A6FEE00CB196C /* CBUUIDPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 096A03C91D1A6FEE00CB196C /* CBUUIDPath.swift */; };
15-
09709D2E1D306ADB00E01235 /* DescriptorValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09709D2D1D306ADB00E01235 /* DescriptorValue.swift */; };
16-
0974ABA41D28C176008B1B4F /* CentralProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0974ABA31D28C176008B1B4F /* CentralProxy.swift */; };
17-
0974ABA81D28C19C008B1B4F /* PeripheralProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0974ABA71D28C19C008B1B4F /* PeripheralProxy.swift */; };
18-
09C576A91CC626C70001EFF7 /* Central.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A51CC626C70001EFF7 /* Central.swift */; };
19-
09C576AA1CC626C70001EFF7 /* SBError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A61CC626C70001EFF7 /* SBError.swift */; };
20-
09C576AB1CC626C70001EFF7 /* Peripheral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A71CC626C70001EFF7 /* Peripheral.swift */; };
21-
09C576AC1CC626C70001EFF7 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A81CC626C70001EFF7 /* Util.swift */; };
22-
09DEF3B11CC839DE00F9B4EE /* CBUUIDConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09DEF3B01CC839DE00F9B4EE /* CBUUIDConvertible.swift */; };
23-
5949336FBD625A0984FED486 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59493C943987B6C658526D67 /* Result.swift */; };
12+
40BB16B21F3D0F49005F31D9 /* SwiftyBluetooth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0950A66A1D2101DE00512F59 /* SwiftyBluetooth.swift */; };
13+
40BB16B31F3D0F49005F31D9 /* SBError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A61CC626C70001EFF7 /* SBError.swift */; };
14+
40BB16B41F3D0F49005F31D9 /* Central.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A51CC626C70001EFF7 /* Central.swift */; };
15+
40BB16B51F3D0F49005F31D9 /* CentralProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0974ABA31D28C176008B1B4F /* CentralProxy.swift */; };
16+
40BB16B61F3D0F49005F31D9 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 408B5F7C1F3A2E6D0054F435 /* Result.swift */; };
17+
40BB16B71F3D0F49005F31D9 /* Peripheral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A71CC626C70001EFF7 /* Peripheral.swift */; };
18+
40BB16B81F3D0F49005F31D9 /* PeripheralProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0974ABA71D28C19C008B1B4F /* PeripheralProxy.swift */; };
19+
40BB16B91F3D0F49005F31D9 /* DescriptorValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09709D2D1D306ADB00E01235 /* DescriptorValue.swift */; };
20+
40BB16BA1F3D0F49005F31D9 /* CBUUIDConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09DEF3B01CC839DE00F9B4EE /* CBUUIDConvertible.swift */; };
21+
40BB16BB1F3D0F49005F31D9 /* CBUUIDPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 096A03C91D1A6FEE00CB196C /* CBUUIDPath.swift */; };
22+
40BB16BC1F3D0F49005F31D9 /* CBExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 090622BC1D11380200368A28 /* CBExtensions.swift */; };
23+
40BB16BD1F3D0F49005F31D9 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C576A81CC626C70001EFF7 /* Util.swift */; };
2424
/* End PBXBuildFile section */
2525

2626
/* Begin PBXFileReference section */
@@ -43,8 +43,8 @@
4343
09D515311D2DA90A00049B59 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
4444
09DEF3B01CC839DE00F9B4EE /* CBUUIDConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CBUUIDConvertible.swift; path = Source/CBUUIDConvertible.swift; sourceTree = "<group>"; };
4545
403A5CFD1DFA11C9000D5FDE /* SwiftyBluetoothFlags.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = SwiftyBluetoothFlags.xcconfig; path = "Supporting Files/SwiftyBluetoothFlags.xcconfig"; sourceTree = "<group>"; };
46+
408B5F7C1F3A2E6D0054F435 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Source/Result.swift; sourceTree = "<group>"; };
4647
409D194F1DFA206B0095F4BC /* SwiftyBluetoothConfiguration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = SwiftyBluetoothConfiguration.xcconfig; sourceTree = "<group>"; };
47-
59493C943987B6C658526D67 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
4848
/* End PBXFileReference section */
4949

5050
/* Begin PBXFrameworksBuildPhase section */
@@ -114,13 +114,13 @@
114114
09C576A61CC626C70001EFF7 /* SBError.swift */,
115115
09C576A51CC626C70001EFF7 /* Central.swift */,
116116
0974ABA31D28C176008B1B4F /* CentralProxy.swift */,
117+
408B5F7C1F3A2E6D0054F435 /* Result.swift */,
117118
09C576A71CC626C70001EFF7 /* Peripheral.swift */,
118119
0974ABA71D28C19C008B1B4F /* PeripheralProxy.swift */,
119120
09709D2D1D306ADB00E01235 /* DescriptorValue.swift */,
120121
09DEF3B01CC839DE00F9B4EE /* CBUUIDConvertible.swift */,
121122
096A03C91D1A6FEE00CB196C /* CBUUIDPath.swift */,
122123
090622BC1D11380200368A28 /* CBExtensions.swift */,
123-
59493C943987B6C658526D67 /* Result.swift */,
124124
09C576A81CC626C70001EFF7 /* Util.swift */,
125125
);
126126
name = Source;
@@ -204,18 +204,18 @@
204204
isa = PBXSourcesBuildPhase;
205205
buildActionMask = 2147483647;
206206
files = (
207-
09C576AA1CC626C70001EFF7 /* SBError.swift in Sources */,
208-
0974ABA41D28C176008B1B4F /* CentralProxy.swift in Sources */,
209-
09C576AB1CC626C70001EFF7 /* Peripheral.swift in Sources */,
210-
09C576A91CC626C70001EFF7 /* Central.swift in Sources */,
211-
0974ABA81D28C19C008B1B4F /* PeripheralProxy.swift in Sources */,
212-
0950A66B1D2101DE00512F59 /* SwiftyBluetooth.swift in Sources */,
213-
096A03CA1D1A6FEE00CB196C /* CBUUIDPath.swift in Sources */,
214-
09709D2E1D306ADB00E01235 /* DescriptorValue.swift in Sources */,
215-
090622BD1D11380200368A28 /* CBExtensions.swift in Sources */,
216-
09DEF3B11CC839DE00F9B4EE /* CBUUIDConvertible.swift in Sources */,
217-
09C576AC1CC626C70001EFF7 /* Util.swift in Sources */,
218-
5949336FBD625A0984FED486 /* Result.swift in Sources */,
207+
40BB16B21F3D0F49005F31D9 /* SwiftyBluetooth.swift in Sources */,
208+
40BB16B31F3D0F49005F31D9 /* SBError.swift in Sources */,
209+
40BB16B41F3D0F49005F31D9 /* Central.swift in Sources */,
210+
40BB16B51F3D0F49005F31D9 /* CentralProxy.swift in Sources */,
211+
40BB16B61F3D0F49005F31D9 /* Result.swift in Sources */,
212+
40BB16B71F3D0F49005F31D9 /* Peripheral.swift in Sources */,
213+
40BB16B81F3D0F49005F31D9 /* PeripheralProxy.swift in Sources */,
214+
40BB16B91F3D0F49005F31D9 /* DescriptorValue.swift in Sources */,
215+
40BB16BA1F3D0F49005F31D9 /* CBUUIDConvertible.swift in Sources */,
216+
40BB16BB1F3D0F49005F31D9 /* CBUUIDPath.swift in Sources */,
217+
40BB16BC1F3D0F49005F31D9 /* CBExtensions.swift in Sources */,
218+
40BB16BD1F3D0F49005F31D9 /* Util.swift in Sources */,
219219
);
220220
runOnlyForDeploymentPostprocessing = 0;
221221
};
@@ -243,7 +243,7 @@
243243
CLANG_WARN_SUSPICIOUS_MOVE = YES;
244244
CLANG_WARN_UNREACHABLE_CODE = YES;
245245
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
246-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
246+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
247247
COPY_PHASE_STRIP = NO;
248248
CURRENT_PROJECT_VERSION = 1;
249249
DEBUG_INFORMATION_FORMAT = dwarf;
@@ -269,6 +269,7 @@
269269
SDKROOT = iphoneos;
270270
SWIFTYBLUETOOTH_DIRECT_ACCESS = SWIFTYBLUETOOTH_DIRECT_ACCESS;
271271
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
272+
SWIFT_VERSION = 3.0;
272273
TARGETED_DEVICE_FAMILY = "1,2";
273274
VERSIONING_SYSTEM = "apple-generic";
274275
VERSION_INFO_PREFIX = "";
@@ -296,7 +297,7 @@
296297
CLANG_WARN_SUSPICIOUS_MOVE = YES;
297298
CLANG_WARN_UNREACHABLE_CODE = YES;
298299
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
299-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
300+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
300301
COPY_PHASE_STRIP = NO;
301302
CURRENT_PROJECT_VERSION = 1;
302303
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
@@ -315,6 +316,7 @@
315316
SDKROOT = iphoneos;
316317
SWIFTYBLUETOOTH_DIRECT_ACCESS = SWIFTYBLUETOOTH_DIRECT_ACCESS;
317318
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
319+
SWIFT_VERSION = 3.0;
318320
TARGETED_DEVICE_FAMILY = "1,2";
319321
VALIDATE_PRODUCT = YES;
320322
VERSIONING_SYSTEM = "apple-generic";

SwiftyBluetooth.xcodeproj/xcshareddata/xcschemes/SwiftyBluetooth.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
ignoresPersistentStateOnLaunch = "NO"
4242
debugDocumentVersioning = "YES"
4343
debugServiceExtension = "internal"
44-
allowLocationSimulation = "YES">
44+
allowLocationSimulation = "NO">
4545
<MacroExpansion>
4646
<BuildableReference
4747
BuildableIdentifier = "primary"

0 commit comments

Comments
 (0)