Skip to content

Commit c3a59d1

Browse files
authored
Merge pull request #13 from SomaticLabs/swift4
Swift4 support
2 parents 4573673 + 7eef440 commit c3a59d1

8 files changed

Lines changed: 42 additions & 61 deletions

File tree

README.md

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# SwiftyBluetooth
2-
Swift 3, fully featured closures based library for CoreBluetooth on iOS 9+ devices.
3-
4-
For Swift 2 use v0.1.1
2+
Fully featured closures based library for CoreBluetooth on iOS 9+ devices.
53

64
## Background
75
CoreBluetooth and its delegate based API can be difficult to use at time. Often times you already know the specifications of the peripheral you're about to use and simply want to read or write to predetermined characteristics.
86

97
SwiftyBluetooth tries to address these concerns by providing a clear, closure based, API for every `CBCentralManager` and `CBPeripheral` calls. Furthermore, all your calls are guaranteed to timeout in case of untraceable errors. If required, SwiftyBluetooth will also take care of connecting to peripherals and discovering the required attributes when executing read or write operations lowering the amount of work you need to do.
108

119
## Features
12-
- Supports Swift 3 ~> v0.2.0 and Swift 2 = v0.1.1
10+
- Supports Swift 4 ~> v1.0.0, for Swift 3 use v0.3.4
1311
- Synthaxic sugar and helper functions for common CoreBluetooth tasks
1412
- Closure based CBCentralManager peripheral scanning with a timeout
1513
- Notification based event for CBCentralManager state changes and state restoration
@@ -21,7 +19,7 @@ SwiftyBluetooth tries to address these concerns by providing a clear, closure ba
2119
## Usage
2220
The Library has 2 important class:
2321

24-
- The `Central` class, a Singleton wrapper around `CBCentralManager` mostly used to scan for peripherals with a closure callback.
22+
- The `Central` class, a Singleton wrapper around `CBCentralManager` used to scan for peripherals with a closure callback and restore previous sessions.
2523
- The `Peripheral` class, a wrapper around `CBPeripheral` used to call `CBPeripheral` functions with closure callbacks.
2624

2725
Note: The library is currently not thread safe, make sure to run your `Central` and `Peripheral` operations on the main thread.
@@ -57,17 +55,6 @@ peripheral.connect { result in
5755
}
5856
}
5957
```
60-
### Disconnecting from a peripheral
61-
```swift
62-
peripheral.disconnect { result in
63-
switch result {
64-
case .success:
65-
break // You are now disconnected from the peripheral
66-
case .failure(let error):
67-
break // An error happened during the disconnection
68-
}
69-
}
70-
```
7158
### Reading from a peripheral's service's characteristic
7259
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:
7360

@@ -97,10 +84,10 @@ peripheral.readValue(ofCharac: charac) { result in
9784
### Writing to a Peripheral's service's characteristic
9885
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:
9986
```swift
100-
let exampleBinaryData = String(0b1010).dataUsingEncoding(NSUTF8StringEncoding)!
87+
let data = String(0b1010).dataUsingEncoding(NSUTF8StringEncoding)!
10188
peripheral.writeValue(ofCharacWithUUID: "1d5bc11d-e28c-4157-a7be-d8b742a013d8",
10289
fromServiceWithUUID: "4011e369-5981-4dae-b686-619dc656c7ba",
103-
value: exampleBinaryData) { result in
90+
value: data) { result in
10491
switch result {
10592
case .success:
10693
break // The write was successful.
@@ -109,34 +96,13 @@ peripheral.writeValue(ofCharacWithUUID: "1d5bc11d-e28c-4157-a7be-d8b742a013d8",
10996
}
11097
}
11198
```
112-
<<<<<<< HEAD
113-
### Listening to and receiving Characteristic update notifications
114-
=======
11599
### Receiving Characteristic update notifications
116-
>>>>>>> e38dfdfb7686e99d983675988bc6228705919464
117100
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:
118101
```swift
119102
// First we prepare ourselves to receive update notifications
120103
let peripheral = somePeripheral
121104

122-
<<<<<<< HEAD
123-
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: PeripheralEvent.characteristicValueUpdate.rawValue),
124-
object: peripheral,
125-
queue: nil) { notification in
126-
let updatedCharacteristic = notification.userInfo?["characteristic"] as! CBCharacteristic
127-
var newValue = updatedCharacteristic.value
128-
}
129-
130-
// We can then set a characteristic's notification value to true and start receiving updates to that characteristic
131-
peripheral.setNotifyValue(toEnabled: true, forCharacWithUUID: "2A29", ofServiceWithUUID: "180A") { result in
132-
switch result {
133-
case .success(let isNotifying):
134-
break // You will now receive NSNotifications when that characteristic value gets updated.
135-
case .failure(let error):
136-
break // An error happened setting the characteristic to notify.
137-
}
138-
=======
139-
NotificationCenter.default.addObserver(forName: Peripheral.PeripheralCharacteristicValueUpdate,
105+
NotificationCenter.default.addObserver(forName: Peripheral.PeripheralCharacteristicValueUpdate,
140106
object: peripheral,
141107
queue: nil) { (notification) in
142108
let charac = notification.userInfo!["characteristic"] as! CBCharacteristic
@@ -148,7 +114,6 @@ NotificationCenter.default.addObserver(forName: Peripheral.PeripheralCharacteris
148114
// We can then set a characteristic's notification value to true and start receiving updates to that characteristic
149115
peripheral.setNotifyValue(toEnabled: true, forCharacWithUUID: "2A29", ofServiceWithUUID: "180A") { (isNotifying, error) in
150116
// If there were no errors, you will now receive Notifications when that characteristic value gets updated.
151-
>>>>>>> e38dfdfb7686e99d983675988bc6228705919464
152117
}
153118
```
154119
### Discovering services
@@ -223,7 +188,7 @@ $ pod install
223188
Add this to your Cartfile
224189

225190
```ogdl
226-
github "tehjord/SwiftyBluetooth"
191+
github "jordanebelanger/SwiftyBluetooth"
227192
```
228193

229194
## Requirements

SwiftyBluetooth.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Pod::Spec.new do |s|
22
s.name = 'SwiftyBluetooth'
3-
s.version = '0.4.0'
3+
s.version = '1.0.0'
44
s.license = 'MIT'
5-
s.homepage = 'https://github.com/tehjord/SwiftyBluetooth'
6-
s.authors = { 'Jordan Belanger' => '[email protected]' }
5+
s.homepage = 'https://github.com/jordanebelanger/SwiftyBluetooth'
6+
s.authors = { 'Jordane Belanger' => '[email protected]' }
77
s.summary = 'Fully featured closures based library for CoreBluetooth'
8-
s.source = { :git => 'https://github.com/tehjord/SwiftyBluetooth.git', :tag => s.version }
8+
s.source = { :git => 'https://github.com/jordanebelanger/SwiftyBluetooth.git', :tag => s.version }
99
s.source_files = 'SwiftyBluetooth/Source/*.swift'
1010
s.requires_arc = true
1111
s.ios.deployment_target = '9.0'

SwiftyBluetooth.xcodeproj/project.pbxproj

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@
164164
096409A51CC6103D00ADD4D9 /* Project object */ = {
165165
isa = PBXProject;
166166
attributes = {
167-
LastUpgradeCheck = 0810;
167+
LastUpgradeCheck = 0900;
168168
TargetAttributes = {
169169
096409AD1CC6103D00ADD4D9 = {
170170
CreatedOnToolsVersion = 7.3;
171-
LastSwiftMigration = 0800;
171+
LastSwiftMigration = 0900;
172172
};
173173
};
174174
};
@@ -232,14 +232,20 @@
232232
CLANG_CXX_LIBRARY = "libc++";
233233
CLANG_ENABLE_MODULES = YES;
234234
CLANG_ENABLE_OBJC_ARC = YES;
235+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
235236
CLANG_WARN_BOOL_CONVERSION = YES;
237+
CLANG_WARN_COMMA = YES;
236238
CLANG_WARN_CONSTANT_CONVERSION = YES;
237239
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
238240
CLANG_WARN_EMPTY_BODY = YES;
239241
CLANG_WARN_ENUM_CONVERSION = YES;
240242
CLANG_WARN_INFINITE_RECURSION = YES;
241243
CLANG_WARN_INT_CONVERSION = YES;
244+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
245+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
242246
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
247+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
248+
CLANG_WARN_STRICT_PROTOTYPES = YES;
243249
CLANG_WARN_SUSPICIOUS_MOVE = YES;
244250
CLANG_WARN_UNREACHABLE_CODE = YES;
245251
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -286,14 +292,20 @@
286292
CLANG_CXX_LIBRARY = "libc++";
287293
CLANG_ENABLE_MODULES = YES;
288294
CLANG_ENABLE_OBJC_ARC = YES;
295+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
289296
CLANG_WARN_BOOL_CONVERSION = YES;
297+
CLANG_WARN_COMMA = YES;
290298
CLANG_WARN_CONSTANT_CONVERSION = YES;
291299
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
292300
CLANG_WARN_EMPTY_BODY = YES;
293301
CLANG_WARN_ENUM_CONVERSION = YES;
294302
CLANG_WARN_INFINITE_RECURSION = YES;
295303
CLANG_WARN_INT_CONVERSION = YES;
304+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
305+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
296306
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
307+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
308+
CLANG_WARN_STRICT_PROTOTYPES = YES;
297309
CLANG_WARN_SUSPICIOUS_MOVE = YES;
298310
CLANG_WARN_UNREACHABLE_CODE = YES;
299311
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -342,7 +354,8 @@
342354
SKIP_INSTALL = YES;
343355
SWIFTYBLUETOOTH_DIRECT_ACCESS = SWIFTYBLUETOOTH_DIRECT_ACCESS;
344356
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
345-
SWIFT_VERSION = 3.0;
357+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
358+
SWIFT_VERSION = 4.0;
346359
};
347360
name = Debug;
348361
};
@@ -363,7 +376,8 @@
363376
PRODUCT_NAME = "$(TARGET_NAME)";
364377
SKIP_INSTALL = YES;
365378
SWIFTYBLUETOOTH_DIRECT_ACCESS = SWIFTYBLUETOOTH_DIRECT_ACCESS;
366-
SWIFT_VERSION = 3.0;
379+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
380+
SWIFT_VERSION = 4.0;
367381
};
368382
name = Release;
369383
};

SwiftyBluetooth.xcodeproj/xcshareddata/xcschemes/SwiftyBluetooth.xcscheme

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0810"
3+
LastUpgradeVersion = "0900"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,6 +26,7 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
language = ""
2930
shouldUseLaunchSchemeArgsEnv = "YES">
3031
<Testables>
3132
</Testables>
@@ -36,6 +37,7 @@
3637
buildConfiguration = "Debug"
3738
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
3839
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
40+
language = ""
3941
launchStyle = "0"
4042
useCustomWorkingDirectory = "NO"
4143
ignoresPersistentStateOnLaunch = "NO"

SwiftyBluetooth/Source/CentralProxy.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private final class ConnectPeripheralRequest {
172172
if let error = error {
173173
return .failure(error)
174174
} else {
175-
return .success()
175+
return .success(())
176176
}
177177
}()
178178
for callback in callbacks {
@@ -192,7 +192,7 @@ extension CentralProxy {
192192
let uuid = peripheral.identifier
193193

194194
if let cbPeripheral = self.centralManager.retrievePeripherals(withIdentifiers: [uuid]).first , cbPeripheral.state == .connected {
195-
callback(.success())
195+
callback(.success(()))
196196
return
197197
}
198198

@@ -250,7 +250,7 @@ private final class DisconnectPeripheralRequest {
250250
if let error = error {
251251
return .failure(error)
252252
} else {
253-
return .success()
253+
return .success(())
254254
}
255255
}()
256256
for callback in callbacks {
@@ -272,7 +272,7 @@ extension CentralProxy {
272272

273273
if let cbPeripheral = self.centralManager.retrievePeripherals(withIdentifiers: [uuid]).first,
274274
(cbPeripheral.state == .disconnected || cbPeripheral.state == .disconnecting) {
275-
callback(.success())
275+
callback(.success(()))
276276
return
277277
}
278278

@@ -383,7 +383,7 @@ extension CentralProxy: CBCentralManagerDelegate {
383383

384384
let peripheral = Peripheral(peripheral: peripheral)
385385

386-
var rssiOptional: Int? = Int(RSSI)
386+
var rssiOptional: Int? = Int(truncating: RSSI)
387387
if let rssi = rssiOptional, rssi == 127 {
388388
rssiOptional = nil
389389
}

SwiftyBluetooth/Source/PeripheralProxy.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ extension PeripheralProxy {
742742
self.writeCharacteristicValueRequests[writePath] = nil
743743
}
744744

745-
request.callback(.success())
745+
request.callback(.success(()))
746746

747747
self.runWriteCharacteristicValueRequest(writePath)
748748
}
@@ -1141,7 +1141,7 @@ extension PeripheralProxy: CBPeripheralDelegate {
11411141
if let error = error {
11421142
request.callback(.failure(error))
11431143
} else {
1144-
request.callback(.success())
1144+
request.callback(.success(()))
11451145
}
11461146
}
11471147

@@ -1215,7 +1215,7 @@ extension PeripheralProxy: CBPeripheralDelegate {
12151215
if let error = error {
12161216
request.callback(.failure(error))
12171217
} else {
1218-
request.callback(.success())
1218+
request.callback(.success(()))
12191219
}
12201220
}
12211221
}

SwiftyBluetooth/Source/Util.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ extension UInt16 {
4545
return nil
4646
}
4747

48-
self = UInt16(truncatingBitPattern: numberValue.intValue)
48+
self = UInt16(truncatingIfNeeded: numberValue.intValue)
4949
}
5050
}

SwiftyBluetooth/Supporting Files/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.4.0</string>
18+
<string>1.0.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

0 commit comments

Comments
 (0)