称称集成文档

2017-07-13  本文已影响38人  简爱WindMan
  1. 导入vtble库

1.1 新建一个工程,如图:

image.png

1.2 将vtble.aar文件复制到主模块(app)的lib目录下,没有lib的需要在主模块(app)根目录下新建lib文件夹(与src同级)。如图:

image.png

1.3 在app模块下的build.gradle文件里指定路径并依赖vtble库,点击"Sync Now",同步项目


image.png

1.4 确认新建项目的minSdkVersion必须大于等于18,因为vtble库中minSdkVersion=18。如果项目minSdkVersion小于18,则会报类似如下错误:

image.png

1.5 确认集成是否成功,切换项目为"project",在"External Libraries"目录下可以看到"vtble-",说明集成vtble库成功,如果在 Sync项目后未出现如上所述,"Clean Project "并"Rebuild Project"就可以。集成成功后如图:

image.png
  1. 连接设备

2.1 初始化

检测蓝牙状态

由于蓝牙打开界面有"允许"和"拒绝",所以最好调用带有回调的方法,需要自己在activity里实现:

private static final int BLE_OPEN_REQUESTCODE = 100; // 大于1
private VTDeviceManager mBleManager;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_BLUETOOTH_LE)) {
        BluetoothManager mBluetoothManager = (BluetoothManager)
                getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter.isEnabled()) {
            mBleManager.startBle(this);
        } else {
            Intent openBle = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(openBle, BLE_OPEN_REQUESTCODE);
        }
    } else {
        Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
    }
 }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == BLE_OPEN_REQUESTCODE) {
        if (resultCode == RESULT_OK) {
            if (mBluetoothAdapter.isEnabled()) {
                mBleManager.startBle(this);
            }
        } else {
            Toast.makeText(this, "Refused to open Bluetooth", Toast.LENGTH_SHORT).show();
        }
    }
}

在需要连接的界面:a.实现VTDeviceManagerListener接口;b. 注册接口回调;c.调用mVtDeviceManager.startBle(this);

   mVtDeviceManager = VTDeviceManager.getInstance();
    mVtDeviceManager.setDeviceManagerListener(this);
    mVtDeviceManager.startBle(this);

回调方法详解:

 /**
 * startBle回调
 */
@Override
public void onInited() {
    // 初始化完成,可发起连接:

}

/**
 * 发现设备回调
 *
 * @param vtDevice
 */
@Override
public void onDeviceDiscovered(VTDevice vtDevice) {
    // 可更新UI;
}

/**
 * 设备连接成功回调
 *
 * @param vtDevice
 */
@Override
public void onDeviceConnected(VTDevice vtDevice) {
    // 可更新UI;
}

/**
 * 设备断开连接回调
 *
 * @param vtDevice
 */
@Override
public void onDeviceDisconnected(VTDevice vtDevice) {

}

/**
 * 发现蓝牙服务回调
 *
 * @param vtDevice
 */
@Override
public void onDeviceServiceDiscovered(VTDevice vtDevice) {
  // 注册数据接收的回调  ((VTDeviceScale) device).setScaleDataListener(mScaleDataListener);
}

/**
 * 配对成功回调
 */
@Override
public void onDevicePaired(VTDevice vtDevice) {

}

/**
 * 扫描停止回调
 */
@Override
public void onScanStop() {

}

2.2 扫描并连接设备
修改为自己产品的类型、子类型、厂商ID

private void startBleScan() {
    VTModelIdentifier mode = new VTModelIdentifier(
            VTModelIdentifier.VT_PROTOCOL_VERSION_STANDARD,
            VTModelIdentifier.VT_DEVICE_TYPE_VSCALE, // 设备类型
            VTModelIdentifier.VT_VSCALE_FAT, // 设备子类型
            VTModelIdentifier.VT_VENDOR_VTRUMP // 厂商ID
    );
    ArrayList<VTModelIdentifier> list = new ArrayList<>();
    list.add(mode);
    mVtDeviceManager.startScan(20, list);
}

3 数据接收

具体代码如下:

private VTDeviceScale mDevice;
private VTDeviceScale.VTDeviceScaleListener mScaleDataListener = new VTDeviceScale.VTDeviceScaleListener() {
    @Override
    public void onScaleFatAvailable(ScaleInfo scaleInfo) {
        super.onScaleFatAvailable(scaleInfo);
        Log.d(TAG, "onScaleFatAvailable: ");
        // 此处不可更新UI,需要用handler更新UI
        // scaleInfo 包含各项体质指标,设置用户信息后即可收到
    }

    @Override
    public void onScaleWeightAvaible(float weight) {
        super.onScaleWeightAvaible(weight);
        // 此处不可更新UI,需要用handler更新UI
        // weight:a.如果是体重秤,weight为最终数据,即体重
        //        B.如果是体脂称,weight是第一包数据,需设置用户信息
        mDevice.setScaleUserInfo(new ScaleUserInfo()
                .setGender(ScaleUserInfo.Gender.MALE)
                .setAge(25)
                .setHeight(170));
    }
};

/**
 * 发现蓝牙服务回调
 *
 * @param vtDevice
 */
@Override
public void onDeviceServiceDiscovered(VTDevice vtDevice) {
    this.mDevice = (VTDeviceScale) vtDevice;
    ((VTDeviceScale) vtDevice).setScaleDataListener(mScaleDataListener);
}
上一篇 下一篇

猜你喜欢

热点阅读