【问题标题】:Share realtime accelerometer data from WatchOS to iOS app through Bluetooth通过蓝牙将实时加速度计数据从 WatchOS 共享到 iOS 应用程序
【发布时间】:2018-02-22 10:37:15
【问题描述】:

我希望将手表中的实时加速度计和陀螺仪数据推送到相应的 iOS 应用程序,以便在收到数据时进行处理。 我可以通过哪些不同的方式来实现这一目标?

【问题讨论】:

  • 为什么这被否决了?

标签: watchkit core-bluetooth apple-watch watchos watchconnectivity


【解决方案1】:

1。传感器数据

您需要CoreMotion 才能访问加速度计和设备运动数据。

import CoreMotion    

加速度计

let motionManager = CMMotionManager()

if motionManager.isAccelerometerAvailable {
    motionManager.accelerometerUpdateInterval = 1

    motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: { (data, error) in
        if let data = data {
            let x = data.acceleration.x
            let y = data.acceleration.y
            let z = data.acceleration.z

            print("x:\(x) y:\(y) z:\(z)")
        }
    })
}

运动数据

Apple Watch 上不提供单独的陀螺仪传感器,但Motion Data 提供了更多信息。

let motionManager = CMMotionManager()

if motionManager.isDeviceMotionAvailable {      
    motionManager.deviceMotionUpdateInterval = 1

    motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: { (data, error) in
        /*
         data has many properties like: attitude, gravity, heading etc.
         explore, use what you need
        */
    })
}

好读:https://heartbeat.fritz.ai/introduction-to-apple-watchkit-with-core-motion-tracking-jumping-jacks-259ee80d1210


2。观看连接

要从 Apple Watch 应用程序向 iPhone 应用程序发送信息,您需要WatchConnectivity

好教程:https://www.natashatherobot.com/watchconnectivity-say-hello-to-wcsession/

粗略地说,是这样的:

import WatchConnectivity

//this needs to be done just once (on Apple Watch as well as iPhone)
func prepareForWatchConnectivity() {
    if (WCSession.isSupported()) {
        let session = WCSession.default
        session.delegate = self //requires `WCSessionDelegate` protocol, so implement the required delegates as well
        session.activate()
    }
}

然后您可以通过以下方式从 Apple Watch 向 iPhone App 发送消息:

//Example: Sending Accelerometer Data from Apple Watch
WCSession.default.sendMessage(["x":x,
                               "y":y,
                               "z":z],
                              replyHandler: nil)

在 iPhone 上,您执行相同的操作来设置 WatchConnectivity,但您的代理应在此处处理消息:

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
    //message received will be ["x":<value>,"y":<value>,"z":<value>] as sent from Apple Watch
}

上面的 WatchConnectivity 示例是粗略的,只是为了给出一个大致的概念。它很脏,可以大大改进。

【讨论】:

  • 当我尝试这个时,发送的值有延迟。此外,如果手表屏幕变暗,这在后台模式下不起作用。
  • 有没有一种方法可以让我从 HealthKit 数据中访问运动数据。运动数据是指加速度计,而不是步数。
  • @PrabhakarPatil 滞后是可能的。用时间戳标记数据。
  • @PrabhakarPatil 如果手表屏幕变暗,应用程序将停止运行其代码。除非您启用后台模式,否则 Apple Watch 就是这样。这也有局限性。
  • @PrabhakarPatil HealthKit 不提供运动传感器数据。它仅用于读取/写入健康数据。
【解决方案2】:

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2012-06-11
  • 2018-01-16
相关资源
最近更新 更多