目录

SensorsController Development

SensorsController supports the processing and development of the following two scenarios:

Note: Steps 1 through 7 are intended only for connecting to a heart rate device. You can skip these steps and go directly to 8 Registering a listener to connect to a pedometer to collect the step count information.

1 Log in to your HUAWEI ID and obtain the BleController and SensorsController objects

   // Create a SensorsController object to obtain data.
    private SensorsController mSensorsController;
    // Create a BleController object to scan external Bluetooth devices.
    private BleController mBleController;
    // Create a BleDeviceInfo object to temporarily store the scanned devices.
    private BleDeviceInfo mBleDeviceInfo;
    // Create a DataCollector object to temporarily store the external Bluetooth devices.
    private DataCollector mDataCollector;

public void getSensorController(View view) {
        HiHealthOptions options = HiHealthOptions.builder().build();
        // Sign in to the HUAWEI ID.
         AuthHuaweiId signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(options);
        // Obtain the SensorsController and BleController.
        mSensorsController = HuaweiHiHealth.getSensorsController(this, signInHuaweiId);
        mBleController = HuaweiHiHealth.getBleController(this, signInHuaweiId);
}

2 Scan for available BLE devices

To scan for nearby BLE devices, use the BleController.beginBleScan method.
// Start scanning for BLE devices capable of monitoring the heart rate. Multiple data types can be passed at a time. The scanning time is set to 5 seconds.
Task<Void> response = mBleController .beginScan(Arrays.asList(DataType.DT_INSTANTANEOUS_HEART_RATE), 5, new BleScanCallback() {
      @Override
      public void onDeviceDiscover(BleDeviceInfo device) {
             // BLE devices detected during the scanning will automatically be called back to the bleDeviceInfo object. The device names will be printed in the log.
            Log.i("Sensor","Discovered device " + device.getDeviceName());+
             // Save the scanned device information.
            mBleDeviceInfo = device;
      }
      @Override
      public void onScanEnd() {
             // Callback returned when scanning is complete.
            Log.i("Sensor","onScanEnd is called ");
      }
});

3 Stop scanning Bluetooth devices

// Manually stop Bluetooth scanning.
public void stopScanning(View view) {
        mBleController.endScan(mBleCallback);
    }

4 Save the device

For the device to be used by SensorsController, use the BleController.saveDevice method to save the device.

 public void saveDevices(View view) {
        // Save the device that has been detected during the scanning.
        mBleController.saveDevice(mBleDeviceInfo);
    }

After successfully saving the device, you can obtain the data collector objects of the device via SensorsController, for example, using the SensorsController.getDataCollectors method.

5 Delete a BLE device

If a BLE device is no longer used, you can use the BleController.deleteDevice method to delete it.

    public void deleteDevices(View view) {
        // Delete the device information from the saved list.
        mBleController.deleteDevice(mBleDeviceInfo);
    }

6 List all saved devices

List all external BLE devices that have been saved.

    public void listMatchedDevices(View view) {
        Task<List<BleDeviceInfo>> bleDeviceInfoTask = mBleController.getSavedDevices();
        bleDeviceInfoTask.addOnSuccessListener(new OnSuccessListener<List<BleDeviceInfo>>() {
            @Override
            public void onSuccess(List<BleDeviceInfo> bleDeviceInfos) {
                // bleDeviceInfos contains the list of the saved devices.
                for (BleDeviceInfo bleDeviceInfo : bleDeviceInfos) {
                    // Print the device list in log.
                    Log.i("Sensor", "Matched BLE devices:" + bleDeviceInfo.getDeviceName());
                }
            }
        });
    }

7 Obtain data collectors of the device

Use the SensorsController.getDataCollectors method to obtain all saved data collectors.

public void findDataCollectors(View view) {
       // Build a DataCollectorsOptions object and pass the type of device in question (which is heart rate devices in this case).
        DataCollectorsOptions dataCollectorsOptions =
            new DataCollectorsOptions.Builder().setDataTypes(DataType.DT_INSTANTANEOUS_HEART_RATE).build();

        // Use dataSourcesRequest as a parameter to return available heart rate devices.
        mSensorsController.getDataCollectors(dataCollectorsOptions)
            .addOnSuccessListener(new OnSuccessListener<List<DataCollector>>() {
                @Override
                public void onSuccess(List<DataCollector> dataCollectors) {
                    // dataCollectors contains the returned available data collectors.
                    for (DataCollector dataCollector : dataCollectors) {
                        Log.i("Sensor", "Available data collector:" + dataCollector.getDataCollectorName());

                        // Save the heart rate data collectors for later use when registering the listener.
                        mDataCollector = dataCollector;
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {Log.i("findDataCollectors failed... ");
                }
            });
    }

8 Register a listener

To register a listener to receive raw data reported by a specified data collector, use the SensorsController.register method.

    /**
     * Create a listener object for heart rate data. The received heart rate data will be called back to onSamplePoint.
     */
    private OnSamplePointListener heartrateListener = new OnSamplePointListener() {
        @Override
        public void onSamplePoint(SamplePoint dataPoint) {
            // Print the obtained heart rate data to the log.
            Log.i("Sensor", "Heart rate received " + dataPoint.getFieldValue(Field.FIELD_BPM).toString());
        }
    };

    public void registerHeartRate(View view) {
        mSensorsController.register(
            // Build a SensorsOptions object and passing the data type, data collectors, and sampling rate. The data type is mandatory.
            new SensorOptions.Builder().setDataType(DataType.DT_INSTANTANEOUS_HEART_RATE)
                .setDataCollector(mDataCollector)
                // Set the sampling rate to 1 second.
                .setCollectionRate(1, TimeUnit.SECONDS)
                .build(),
            heartrateListener);
            }

9 Unregister a listener

If you do not need to receive real-time data, use the SensorsController.unregister method to unregister the listener.

public void unregisterHeartRate(View view) {
        // Pass the listener object to be unregistered.
        mSensorsController.unregister(heartrateListener);
        }