Commit f2fd89bd authored by Tomás Vega's avatar Tomás Vega

ios done

parent 1e5d2550
No preview for this file type
......@@ -8,10 +8,6 @@
import UIKit
import CoreBluetooth
import AVFoundation
import MediaPlayer
let storedItemsKey = "storedItems"
class ViewController: UIViewController,
CBCentralManagerDelegate,
......
......@@ -146,7 +146,7 @@ Create a function to initialize the BLE. Here you can define a lot of parameters
Bluefruit.Advertising.start(0);
}
You need to setup the connect and disconnect callbacks, which can be found in the example sketch. Create a function that adds the values you want to send in an array and emits it. In your 'loop' function, read from your sensors, process it as you wish, and then call the previously defined function to send the data via BLE.
You need to setup the connect and disconnect callbacks, which can be found in the example sketch. Create a function that adds the values you want to send in an array and emits it. Remember ints and floats are 4 bytes long in the NRF52. In your 'loop' function, read from your sensors, process it as you wish, and then call the previously defined function to send the data via BLE.
void sendData(){
int numVals = 3;
......@@ -163,4 +163,49 @@ You need to setup the connect and disconnect callbacks, which can be found in th
### Mobile
I have created a barebones iOS app that connects to the BC832 and receives the data via BLE. The file where all of this is done is the [ViewController.swift](/MPL3115A2_App/MPL3115A2_App/ViewController.swift). Hello
I created a barebones iOS app that connects to the BC832 and receives the data via BLE. The file where all of this is done is the [ViewController.swift](/MPL3115A2_App/MPL3115A2_App/ViewController.swift).
First import CoreBluetooth
import CoreBluetooth
Declare the necessary supporting managers
var manager:CBCentralManager!
var _peripheral:CBPeripheral!
var sendCharacteristic: CBCharacteristic!
var loadedService: Bool = true
Define the name of your device, which is the same as the one you used in the sketch, and the service, write, and read UUIDs. For the NRF52, the following ones are set by default.
let NAME = "MPL3115A2"
let UUID_SERVICE = CBUUID(string: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
let UUID_WRITE = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
let UUID_READ = CBUUID(string: "6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
Use all the 'centralManager' and 'peripheral' functions that I wrote in the 'ViewController'. The important function is where the data is 'didUpdateValueFor'. I use the 'getBytes' method to extract the values you sent in your sketch, each 4 bytes long.
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
// Make sure it is the peripheral we want
if characteristic.uuid == UUID_READ {
// Get bytes into string
let dataReceived = characteristic.value! as NSData
print(dataReceived)
var pressure: Float = 0
var altitude: Float = 0
var temperature: Float = 0
dataReceived.getBytes(&pressure, range: NSRange(location: 0, length: 4))
dataReceived.getBytes(&altitude, range: NSRange(location: 4, length: 4))
dataReceived.getBytes(&temperature, range: NSRange(location: 8, length: 4))
print(pressure, altitude, temperature);
presValue.text = String(format: "%.1f inches (Hg)", pressure);
altValue.text = String(format: "%.1f meters", altitude);
tempValue.text = String(format: "%.1f *C", temperature);
}
}
# Happy making!
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment