Skip to content

Commit cbb7fb6

Browse files
Michael O'RileyMichael O'Riley
authored andcommitted
Fixed it all up there
1 parent c2f7a63 commit cbb7fb6

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

BLEMingleiOS.xcodeproj/project.pbxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
ORGANIZATIONNAME = BitGarage;
146146
TargetAttributes = {
147147
C6B53501502AC80DD49A29CC = {
148-
DevelopmentTeam = J26D86F62R;
149148
LastSwiftMigration = 0800;
150149
};
151150
};

BLEMingleiOS/BLEMingle.swift

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ extension String {
3131

3232
let data = NSMutableData(capacity: this_max / 2)
3333

34-
for i in 0 ..< 9999 {
34+
for i in 0 ..< ((trimmedString.characters.count / 2) - 1) {
3535
let lower = i * 2
3636
let upper = lower + 2
3737
let byteString = trimmedString[lower..<upper]
38-
let num = UInt8(byteString.withCString { strtoul($0, nil, 16) })
39-
data?.append([num] as [UInt8], length: 1)
40-
if (trimmedString[lower+2..<upper+2].characters.count<2)
41-
{
42-
break;
43-
}
38+
let something = byteString.withCString { strtoul($0, nil, 16) }
39+
let num = UInt16(something)
40+
data?.append([num] as [UInt16], length: 1)
4441
}
4542

4643
return data
@@ -118,22 +115,39 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
118115

119116
}
120117

121-
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!){
118+
func hexToScalar(char: String) -> UnicodeScalar {
119+
var total = 0
120+
for scalar in char.uppercased().unicodeScalars {
121+
if !(scalar >= "A" && scalar <= "F" || scalar >= "0" && scalar <= "9") {
122+
assertionFailure("Input is wrong")
123+
}
124+
125+
if scalar >= "A" {
126+
total = 16 * total + 10 + Int(scalar.value) - 65 /* 'A' */
127+
} else {
128+
total = 16 * total + Int(scalar.value) - 48 /* '0' */
129+
}
130+
}
131+
return UnicodeScalar(total)!
132+
}
133+
134+
func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber){
122135

123136
delegate?.didDiscoverPeripheral(peripheral)
124137
let splitUp : [String] = "\(advertisementData)".components(separatedBy: "\n")
125138
if (splitUp.count > 1)
126139
{
127140
var chop = splitUp[1]
128-
var counter = chop.characters.count - 2
141+
let counter = chop.characters.count - 2
129142
chop = chop[0..<counter]
130143
let chopSplit : [String] = "\(chop)".components(separatedBy: "\"")
131144

132145
if !(chopSplit.count > 1 && chopSplit[1] == "Device Information")
133146
{
134-
var hexString = chop[4..<7] + chop[12..<19] + chop[21..<26]
135-
var datas = hexString.dataFromHexadecimalString()
136-
var string = NSString(data: datas! as Data, encoding: String.Encoding.utf8.rawValue) as String?
147+
let hexString = chop[4..<7] + chop[12..<19] + chop[21..<26]
148+
let hexArray = [hexString[0..<1], hexString[2..<3], hexString[4..<5], hexString[6..<7], hexString[8..<9], hexString[10..<11], hexString[12..<13], hexString[14..<15], hexString[16..<17]]
149+
let charArray = hexArray.map { Character(hexToScalar(char: $0)) }
150+
var string = String(charArray) as String?
137151
if (string == nil) {
138152
} else if (!usedList.contains(string!))
139153
{
@@ -156,11 +170,11 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
156170
}
157171
}
158172

159-
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
173+
func centralManager(_: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
160174

161175
}
162176

163-
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
177+
func centralManager(_: CBCentralManager, didConnect peripheral: CBPeripheral) {
164178

165179
print("Connected to peripheral: \(peripheral)")
166180

@@ -169,7 +183,7 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
169183
peripheral.discoverServices([CBUUID(string: TRANSFER_SERVICE_UUID)])
170184
}
171185

172-
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
186+
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
173187
if error != nil {
174188
return
175189
}
@@ -179,7 +193,7 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
179193
}
180194
}
181195

182-
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
196+
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
183197

184198
print("didDiscoverCharacteristicsForService: \(service)")
185199

@@ -191,25 +205,25 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
191205
}
192206
}
193207

194-
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
208+
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
195209
if error != nil {
196210
return
197211
}
198212

199-
var stringFromData = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)
213+
let stringFromData = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)
200214

201215
if (stringFromData! == "EOM") {
202216
print("Data Received: \(NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue))")
203217
data.length = 0
204-
// peripheral.setNotifyValue(false, forCharacteristic: characteristic)
205-
// centralManager.cancelPeripheralConnection(peripheral)
218+
peripheral.setNotifyValue(false, for: characteristic)
219+
centralManager.cancelPeripheralConnection(peripheral)
206220
}
207221
else {
208222
data.append(characteristic.value!)
209223
}
210224
}
211225

212-
func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
226+
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
213227
if error != nil {
214228
return
215229
}
@@ -227,7 +241,7 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
227241
}
228242
}
229243

230-
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
244+
func centralManager(_: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
231245
print("Peripheral Disconnected")
232246
discoveredPeripheral = nil
233247
}
@@ -254,7 +268,6 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
254268

255269
func sendDataToPeripheral(data: NSData) {
256270
dataToSend = data
257-
peripheralManager.stopAdvertising()
258271
startAdvertisingToPeripheral()
259272
}
260273

@@ -263,32 +276,37 @@ class BLEMingle: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate
263276
{
264277
datastring = NSString(data:dataToSend as Data, encoding:String.Encoding.utf8.rawValue) as! String
265278
datastring = "iPhone: " + datastring
266-
267-
sendMessage(message: datastring)
279+
let count = Double(datastring.characters.count)
280+
for i in 0..<Int(ceil(count / 14.0000))
281+
{
282+
let time = DispatchTime.now() + .milliseconds(100 * i)
283+
let stop = DispatchTime.now() + .milliseconds(100 * (i+1))
284+
if ((datastring.characters.count - (14 * i)) > 14)
285+
{
286+
let piece = datastring[(14 * i)..<(14 * (i + 1) - 1)] + "-"
287+
DispatchQueue.main.asyncAfter(deadline: time) {
288+
() -> Void in self.sendMessage(message: piece);
289+
}
290+
}
291+
else
292+
{
293+
let piece = datastring[(14 * i)..<(datastring.characters.count-1)]
294+
DispatchQueue.main.asyncAfter(deadline: time) {
295+
() -> Void in self.sendMessage(message: piece);
296+
}
297+
DispatchQueue.main.asyncAfter(deadline: stop) {
298+
() -> Void in self.peripheralManager.stopAdvertising();
299+
}
300+
}
301+
}
268302
}
269303
}
270304

271305
func sendMessage(message: String)
272306
{
273-
let count = message.characters.count;
274-
var part:String
307+
let messageUUID = StringToUUID(hex: message)
275308

276-
if (count > 15)
277-
{
278-
let delay = 10.000 * Double(NSEC_PER_SEC)
279-
let when = DispatchTime.now() + delay
280-
part = message[0..<14] + "-"
281-
282-
DispatchQueue.main.asyncAfter(deadline: when){
283-
() -> Void in self.sendMessage(message: message[15..<count-1]);
284-
}
285-
}
286-
else
287-
{
288-
part = message
289-
}
290-
291-
let messageUUID = StringToUUID(hex: part)
309+
peripheralManager.stopAdvertising()
292310
peripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [CBUUID(string: messageUUID)]])
293311
}
294312

BLEMingleiOS/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleVersion</key>
24-
<string>14</string>
24+
<string>16</string>
2525
<key>LSRequiresIPhoneOS</key>
2626
<true/>
2727
<key>NSBluetoothPeripheralUsageDescription</key>

0 commit comments

Comments
 (0)