Skip to content

Commit a7ec67e

Browse files
committed
Merge pull request #3 from domenukk/master
It now compiles under swift 1.2. Wow you are awesome, dude.
2 parents 7ce0d35 + 5493b62 commit a7ec67e

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.xcuserdatad
3+
*.xcworkspace

BLEMingleiOS/BLEMingle.swift

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import Foundation
22
import CoreBluetooth
33

44
extension String {
5-
var length: Int {
6-
let characters = Array(self.characters)
7-
return characters.count
8-
}
9-
105
subscript (r: Range<Int>) -> String {
116
get {
127
let subStart = advance(self.startIndex, r.startIndex, self.endIndex)
@@ -20,22 +15,16 @@ extension String {
2015

2116
// make sure the cleaned up string consists solely of hex digits, and that we have even number of them
2217

23-
var error: NSError?
24-
let regex: NSRegularExpression?
25-
do {
26-
regex = try NSRegularExpression(pattern: "^[0-9a-f]*$", options: .CaseInsensitive)
27-
} catch var error1 as NSError {
28-
error = error1
29-
regex = nil
30-
}
31-
let found = regex?.firstMatchInString(trimmedString, options: [], range: NSMakeRange(0, trimmedString.length))
32-
if found == nil || found?.range.location == NSNotFound || trimmedString.length % 2 != 0 {
18+
let regex = NSRegularExpression(pattern: "^[0-9a-f]*$", options: .CaseInsensitive, error: nil)
19+
20+
let found = regex!.firstMatchInString(trimmedString, options: nil, range: NSMakeRange(0, count(trimmedString)))
21+
if found == nil || found?.range.location == NSNotFound || count(trimmedString) % 2 != 0 {
3322
return nil
3423
}
35-
24+
3625
// everything ok, so now let's build NSData
3726

38-
let data = NSMutableData(capacity: trimmedString.length / 2)
27+
let data = NSMutableData(capacity: count(trimmedString) / 2)
3928

4029
for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = index.successor().successor() {
4130
let byteString = trimmedString.substringWithRange(Range<String.Index>(start: index, end: index.successor().successor()))
@@ -117,28 +106,28 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
117106
func centralManagerDidUpdateState(central: CBCentralManager) {
118107

119108
}
120-
121-
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
109+
110+
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!){
122111

123112
delegate?.didDiscoverPeripheral(peripheral)
124-
var splitUp = split("\(advertisementData)".characters) {$0 == "\n"}.map { String($0) }
113+
var splitUp = split("\(advertisementData)") {$0 == "\n"}.map { String($0) }
125114
if (splitUp.count > 1)
126115
{
127116
var chop = splitUp[1]
128-
chop = chop[0...chop.length-2]
129-
var chopSplit = split("\(chop)".characters) {$0 == "\""}.map { String($0) }
117+
chop = chop[0...count(chop)-2]
118+
var chopSplit = split("\(chop)") {$0 == "\""}.map { String($0) }
130119

131120
if !(chopSplit.count > 1 && chopSplit[1] == "Device Information")
132121
{
133122
var hexString = chop[4...7] + chop[12...19] + chop[21...26]
134123
var datas = hexString.dataFromHexadecimalString()
135124
var string = NSString(data: datas!, encoding: NSUTF8StringEncoding) as! String
136-
if (!usedList.contains(string))
125+
if (!contains(usedList, string))
137126
{
138127
usedList.append(string)
139-
if (string.length == 9 && string[string.length-1...string.length-1] == "-")
128+
if (count(string) == 9 && string[count(string)-1...count(string)-1] == "-")
140129
{
141-
finalString = finalString + string[0...string.length-2]
130+
finalString = finalString + string[0...count(string)-2]
142131
}
143132
else
144133
{
@@ -172,7 +161,7 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
172161
}
173162

174163
for service in peripheral.services ?? [] {
175-
peripheral.discoverCharacteristics([CBUUID(string: TRANSFER_CHARACTERISTIC_UUID)], forService: service as CBService)
164+
peripheral.discoverCharacteristics([CBUUID(string: TRANSFER_CHARACTERISTIC_UUID)], forService: service as! CBService)
176165
}
177166
}
178167

@@ -181,9 +170,9 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
181170
print("didDiscoverCharacteristicsForService: \(service)")
182171

183172
for characteristic in service.characteristics ?? [] {
184-
if ((characteristic as CBCharacteristic).UUID.isEqual(CBUUID(string: TRANSFER_CHARACTERISTIC_UUID))) {
173+
if ((characteristic as! CBCharacteristic).UUID.isEqual(CBUUID(string: TRANSFER_CHARACTERISTIC_UUID))) {
185174
print("Discovered characteristic: \(characteristic)")
186-
peripheral .setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
175+
peripheral .setNotifyValue(true, forCharacteristic: characteristic as! CBCharacteristic)
187176
}
188177
}
189178
}
@@ -231,10 +220,10 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
231220

232221
func StringToUUID(hex: String) -> String
233222
{
234-
var rev = String(Array(hex.characters.reverse()))
223+
var rev = String(reverse(hex))
235224
var hexData: NSData! = rev.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
236225
rev = hexData.toHexString()
237-
while(rev.length < 32) {
226+
while(count(rev) < 32) {
238227
rev = "0" + rev;
239228
}
240229
rev = rev[0...31]
@@ -258,10 +247,10 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
258247
func sendPart() {
259248
var piece:String = datastring
260249
peripheralManager.stopAdvertising()
261-
let part:String = piece.length > 14 ? piece[0...14] + "-" : piece[0...piece.length-1] + " "
262-
if (piece.length > 15)
250+
let part:String = count(piece) > 14 ? piece[0...14] + "-" : piece[0...count(piece)-1] + " "
251+
if (count(piece) > 15)
263252
{
264-
piece = piece[15...piece.length-1]
253+
piece = piece[15...count(piece)-1]
265254
}
266255
let messageUUID = StringToUUID(part)
267256
peripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [CBUUID(string: messageUUID)]])
@@ -278,9 +267,9 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
278267
{
279268
datastring = NSString(data:dataToSend, encoding:NSUTF8StringEncoding) as! String
280269
datastring = "iPhone: " + datastring
281-
if (datastring.length > 15)
270+
if (count(datastring) > 15)
282271
{
283-
for (var i:Double = 0; i < Double(datastring.length)/15.000; i++)
272+
for (var i:Double = 0; i < Double(count(datastring))/15.000; i++)
284273
{
285274
let delay = i/10.000 * Double(NSEC_PER_SEC)
286275
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
@@ -328,7 +317,8 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
328317

329318
func transferData() {
330319
if sendingEOM {
331-
var didSend:Bool = peripheralManager.updateValue("EOM".dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: transferCharacteristic, onSubscribedCentrals: nil)
320+
321+
var didSend:Bool = peripheralManager.updateValue("EOM".dataUsingEncoding(NSUTF8StringEncoding), forCharacteristic: transferCharacteristic, onSubscribedCentrals: nil)
332322

333323
if didSend {
334324

@@ -372,7 +362,7 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
372362
if sendDataIndex >= dataToSend.length {
373363
sendingEOM = true
374364

375-
var eomSent = peripheralManager.updateValue("EOM".dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: transferCharacteristic, onSubscribedCentrals: nil)
365+
var eomSent = peripheralManager.updateValue("EOM".dataUsingEncoding(NSUTF8StringEncoding), forCharacteristic: transferCharacteristic, onSubscribedCentrals: nil)
376366

377367
if eomSent {
378368
sendingEOM = false

BLEMingleiOS/BLEMingleiOS.jar

-122 KB
Binary file not shown.

0 commit comments

Comments
 (0)