如何使用Android Fit SDK扫描BLE设备?我收到接口不匹配错误



我遵循了示例中的代码(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)

请帮忙。谢谢

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

BleScanCallback callback = new BleScanCallback() {
    @Override
    public void onDeviceFound(BleDevice device) {
        PendingResult<Status> pendingResult =
                Fitness.BleApi.claimBleDevice(mClient, device);
    }

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

.addApi(Fitness.BLE_API)

试试这个,

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);
    }

相关内容

最新更新