Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
Like this article? We recommend

Previewing the Camera

When you subscribe to the keypresses characteristic, you would want to preview the camera on the iPhone, so that users can take a snapshot using the SensorTag.

Add the following statements in bold to the ViewController.swift file:

import UIKit
import CoreBluetooth

class ViewController: UIViewController,
    CBCentralManagerDelegate,
    CBPeripheralDelegate,
    UIImagePickerControllerDelegate,
    UINavigationControllerDelegate {

    @IBOutlet weak var btnLeft: UIButton!
    @IBOutlet weak var btnRight: UIButton!
    @IBOutlet weak var txtStatus: UILabel!

    var serviceKeyPressesUDID: CBUUID!
    var characteristicKeyPressesUDID: CBUUID!
    var connectedSensorTag: CBPeripheral!

    var centralManager: CBCentralManager!

    var picker:UIImagePickerController!

    ...

    func displayCamera() {
        picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = UIImagePickerControllerSourceType.Camera
        picker.cameraDevice = UIImagePickerControllerCameraDevice.Front
        picker.showsCameraControls = false
        self.presentViewController(picker, animated:true, completion:nil)
    }

    ...

    func peripheral(peripheral: CBPeripheral!,
        didUpdateNotificationStateForCharacteristic
        characteristic: CBCharacteristic!,
        error: NSError!) {
        if (error != nil) {
            println(
              "Error changing notification state: \(error.localizedDescription)")
        } else {
            println("Characteristic's value subscribed")
            //---show camera preview---
            self.displayCamera()
        }
    }

Deploy the application to the device again and connect the SensorTag to the device. Notice that the camera preview for the front camera is shown on the device (see Figure 8).

Figure 8

Figure 8

Previewing the front camera of the iPhone.

  • + Share This
  • 🔖 Save To Your Account