USB-Meter / USB Meter / Model / BluetoothRadio.swift
Newer Older
77 lines | 2.32kb
Bogdan Timofte authored 2 months ago
1
//
2
//  BluetoothRadio.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 19/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import CoreBluetooth
10

            
11
/**
12
 Bluetooth Radio Modules
13
 # DX-BT18 (HM-10)
14
 - Documentation [DX-BT18 User Manual](https:fccid.io/2AKS8DX-BT18/User-Manual/Users-Manual-4216091)
15
 - Code [HM10 Bluetooth Serial iOS](https://github.com/hoiberg/HM10-BluetoothSerial-iOS)
16
 # PW0316
17
 - Documentation [PW0316 BLE4.0 User Manual](http://www.phangwei.com/o/PW0316_User_Manual_V2.9.pdf)
Bogdan Timofte authored a month ago
18
 # BT24-M
19
 - Seen in newer TC66C units as a transparent BLE serial module on `FFE0`
Bogdan Timofte authored 2 months ago
20
 */
21
enum BluetoothRadio : CaseIterable {
22
    case BT18
23
    case PW0316
Bogdan Timofte authored a month ago
24
    case BT24M
Bogdan Timofte authored 2 months ago
25
    case UNKNOWN
26
}
27

            
28
/**
29
 Dictionary containing services used in our communication for each radio
30
 */
31
var BluetoothRadioServicesUUIDS: [BluetoothRadio:[CBUUID]] = [
32
    .BT18 : [CBUUID(string: "FFE0")],
Bogdan Timofte authored a month ago
33
    .PW0316 : [CBUUID(string: "FFE0"), CBUUID(string: "FFE5")],
34
    .BT24M : [CBUUID(string: "FFE0")]
Bogdan Timofte authored 2 months ago
35
]
36

            
Bogdan Timofte authored 2 months ago
37
/**
38
 Dictionary containing preferred notification characteristics used in our communication.
39

            
40
 Notes:
41
 - UM-family radios are documented around an HM-10 style `FFE0` / `FFE1` path
42
 - DX-BT18 can additionally expose `FFE2` as a write-only characteristic
43
 - TC66C PW0316 uses `FFE4` for notifications
44
 */
45
var BluetoothRadioNotifyUUIDs: [BluetoothRadio:[CBUUID]] = [
46
    .BT18 : [CBUUID(string: "FFE1")],
Bogdan Timofte authored a month ago
47
    .PW0316 : [CBUUID(string: "FFE4")],
48
    .BT24M : [CBUUID(string: "FFE1")]
Bogdan Timofte authored 2 months ago
49
]
50

            
51
/**
52
 Dictionary containing preferred write characteristics used in our communication.
53

            
54
 Preference order matters:
55
 - for BT18 radios we prefer `FFE2` when available and fall back to `FFE1`
56
 - for PW0316 radios we write to `FFE9`
57
 */
58
var BluetoothRadioWriteUUIDs: [BluetoothRadio:[CBUUID]] = [
59
    .BT18 : [CBUUID(string: "FFE2"), CBUUID(string: "FFE1")],
Bogdan Timofte authored a month ago
60
    .PW0316 : [CBUUID(string: "FFE9")],
61
    .BT24M : [CBUUID(string: "FFE1")]
Bogdan Timofte authored 2 months ago
62
]
63

            
Bogdan Timofte authored 2 months ago
64
/**
65
 Returns an array containing all service UUIDs used by radios
66
 */
67
func allBluetoothRadioServices () -> [CBUUID] {
68
    var retval: [CBUUID] = []
69
    for radio in BluetoothRadio.allCases {
70
        for serviceUUID in BluetoothRadioServicesUUIDS[radio] ?? [] {
71
            if !retval.contains(serviceUUID) {
72
                retval.append(serviceUUID)
73
            }
74
        }
75
    }
76
    return retval
77
}