Omi Swift Library

An easy to install package to get started with the omi dev kit 1 in seconds.

Installation

  1. Open Xcode => File => New Project => Ios => App => Create project (Interface: storyboard

  1. In Xcode navigate to File → Swift Packages → Add Package Dependency…
  2. Select a project
  3. Paste the repository URL (https://github.com/BasedHardware/omi) and click Next.

If you aren’t being asked to add the package to your target, click on “add Package” again, then “Add to Target” and choose your project

  1. install Requirement

Go to “Targets => your project => Info” and add this permission:

<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app needs Bluetooth access to connect to BLE devices.</string>

Run in 2 minutes

  1. Copy this code into ViewController.swift
//
//  ViewController.swift
//  omi_demo
//
//

import UIKit
import omi_lib

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        self.lookForDevice()
    }

    func lookForDevice() {
        OmiManager.startScan { device, error in
            // connect to first found omi device
            print("starting scan")
            if let device = device {
                print("got device ", device)
                self.connectToOmiDevice(device: device)
                OmiManager.endScan()
            }
        }
    }

    func lookForSpecificDevice(device_id: String) {
        OmiManager.startScan { device, error in
            // connect to first found omi device
            if let device = device, device.id == "some_device_id" {
                print("got device ", device)
                self.connectToOmiDevice(device: device)
                OmiManager.endScan()
            }
        }
    }

    func connectToOmiDevice(device: Device) {
        OmiManager.connectToDevice(device: device)
        self.listenToLiveTranscript(device: device)
        self.reconnectIfDisconnects()
    }

    func reconnectIfDisconnects() {
        OmiManager.connectionUpdated { connected in
            if connected == false {
                self.lookForDevice()
            }
        }
    }

    func listenToLiveTranscript(device: Device) {
        OmiManager.getLiveTranscription(device: device) { transcription in
            print("transcription:", transcription ?? "no transcription")
        }
    }

    func listenToLiveAudio(device: Device) {
        OmiManager.getLiveAudio(device: device) { file_url in
            print("file_url: ", file_url?.absoluteString ?? "no url")
        }
    }
}

  1. Select your team, connect the phone (we suggest via cable), and run the project

  2. turn on omi device - the app should connect automatically

Speak. You will not see any UI on the mobile app, but you should see transcription in logs. Transcription runs locally using whisper

Other Usage

The core interface for interacting with the Omi device is the OmiManager.swift. The OmiManager abstracts things like scanning, connecting, and reading bluetooth data into a few simple function calls.

Looking for a device

import omi_lib

func lookForDevice() {
    OmiManager.startScan { device, error in
        // connect to first found omi device
        if let device = device {
            print("got device ", device)
            self.connectToOmiDevice(device: device)
            OmiManager.endScan()
        }
    }
}

func lookForSpecificDevice(device_id: String) {
    OmiManager.startScan { device, error in
        // connect to first found omi device
        if let device = device, device.id == "some_device_id" {
            print("got device ", device)
            self.connectToOmiDevice(device: device)
            OmiManager.endScan()
        }
    }
}

Connecting / Reconnecting to a device

func connectToOmiDevice(device: Device) {
    OmiManager.connectToDevice(device: device)
    self.reconnectIfDisconnects()
}

func reconnectIfDisconnects() {
    OmiManager.connectionUpdated { connected in
        if connected == false {
            self.lookForDevice()
        }
    }
}

Getting Live Data

func listenToLiveTranscript(device: Device) {
    OmiManager.getLiveTranscription(device: device) { transcription in
        print("transcription:", transcription ?? "no transcription")
    }
}

func listenToLiveAudio(device: Device) {
    OmiManager.getLiveAudio(device: device) { file_url in
        print("file_url: ", file_url?.absoluteString ?? "no url")
    }
}

Licensing

Omi’s Swift SDK is available under MIT License