Skip to content

DriveMotionClient

DriveMotion supports starting and stopping driving through method calls instead of automatic detection in manual mode. To use this feature, developers must obtain TNDriveMotionClient after initializing DriveMotion.

Get TNDriveMotionClient

1
2
3
4
5
    do {
        let driveMotionClient = try TNDriveMotionService.getDriveMotionClient()
    } catch {
        // The SDK is not initialized
    }
1
2
3
4
5
    NSError *error;
    TNDriveMotionClient *driveMotionClient = [TNDriveMotionService getDriveMotionClientWithError:&error];
    if (error) {
        // The SDK is not initialized
    }

Use TNDriveMotionClient

TNDriveMotionClient provides the entrance to start driving and stop driving manually, but TNDriveDetectionMode must be set to manual, otherwise TNDriveMotionException will be generated.

Start Drive

The startDrive() method will notify DriveMotion to create a new trip and start it. This is an asynchronous operation. When DriveMotion completes all this, it will send TNDriveStartEvent to the application via broadcast. Then the application can get the session id, trip id, start time and start location information with the SDK.

Start Drive in synchronous [Deprecated, using asynchronous instead]

1
2
3
4
5
    do {
        try driveMotionClient.startDrive()
    } catch {
        // The TNDriveDetectionMode is set to .auto or start fail
    }
1
2
3
4
5
    NSError *error;
    [driveMotionClient startDriveWithError:&error];
    if (error) {
        // The TNDriveDetectionMode is set to TNDriveDetectionModeAuto or start fail
    }

Start Drive in asynchronous

1
2
3
4
5
6
7
8
9
    let request = driveMotionClient.startDriveRequest().build()
    TNStartDriveCall().execute(request: request) { response, error in
       if let response = response {
            // Handle response
        }
        if let error = error {
            // Handle error
        }
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    TNStartDriveRequest* request = [[driveMotionClient startDriveRequest] build];
    TNStartDriveCall* call = [[TNStartDriveCall alloc]init];
    [call executeWithRequest:request callback:^(TNStartDriveResponse * _Nullable response, TNDriveMotionException * _Nullable error)  {
        if (response) {
            // Handle response
        }
        if (error) {
            // Handle error
        }
    }];

Stop Drive

The stopDrive() method will notify DriveMotion to end current trip. This is an asynchronous operation. When DriveMotion completes end, it will send TNDriveEndEvent and TNDriveAnalyzedEvent to the application via broadcast. Then the application can get the session id, trip id, end time, end location and trip valid status information with the SDK by DriveEndEvent. And driveAnalyzedEvent will contain the final trip information and score information.

Note

If the trip distance is too short or been detected as invalid trip, it will be hidden from any API calls when trip finished. The value driveEndEvent.isValidTrip will indicate the specific status of the finished trip.

Stop Drive in synchronous [Deprecated, using asynchronous instead]

1
2
3
4
5
    do {
        try driveMotionClient.stopDrive()
    } catch {
        // The TNDriveDetectionMode is set to .auto or stop fail
    }
1
2
3
4
5
    NSError *error;
    [driveMotionClient stopDriveWithError:&error];
    if (error) {
        // The TNDriveDetectionMode is set to TNDriveDetectionModeAuto or stop fail
    }

Stop Drive in asynchronous

1
2
3
4
5
6
7
8
9
    let request = driveMotionClient.stopDriveRequest().build()
    TNStopDriveCall().execute(request: request) { response, error in
       if let response = response {
            // Handle response
        }
        if let error = error {
            // Handle error
        }
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    TNStopDriveRequest* request = [[driveMotionClient stopDriveRequest] build];
    TNStopDriveCall* call = [[TNStopDriveCall alloc]init];
    [call executeWithRequest:request callback:^(TNStopDriveResponse * _Nullable response, TNDriveMotionException * _Nullable error) {
        if (response) {
            // Handle response
        }
        if (error) {
            // Handle error
        }
    }];