CoreMotionActivityManager返回Automotive或Automotive + Stationary true

我试图检测Automotive ActivtyType ,但问题是,如果“我去驾驶,然后停下车,留在车里”,只是检查coreMotion日志:我会继续得到无数的混合callback

 high Confidence: Automotive: True, Stationary: True 

要么

 high confidence: Automotive: True, Stationary: False 

要么

 low confidence: Automotive: True, Stationary: True 

我没有得到一个固定的:当然,汽车也是真实的

没有太多的模式,至less我还没有find一个模式。

问:有没有人find一种可靠的方法来检测汽车何时是真正的汽车?

我试着计算我得到的callback数,然后做一些计算,但这似乎并不可靠。

FWIW用户下车的那一刻,然后我得到一个步行或固定(没有汽车…这是好的)callback,并使用这些callback设置标志为真…所以,如果我得到任何automotivecallback…然后我知道这是一个真正的汽车…

我的代码:

 func beginMotionTracking(){ let motionLog = OSLog(subsystem: "Spike", category: "Motion") shouldUseTimer = false motionActivityManager = CMMotionActivityManager() var totalWalking = 0 var totalAutomotive = 0 var totalStationary = 0 var totalFalseAutomotive = 0 motionActivityManager?.startActivityUpdates(to: OperationQueue.main){ [weak self] activity in os_log("Motion is Tracking | desiredAccuracy is %{public}f | RemainingTime : %{public}f ",log: motionLog, type: .default, (self?.locationManager.desiredAccuracy)! , UIApplication.shared.remainingTime()) if activity?.walking == true && (activity?.confidence == .medium || activity?.confidence == .high) && activity?.automotive == false && activity?.stationary == false && activity?.unknown == false { totalWalking += 1 os_log("medium and high conf: walking %{public}d time", log: motionLog, type: .error, totalWalking) }else if activity?.stationary == true && (activity?.confidence == .medium || activity?.confidence == .high) && activity?.automotive == false && activity?.walking == false && activity?.unknown == false { totalStationary += 1 os_log("medium and high conf: stationary %{public}d time", log: motionLog, type: .error, totalStationary) // false automotive }else if activity?.automotive == true && activity?.stationary == true && (activity?.confidence == .high) && activity?.walking == false && activity?.unknown == false { totalFalseAutomotive += 1 os_log("high conf: FALSE Automotive %{public}d time", log: motionLog, type: .error, totalFalseAutomotive) if totalFalseAutomotive > 2{ totalFalseAutomotive = 0 totalAutomotive = 0 totalStationary = 0 totalWalking = 0 os_log("Too many FALSE automotives, REST all counts back to 0", log: motionLog, type: .fault) } } else if activity?.automotive == true && (activity?.confidence == .high) && activity?.walking == false && activity?.stationary == false && activity?.unknown == false { totalAutomotive += 1 os_log("high conf: Automotive %{public}d time", log: motionLog, type: .error, totalAutomotive) if ((totalWalking > 3 && totalAutomotive > 2) || (totalStationary > 3 && totalAutomotive > 2) || (totalAutomotive > 7)){ self?.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters os_log("Motion is Automotive and is about to be stopped: desired AccuracyChanged to HundredMeters | RemainingTime : %{public}f ", log: motionLog, type: .fault, UIApplication.shared.remainingTime()) self?.shouldUseTimer = true self?.motionActivityManager?.stopActivityUpdates() } } } } 

我经历了所有这些麻烦,因为我试图在用户不开车超过3分钟,然后使用核心运动来检测automotive运动并使用它来放回核心定位精度时降低精度定位精度达到百米。