Ver código fonte

Update documentation

master
陈福行 4 anos atrás
pai
commit
485b32e039
2 arquivos alterados com 2 adições e 729 exclusões
  1. 1
    363
      README.md
  2. 1
    366
      README_CN.md

+ 1
- 363
README.md Ver arquivo

@@ -1,366 +1,4 @@
[TOC]
# AILink SDK Instructions-Android

[aar package download address](https://github.com/elinkthings/AILinkSdkDemoAndroid/releases)

[key registered address](http://sdk.aicare.net.cn)




## Conditions of use:
1. Android SDK minimum version android4.4 (API 19).
2. The Bluetooth version used by the device needs 4.0 and above.
3. Configure java1.8
4. The project depends on the androidx library

## Import SDK


```
repositories {
flatDir {
dirs 'libs'
}
}


1. Add the JitPack repository to your build file
Add this to the root build.gradle at the end of the repository:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

2. Add dependencies
dependencies {
...
implementation 'com.github.elinkthings:AILinkSDKRepositoryAndroid:1.3.1'//Bluetooth library
implementation 'com.github.elinkthings:AILinkSDKParsingLibraryAndroid:1.2.9'//Parsing library
}

3.Configure java1.8 in gradle

android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}


You can also use the aar package dependency. Please download it to the project's libs yourself.
The download address is at the top of the document.


```

## Permission settings

```
<!--In most cases, you need to ensure that the device supports BLE.-->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true"/>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<!--Android 6.0 and above. Bluetooth scanning requires one of the following two permissions. You need to apply at run time.-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!--Optional. If your app need dfu function.-->
<uses-permission android:name="android.permission.INTERNET"/>
```

> 6.0 and above systems must locate permissions, and need to obtain permissions manually

## Start integration

> Add below AndroidManifest.xml application tag
```
<application>
...

<service android:name="com.pingwang.bluetoothlib.server.ELinkBleServer"/>

</application>

```
> > Initialize [key registered address](http://sdk.aicare.net.cn)

```
// Call in application
AILinkSDK.getInstance().init(this, key, secret);

```

> You can use the BleBeseActivity class provided in the library,
and inherit the implementation method

- Bind service:
```
ps:Bind services where you need to handle Bluetooth, get Bluetooth device objects to handle, or comprehensively handle them in one place.
private void bindService() {
if (bindIntent == null) {
bindIntent = new Intent(this, ELinkBleServer.class);
if (mFhrSCon != null)
this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
}
}

private ServiceConnection mFhrSCon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//Establish a connection with the service
mBluetoothService = ((ELinkBleServer.BluetoothBinder) service).getService();
onServiceSuccess();
}

@Override
public void onServiceDisconnected(ComponentName name) {
//Disconnect from the service
mBluetoothService = null;
onServiceErr();
}
};

```
- Set the mBluetoothService.setOnCallback ();
```
Implement OnCallbackBle interface to get search, connect, disconnect status and data
/ **
  * Bluetooth search, connection and other operating interfaces
  * /
public interface OnCallbackBle extends OnCallback {
     / **
      * Start scanning device
      * /
     default void onStartScan () ()
     / **
      * Called back every time a device is scanned
      * /
     default void onScanning (BleValueBean data) ()
     / **
      * Scan timeout (completed)
      * /
    default void onScanTimeOut () ()
     / **
      * connecting
      * /
    default void onConnecting (String mac) ()
   / **
      * Connection disconnected in UI thread
      * /
    default void onDisConnected (String mac, int code) {}

     / **
      * Successful connection (discover service), in UI thread
      * /
   default void onServicesDiscovered (String mac) {}

     / **
      * Bluetooth is turned on, triggering thread
      * /
    default void bleOpen () {}

     / **
      * Bluetooth is not turned on, triggering thread
      * /
    default void bleClose () {}
}
```
- search for mBluetoothService.scanLeDevice (long timeOut);//timeOut milliseconds
```
/ **
      * Search device
      * Scanning too often will cause scanning to fail
      * Need to ensure that the total duration of 5 scans exceeds 30s
      * @param timeOut timeout time, milliseconds (how long to search to get data, 0 means always searching)
      * /
      scanLeDevice (long timeOut)

   / **
     * Search device
     * Scanning too often will cause scanning to fail
     * Need to ensure that the total duration of 5 scans exceeds 30s
     * @param timeOut timeout time, milliseconds (how long to search to get data, 0 means always searching)
     * @param scanUUID filtered UUID (empty / null code does not filter)
     * /
     scanLeDevice (long timeOut, UUID scanUUID)

The discovered device will be returned in onScanning (BleValueBean data) in the OnCallbackBle interface
```
- connect mBluetoothService.connectDevice (String mAddress);
```
Note: It is recommended to stop searching for mBluetoothService.stopScan () before connecting,
so the connection process will be more stable After the connection is successful and the service is successfully obtained,
it will be returned in onServicesDiscovered (String mac) in the OnCallbackBle interface
```
- Disconnect
```
Disconnect all connections mBluetoothService.disconnectAll (),
since this library supports multiple connections, only the method of disconnecting the device is provided in the service

```

- Get connected device object
```
Note:If no parsing package is added, you need to create a class to inherit BaseBleDeviceData.class, and then you can get the payload data of the device through this class onNotifyData interface
BleDevice bleDevice = mBluetoothService.getBleDevice (mAddress);
The BleDevice object has all operations on this device, including operations such as disconnecting, sending instructions, receiving instructions, etc.
BleDevice.disconnect (); // Disconnect
BleDevice.sendData (SendDataBean sendDataBean) // Send instructions, the content needs to be encapsulated with SendDataBean


    / **
     * @param hex content
     * @param uuid feature uuid to operate
     * @param type operation type (1 = read, 2 = write, 3 = signal strength) {@link BleConfig}
     * @param uuidService service uuid (Generally, no setting is needed, just use the default one)
     * /
    public SendDataBean (byte [] hex, UUID uuid, int type, UUID uuidService) {
        this.hex = hex;
        this.uuid = uuid;
        this.type = type;
        if (uuidService! = null)
            this.uuidService = uuidService;
 }
Under normal circumstances, you can use a subclass of SendDataBean;
Since there is a sending queue for sending data, the SendDataBean object is not recommended to be reused to avoid data overwriting;
SendBleBean is used to interact with the Bluetooth module;
SendMcuBean is used to interact with MCU;
If there is a need for custom transparent data transmission, please inherit SendDataBean or send it using SendDataBean object.

```
## More commonly used interface introduction
- setOnBleVersionListener (OnBleVersionListener bleVersionListener) in BleDevice // device version number, unit interface
```
  public interface OnBleVersionListener {
    / **
     * BM module software and hardware version number
     * /
    default void onBmVersion (String version) ()
    / **
     * mcu supported units (all are supported by default)
     * @param list null or empty means support all
     * /
    default void onSupportUnit (List <SupportUnitBean> list) {}
}
```
- setOnMcuParameterListener (OnMcuParameterListener mcuParameterListener) in BleDevice // Power, time interface
```
public interface OnMcuParameterListener {
    / **
     * mcu battery status
     * @param status current battery status (0 = no charge, 1 = charging, 2 = full charge, 3 = charging abnormality)
     * @param battery current battery percentage (0 ~ 100), default: 0xFFFF
     * /
   default void onMcuBatteryStatus (int status, int battery) ()

    / **
     * system time
     * @param status time status (0 = invalid, 1 = valid)
     * @param times time array (year, month, day, hour, minute, second)
     * /
   default void onSysTime (int status, int [] times) {}

}
```
- setOnBleOtherDataListener (OnBleOtherDataListener onBleOtherDataListener) in BleDevice // Transparent data interface, if the data format does not conform to the protocol, this interface will return data
```
public interface OnBleOtherDataListener {

    / **
     * Transparent data
     * @param data does not support protocol transparent data
     * /
    void onNotifyOtherData (byte [] data);

}
```

## Matters needing attention

- The Bluetooth library only provides data, and analyzes some ble data. The data connected to the MCU module is not parsed.
- Please use the AILinkBleParsingAndroid library for module data analysis, which provides analysis templates for each module
- AILinkBleParsingAndroid library needs to rely on AILinkSDKRepositoryAndroid library, it is not recommended to use it alone
- The BaseBleDeviceData object is the base class object of the module device. It is recommended to inherit the implementation operation. For more details, please refer to the template in the AILinkBleParsingAndroid library.
- AILinkBleParsingAndroid library has source code provided, you can find start on github
- For more operations, please refer to the demo, you can clone this project


## [AILinkBleParsingAndroid library overview](https://elinkthings.github.io/AILinkSDKAndroidDoc/)

- [baby scale](https://elinkthings.github.io/AILinkSDKAndroidDoc/babyscale/en/index.html)
```
BabyDeviceData parsing class
BabyBleConfig directive configuration class
```
- [height gauge](https://elinkthings.github.io/AILinkSDKAndroidDoc/height/en/index.html)
```
HeightDeviceData Parsing Class
HeightBleConfig directive configuration class
```
- [sphygmomanometer](https://elinkthings.github.io/AILinkSDKAndroidDoc/sphygmomanometer/en/index.html)
```
SphyDeviceData parsing class
SphyBleConfig instruction configuration class
```
- [thermometer](https://elinkthings.github.io/AILinkSDKAndroidDoc/thermometer/en/index.html)
```
TempDeviceData parsing class
TempBleConfig instruction configuration class
```
- [forehead gun](https://elinkthings.github.io/AILinkSDKAndroidDoc/foreheadgun/en/index.html)
```
TempGunDeviceData parsing class
TempGunBleConfig instruction configuration class
```
- [TPMS (Smart Tire Pressure)](https://elinkthings.github.io/AILinkSDKAndroidDoc/tpms/en/index.html)
```
TPMS transfer board:
TpmsDeviceData Parsing Class
TpmsBleConfig directive configuration class
```

- [Body Fat Scale](https://elinkthings.github.io/AILinkSDKAndroidDoc/BodyFatScale/en/index.html)
```
BodyFatBleUtilsData Body Fat Scale Object
BodyFatDataUtil Body fat scale analysis and instruction configuration class
BodyFatRecord Body Fat Record Object (Measured Return)
McuHistoryRecordBean history object
User user information object
```

## Version History
| Version number | Update time | Author | Update information
|:----|:---|:-----|-----|
|1.2.9| 2020/04/10| xing| Modify SDK to gradle form dependency, fix known bugs
|1.3.0| 2020/05/08| xing| Fix bug that only parses 20 bytes
|1.3.1| 2020/05/08| xing| Fix disconnected bug

## FQA

- Can't scan the Bluetooth device?

1. Check whether the permissions of the App are normal. The 6.0 and above systems must locate the permissions and need to manually obtain the permissions;
2. Check whether the location service of the mobile phone is turned on, and some mobile phones may need to turn on GPS;
3. Whether ELinkBleServer is registered in AndroidManifest;
4. Whether the device is connected by other mobile phones;
5. Whether the search method is called too frequently, the scanLeDevice method needs to ensure that the total length of 5 scans exceeds 30s (different from different mobile phones, it is recommended to reduce the frequency as much as possible);
6. Restart the Bluetooth of the mobile phone and try again, some mobile phones need to restart the entire mobile phone;


## Contact Us
Shenzhen elink things Co., Ltd.

Phone: 0755-81773367

Official website: www.elinkthings.com

Email: app@elinkthings.com
http://doc.elinkthings.com/web/#/12?page_id=46

+ 1
- 366
README_CN.md Ver arquivo

@@ -1,369 +1,4 @@
[TOC]
# AILink SDK使用说明 - Android

[aar包下载地址](https://github.com/elinkthings/AILinkSdkDemoAndroid/releases)

[key注册地址](http://sdk.aicare.net.cn)




## 使用条件
1. Android SDK最低版本android4.4(API 19)。
2. 设备所使用蓝牙版本需要4.0及以上。
3. 配置java1.8
4. 项目依赖androidx库

## 导入SDK


```
repositories {
flatDir {
dirs 'libs'
}
}


1.将JitPack存储库添加到您的构建文件中
将其添加到存储库末尾的root build.gradle中:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

2.添加依赖项
dependencies {
implementation 'com.github.elinkthings:AILinkSDKRepositoryAndroid:1.3.1'//蓝牙库
implementation 'com.github.elinkthings:AILinkSDKParsingLibraryAndroid:1.2.9'//解析库
}

3.在gradle中配置java1.8
android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

也可以使用aar包依赖,请自行下载放到项目的libs中,下载地址在文档顶部



```

## 权限设置

```
<!--In most cases, you need to ensure that the device supports BLE.-->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true"/>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<!--Android 6.0 and above. Bluetooth scanning requires one of the following two permissions. You need to apply at run time.-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!--Optional. If your app need dfu function.-->
<uses-permission android:name="android.permission.INTERNET"/>
```

> 6.0及以上系统必须要定位权限,且需要手动获取权限

## 开始集成

> 首先给SDK配置key和secret,[申请地址](http://sdk.aicare.net.cn)
```
//在主项目的application中初始化
AILinkSDK.getInstance().init(this, key, secret);
```

> 在AndroidManifest.xml application标签下面增加
```
<application>
...

<service android:name="com.pingwang.bluetoothlib.server.ELinkBleServer"/>

</application>

```


- 绑定服务
> 注:可使用库中提供的BleBeseActivity类,继承实现方法,
里面有绑定服务判断权限等相关操作,详细可参考demo
```
ps:在需要处理蓝牙的地方绑定服务,拿到蓝牙设备对象来处理,也可在一个地方综合处理.

private void bindService() {
if (bindIntent == null) {
bindIntent = new Intent(this, ELinkBleServer.class);
if (mFhrSCon != null)
this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
}
}

private ServiceConnection mFhrSCon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//与服务建立连接
mBluetoothService = ((ELinkBleServer.BluetoothBinder) service).getService();
onServiceSuccess();
}

@Override
public void onServiceDisconnected(ComponentName name) {
//与服务断开连接
mBluetoothService = null;
onServiceErr();
}
};
```
- 绑定服务成功后设置监听mBluetoothService.setOnCallback();
```
实现OnCallbackBle接口可以获取搜索,连接,断开等状态和数据
/**
* 蓝牙搜索,连接等操作接口
*/
public interface OnCallbackBle extends OnCallback {
/**
* 开始扫描设备
*/
default void onStartScan(){}
/**
* 每扫描到一个设备就会回调一次
*/
default void onScanning(BleValueBean data){}
/**
* 扫描超时(完成)
*/
default void onScanTimeOut(){}
/**
* 正在连接
*/
default void onConnecting(String mac){}
/**
* 连接断开,在UI线程
*/
default void onDisConnected(String mac, int code){}

/**
* 连接成功(发现服务),在UI线程
*/
default void onServicesDiscovered(String mac){}

/**
* 已开启蓝牙,在触发线程
*/
default void bleOpen(){}

/**
* 未开启蓝牙,在触发线程
*/
default void bleClose(){}
}
```
- 搜索 mBluetoothService.scanLeDevice(long timeOut);//timeOut毫秒
```
/**
* 搜索设备
* 扫描过于频繁会导致扫描失败
* 需要保证5次扫描总时长超过30s
* @param timeOut 超时时间,毫秒(搜索多久去取数据,0代表一直搜索)
*/
scanLeDevice(long timeOut)

/**
* 搜索设备
* 扫描过于频繁会导致扫描失败
* 需要保证5次扫描总时长超过30s
* @param timeOut 超时时间,毫秒(搜索多久去取数据,0代表一直搜索)
* @param scanUUID 过滤的UUID(空/null代码不过滤)
*/
scanLeDevice(long timeOut, UUID scanUUID)

搜索到的设备会在OnCallbackBle接口中的onScanning(BleValueBean data)返回
```
- 连接mBluetoothService.connectDevice(String mAddress);
```
注:连接之前建议停止搜索mBluetoothService.stopScan(),这样连接过程会更稳定
连接成功并获取服务成功后会在OnCallbackBle接口中的onServicesDiscovered(String mac)返回
```
- 断开连接
```
mBluetoothService.disconnectAll()断开所有连接,由于此库支持多连接,
所以service中只提供断开设备的方法,可在BleDevice.disconnect();断开连接
```

- 获取连接的设备对象
```

BleDevice bleDevice = mBluetoothService.getBleDevice(mAddress);
BleDevice对象拥有对此设备的所有操作,包括断开连接,发送指令,接收指令等操作
BleDevice.disconnect();//断开连接
BleDevice.sendData(SendDataBean sendDataBean)//发送指令,内容需要用SendDataBean装载


/**
* @param hex 发送的内容
* @param uuid 需要操作的特征uuid
* @param type 操作类型(1=读,2=写,3=信号强度) {@link BleConfig}
* @param uuidService 服务uuid(一般情况下不需要设置,使用默认的即可)
*/
public SendDataBean(byte[] hex, UUID uuid, int type, UUID uuidService) {
this.hex = hex;
this.uuid = uuid;
this.type = type;
if (uuidService != null)
this.uuidService = uuidService;
}

正常情况下,使用SendDataBean的子类即可;
由于发送数据存在发送队列,SendDataBean对象不建议复用,避免数据给覆盖;
SendBleBean用于与蓝牙模块交互;
SendMcuBean用于与mcu交互;
UUID不变的情况下,自定义透传数据使用SendMcuBean对象即可;
如果有自定义透传数据同时UUID也是特殊定义的,请参考SendMcuBean对象新建一个类继承SendDataBean即可;

```

- App与设备交互
- 依赖AILinkSDKParsingLibraryAndroid解析库,解析库中提供了各个模块的数据解析和控制指令,只需要实现各模块中的接口即可拿到数据.详细请参考demo和文档.也可以自行阅读源码

- 由于解析库提供了标准的数据解析,自由度相对较低,也可以继承对应的解析类进行扩展.如果这样还不能满足需求,你可以创建一个类去继承BaseBleDeviceData.class,然后实现相关方法通过 onNotifyData 接口获取到设备的Payload 数据,接下来可以自行解析数据;


## 较常用的接口介绍
- BleDevice 中的setOnBleVersionListener(OnBleVersionListener bleVersionListener)//设备版本号,单位接口
```
public interface OnBleVersionListener {
/**
* BM 模块软、硬件版本号
*/
default void onBmVersion(String version){}
/**
* mcu 支持的单位(默认支持所有)
* @param list null或者空代表支持所有
*/
default void onSupportUnit(List<SupportUnitBean> list) {}
}
```
- BleDevice 中的setOnMcuParameterListener(OnMcuParameterListener mcuParameterListener)//电量,时间接口
```
public interface OnMcuParameterListener {
/**
* mcu电池状态
* @param status 当前电池状态(0=没充电,1=充电中,2=充满电,3=充电异常)
* @param battery 当前电量百分比(0~100),默认为:0xFFFF
*/
default void onMcuBatteryStatus(int status, int battery){}

/**
* 系统时间
* @param status 时间状态(0=无效,1=有效)
* @param times 时间数组(年,月,日,时,分,秒)
*/
default void onSysTime(int status, int[] times){}

}
```
- BleDevice 中的setOnBleOtherDataListener(OnBleOtherDataListener onBleOtherDataListener) //透传数据接口,数据格式不符合协议的才会走此接口返回数据
```
public interface OnBleOtherDataListener {

/**
* 透传数据
* @param data 不支持协议的透传数据
*/
void onNotifyOtherData(byte[] data);

}
```

## 注意事项

- 蓝牙库只提供数据,通过继承BaseBleDeviceData对象实现onNotifyData方法可以接收数据
- 数据解析请使用AILinkBleParsingAndroid 库,里面有提供各模块的解析模板
- AILinkBleParsingAndroid库需要依赖AILinkSDKRepositoryAndroid库,不可单独使用
- BaseBleDeviceData对象为模块设备的基类对象,建议继承实现操作,更多请参考AILinkBleParsingAndroid库中的模板
- AILinkBleParsingAndroid库有源码提供,可在github上start
- 更多操作请参考demo,将此项目clone下来即可



## [AILinkBleParsingAndroid库概述](https://elinkthings.github.io/AILinkSDKAndroidDoc/README_CN.html)

- [婴儿秤](https://elinkthings.github.io/AILinkSDKAndroidDoc/babyscale/zh/index.html)
```
BabyDeviceData解析类
BabyBleConfig 指令配置类
```
- [身高仪](https://elinkthings.github.io/AILinkSDKAndroidDoc/height/zh/index.html)
```
HeightDeviceData解析类
HeightBleConfig指令配置类
```
- [血压计](https://elinkthings.github.io/AILinkSDKAndroidDoc/sphygmomanometer/zh/index.html)
```
SphyDeviceData解析类
SphyBleConfig指令配置类
```
- [体温计](https://elinkthings.github.io/AILinkSDKAndroidDoc/thermometer/zh/index.html)
```
TempDeviceData解析类
TempBleConfig指令配置类
```
- [额温枪](https://elinkthings.github.io/AILinkSDKAndroidDoc/foreheadgun/zh/index.html)
```
TempGunDeviceData解析类
TempGunBleConfig指令配置类
```
- [TPMS(智能胎压)](https://elinkthings.github.io/AILinkSDKAndroidDoc/tpms/zh/index.html)
```
TPMS转接板:
TpmsDeviceData解析类
TpmsBleConfig指令配置类
```
- [体脂秤](https://elinkthings.github.io/AILinkSDKAndroidDoc/BodyFatScale/zh/index.html)
```
BodyFatBleUtilsData 体脂秤对象
BodyFatDataUtil 体脂秤解析和指令配置类
BodyFatRecord 体脂记录对象(测量返回)
McuHistoryRecordBean 历史记录对象
User 用户信息对象
```

## 版本历史
|版本号|更新时间|作者|更新信息|
|:----|:---|:-----|-----|
|1.2.9| 2020/4/10| xing| 修改SDK为gradle形式依赖,修复已知bug
|1.3.0| 2020/05/08| xing| 修复只解析20个byte的bug
|1.3.1| 2020/05/08| xing| 修复断开连接的bug

## FQA

- 扫描不到蓝牙设备?

1. 查看App权限是否正常,6.0及以上系统必须要定位权限,且需要手动获取权限;
2. 查看手机的定位服务是否开启,部分手机可能需要打开GPS;
3. ELinkBleServer是否在在AndroidManifest中注册;
4. 设备是否被其他手机连接;
5. 是否调用搜索方法太频繁, scanLeDevice方法需要保证5次扫描总时长超过30s(各别手机有差异,建议尽量减少频率);
6. 重启手机蓝牙再试试,部分手机需要整个手机重启;


## 联系我们
深圳市易连物联网有限公司

电话:0755-81773367

官网:www.elinkthings.com

邮箱:app@elinkthings.com
[http://doc.elinkthings.com/web/#/12?page_id=46

Carregando…
Cancelar
Salvar