Skip to content

Commit

Permalink
Receiving battery level notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwolff committed Aug 21, 2019
1 parent f7ff9a2 commit 7e78bb9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
33 changes: 25 additions & 8 deletions ParticleBluetoothiOS/Base.lproj/Main.storyboard
Expand Up @@ -19,37 +19,49 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<slider opaque="NO" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="255" translatesAutoresizingMaskIntoConstraints="NO" id="1oI-5C-LPz">
<rect key="frame" x="158" y="383" width="174" height="31"/>
<rect key="frame" x="158" y="360" width="174" height="31"/>
<connections>
<action selector="redChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="wAu-07-8US"/>
</connections>
</slider>
<slider opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="255" translatesAutoresizingMaskIntoConstraints="NO" id="UR0-Ov-g4P">
<rect key="frame" x="157" y="433" width="174" height="31"/>
<rect key="frame" x="157" y="410" width="174" height="31"/>
<connections>
<action selector="greenChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="UXQ-UN-Mal"/>
</connections>
</slider>
<slider opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="255" translatesAutoresizingMaskIntoConstraints="NO" id="WhX-RD-bIS">
<rect key="frame" x="157" y="483" width="174" height="31"/>
<rect key="frame" x="157" y="460" width="174" height="31"/>
<connections>
<action selector="blueChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="Qd8-Dh-W0a"/>
</connections>
</slider>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" preservesSuperviewLayoutMargins="YES" text="Red" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="o6r-Gj-CJR">
<rect key="frame" x="85" y="388" width="30" height="21"/>
<rect key="frame" x="85" y="365" width="30" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Green" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4gP-Y5-nuc">
<rect key="frame" x="85" y="438" width="47" height="21"/>
<rect key="frame" x="85" y="415" width="47" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Blue" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2ay-py-aOq">
<rect key="frame" x="85" y="488" width="34" height="21"/>
<rect key="frame" x="85" y="465" width="34" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Battery" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="izY-tW-o2j">
<rect key="frame" x="85" y="515" width="56" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="?%" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LRV-Wh-vNo">
<rect key="frame" x="306" y="515" width="23" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand All @@ -61,21 +73,26 @@
<constraint firstItem="UR0-Ov-g4P" firstAttribute="leading" secondItem="4gP-Y5-nuc" secondAttribute="trailing" constant="27" id="4Xt-gw-Wah"/>
<constraint firstItem="2ay-py-aOq" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="65" id="90X-iV-LJI"/>
<constraint firstItem="4gP-Y5-nuc" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="65" id="Cl7-33-7f9"/>
<constraint firstItem="o6r-Gj-CJR" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="centerYWithinMargins" constant="-65" id="KmU-Ni-1n0"/>
<constraint firstItem="LRV-Wh-vNo" firstAttribute="top" secondItem="WhX-RD-bIS" secondAttribute="bottom" constant="25" id="HQh-iY-jzf"/>
<constraint firstItem="o6r-Gj-CJR" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="centerYWithinMargins" constant="-88" id="KmU-Ni-1n0"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="LRV-Wh-vNo" secondAttribute="trailing" constant="85" id="Mkt-8J-J7h"/>
<constraint firstItem="WhX-RD-bIS" firstAttribute="leading" secondItem="2ay-py-aOq" secondAttribute="trailing" constant="40" id="PfG-01-vxo"/>
<constraint firstItem="1oI-5C-LPz" firstAttribute="leading" secondItem="o6r-Gj-CJR" secondAttribute="trailing" constant="45" id="Sxz-m1-G8O"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="WhX-RD-bIS" secondAttribute="trailing" constant="85" id="Wyc-iq-GLu"/>
<constraint firstItem="1oI-5C-LPz" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="centerYWithinMargins" constant="-70" id="aIb-IN-w21"/>
<constraint firstItem="izY-tW-o2j" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="85" id="Yd1-ST-dR0"/>
<constraint firstItem="1oI-5C-LPz" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="centerYWithinMargins" constant="-93" id="aIb-IN-w21"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="UR0-Ov-g4P" secondAttribute="trailing" constant="85" id="aQY-23-dQf"/>
<constraint firstItem="2ay-py-aOq" firstAttribute="top" secondItem="4gP-Y5-nuc" secondAttribute="bottom" constant="29" id="cPR-dI-Unt"/>
<constraint firstItem="o6r-Gj-CJR" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="65" id="fVx-2C-vFq"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="1oI-5C-LPz" secondAttribute="trailing" constant="84" id="in3-fB-u8j"/>
<constraint firstItem="WhX-RD-bIS" firstAttribute="top" secondItem="UR0-Ov-g4P" secondAttribute="bottom" constant="20" id="oht-Q1-OC4"/>
<constraint firstItem="izY-tW-o2j" firstAttribute="top" secondItem="2ay-py-aOq" secondAttribute="bottom" constant="29" id="pni-PU-x1c"/>
<constraint firstItem="UR0-Ov-g4P" firstAttribute="top" secondItem="1oI-5C-LPz" secondAttribute="bottom" constant="20" id="qy4-jW-UEa"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="batteryPercentLabel" destination="LRV-Wh-vNo" id="Fqg-BH-pFf"/>
<outlet property="blueSlider" destination="WhX-RD-bIS" id="ih2-bG-xpl"/>
<outlet property="greenSlider" destination="UR0-Ov-g4P" id="Kmt-y8-Xi8"/>
<outlet property="redSlider" destination="1oI-5C-LPz" id="Uh9-XE-c5K"/>
Expand Down
3 changes: 3 additions & 0 deletions ParticleBluetoothiOS/Models/ParticlePerihperal.swift
Expand Up @@ -22,4 +22,7 @@ class ParticlePeripheral: NSObject {
public static let greenLEDCharacteristicUUID = CBUUID.init(string: "b4250402-fb4b-4746-b2b0-93f0e61122c6")
public static let blueLEDCharacteristicUUID = CBUUID.init(string: "b4250403-fb4b-4746-b2b0-93f0e61122c6")

public static let batteryServiceUUID = CBUUID.init(string: "180f")
public static let batteryCharacteristicUUID = CBUUID.init(string: "2a19")

}
37 changes: 35 additions & 2 deletions ParticleBluetoothiOS/ViewController.swift
Expand Up @@ -15,11 +15,13 @@ class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDe
@IBOutlet weak var redSlider: UISlider!
@IBOutlet weak var greenSlider: UISlider!
@IBOutlet weak var blueSlider: UISlider!
@IBOutlet weak var batteryPercentLabel: UILabel!

// Characteristics
private var redChar: CBCharacteristic?
private var greenChar: CBCharacteristic?
private var blueChar: CBCharacteristic?
private var battChar: CBCharacteristic?

// Properties
private var centralManager: CBCentralManager!
Expand Down Expand Up @@ -67,7 +69,7 @@ class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDe
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
if peripheral == self.peripheral {
print("Connected to your Particle Board")
peripheral.discoverServices([ParticlePeripheral.particleLEDServiceUUID])
peripheral.discoverServices([ParticlePeripheral.particleLEDServiceUUID,ParticlePeripheral.batteryServiceUUID]);
}
}

Expand Down Expand Up @@ -102,12 +104,35 @@ class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDe
peripheral.discoverCharacteristics([ParticlePeripheral.redLEDCharacteristicUUID,
ParticlePeripheral.greenLEDCharacteristicUUID,
ParticlePeripheral.blueLEDCharacteristicUUID], for: service)
return
}
if( service.uuid == ParticlePeripheral.batteryServiceUUID ) {
print("Battery service found")
peripheral.discoverCharacteristics([ParticlePeripheral.batteryCharacteristicUUID], for: service)
}
}
}
}

func peripheral(_ peripheral: CBPeripheral,
didUpdateNotificationStateFor characteristic: CBCharacteristic,
error: Error?) {
print("Enabling notify ", characteristic.uuid)

if error != nil {
print("Enable notify error")
}
}

func peripheral(_ peripheral: CBPeripheral,
didUpdateValueFor characteristic: CBCharacteristic,
error: Error?) {
if( characteristic == battChar ) {
print("Battery:", characteristic.value![0])

batteryPercentLabel.text = "\(characteristic.value![0])%"
}
}

// Handling discovery of characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
Expand Down Expand Up @@ -136,6 +161,14 @@ class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDe

// Unmask blue slider
blueSlider.isEnabled = true
} else if characteristic.uuid == ParticlePeripheral.batteryCharacteristicUUID {
print("Battery characteristic found");

// Set the char
battChar = characteristic

// Subscribe to the char.
peripheral.setNotifyValue(true, for: characteristic)
}
}
}
Expand Down

0 comments on commit 7e78bb9

Please sign in to comment.