2

我遵循示例中的代码(https://developers.google.com/fit/android/ble-sensors),但出现以下错误。

private void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
        .addApi(Fitness.API)
        .addScope(FitnessScopes.SCOPE_ACTIVITY_READ)
        .addScope(FitnessScopes.SCOPE_BODY_READ)
        .addScope(FitnessScopes.SCOPE_LOCATION_READ)
        .addConnectionCallbacks(
                new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle bundle) {
                        Log.i(TAG, "Connected!!!");
                        buildBle();  // Scan for BLE Devices.
                    }

                    @Override
                    public void onConnectionSuspended(int i) {
                        // ..
                    }
                }
        )
        .addOnConnectionFailedListener(
                new GoogleApiClient.OnConnectionFailedListener() {
                    // ..
                }
        )
        .build();
}

private void buildBle() {
    BleScanCallback callback = new BleScanCallback() {
        @Override
        public void onDeviceFound(BleDevice device) {
            ClaimBleDeviceRequest request = new ClaimBleDeviceRequest.Builder()
                .setDevice(device)
                .build();
            PendingResult<Status> pendingResult =
                    Fitness.BleApi.claimBleDevice(mClient, request);
        }
        @Override
        public void onScanStopped() {
            // ..
        }
    };

    StartBleScanRequest request = new StartBleScanRequest.Builder()
        .setDataTypes(DataTypes.HEART_RATE_BPM)
        .setBleScanCallback(callback)
        .build();

    PendingResult<Status> pendingResult =
        Fitness.BleApi.startBleScan(mClient, request);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ..
    buildFitnessClient();
}

调用buildBle()方法给出以下错误。

09-29 12:38:27.777: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:27.777: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:27.777: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:27.777: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:27.777: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:27.777: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:27.777: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:28.794: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:28.794: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:28.794: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:28.794: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:28.794: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:28.794: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:28.794: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:29.801: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:29.802: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:29.802: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:29.802: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:29.802: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:29.802: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:29.802: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:31.816: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:31.817: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:31.817: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:31.817: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:31.817: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:31.817: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:31.817: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:32.845: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:32.845: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:32.845: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:32.845: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:32.845: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:32.845: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:32.845: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:33.842: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:33.854: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:33.854: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:33.854: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:33.854: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:33.854: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:33.854: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:34.847: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:34.848: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:34.848: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:34.848: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:34.848: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:34.848: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:34.848: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:35.849: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:35.856: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:35.856: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:35.856: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:35.856: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:35.856: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:35.856: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)
09-29 12:38:36.275: W/Parcel(5900): **** enforceInterface() expected 'com.google.android.gms.fitness.ble.IBleScanCallback' but read 'com.google.android.gms.fitness.request.IBleScanCallback'
09-29 12:38:36.276: W/Binder(5900): Caught a RuntimeException from the binder stub implementation.
09-29 12:38:36.276: W/Binder(5900): java.lang.SecurityException: Binder invocation to an incorrect interface
09-29 12:38:36.276: W/Binder(5900):     at android.os.Parcel.nativeEnforceInterface(Native Method)
09-29 12:38:36.276: W/Binder(5900):     at android.os.Parcel.enforceInterface(Parcel.java:453)
09-29 12:38:36.276: W/Binder(5900):     at com.google.android.gms.fitness.ble.d$a.onTransact(Unknown Source)
09-29 12:38:36.276: W/Binder(5900):     at android.os.Binder.execTransact(Binder.java:404)

请帮忙。谢谢。

4

3 回答 3

0

您应该在 claimBleDevice 调用中使用该设备。像这样:

BleScanCallback callback = new BleScanCallback() {
    @Override
    public void onDeviceFound(BleDevice device) {

        PendingResult<Status> pendingResult =
                Fitness.BleApi.claimBleDevice(mClient, device);
    }
于 2015-02-01T17:07:40.600 回答
0

尝试将此添加到 GoogleApiClient 构建器:

.addApi(Fitness.BLE_API)
于 2015-05-11T12:38:56.490 回答
0

尝试这个 ,

private BleScanCallback callback;


 private void buildBle() {
       callback = new BleScanCallback() {
            @Override
            public void onDeviceFound(BleDevice device) {
            //    ClaimBleDeviceRequest request = new ClaimBleDeviceRequest(mClient,device);

                PendingResult<Status> pendingResult =
                        Fitness.BleApi.claimBleDevice(mClient, device);
            }
            @Override
            public void onScanStopped() {

                // ..
            }
        };

        StartBleScanRequest request = new StartBleScanRequest.Builder()
                .setDataTypes(DataType.TYPE_HEART_RATE_BPM)
                .setBleScanCallback(callback)
                .build();

        PendingResult<Status> pendingResult =
                Fitness.BleApi.startBleScan(mClient, request);

    }
于 2016-05-17T07:17:55.973 回答