Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ficat committed May 2, 2020
1 parent 04fb690 commit 6aad2fe
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 62 deletions.
88 changes: 55 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ allprojects {
}
dependencies {
implementation 'com.github.Ficat:EasyBle:v1.0.4'
implementation 'com.github.Ficat:EasyBle:v2.0.1'
}
```

Expand All @@ -37,25 +37,28 @@ If you want to turn on bluetooth, I strongly recommend you call enableBluetooth(
```

### Step
### 1.Get ble manager
### 1.Get ble manager and initialization

```java

//Get ble manager
BleManager bleManager = BleManager.getInstance(this.getApplication());

//Set options, you can call this method many times, this framework will use
//the newest options. For example,you have set scan period(like 10s), but next
//scan you wanna scan period longer(like 15s), so you can call this method again
//to set scan period is 15s
BleManager.Options options = new BleManager.Options();
options.loggable = true; //does it print log?
options.connectTimeout = 10000; //connection time out
options.scanPeriod = 12000; //scan period
options.scanDeviceName = "deviceName";
options.scanDeviceAddress = "deviceAddress";//like "DD:0D:30:00:0D:9B"
options.scanServiceUuids = serviceUuidArray;
bleManager.option(options);
//scan/connection options is not necessary, if you don't set,
//it will use default config
BleManager.ScanOptions scanOptions = BleManager.ScanOptions
.newInstance()
.scanPeriod(10000)
.scanDeviceName(null);

BleManager.ConnectOptions connectOptions = BleManager.ConnectOptions
.newInstance()
.connectTimeout(12000);


BleManager bleManager = BleManager
.getInstance()
.setScanOptions(scanOptions)//it is not necessary
.setConnectionOptions(connectOptions)//like scan options
.setLog(true, "TAG")
.init(this.getApplication());//Context is needed here,do not use Activity,which can cause Activity leak

```

Expand Down Expand Up @@ -85,6 +88,9 @@ If sdk version >=23, scanning ble must have location permissions
}
});

//start scan with specified scanOptions
bleManager.startScan(scanOptions, bleScanCallback);

```

Once target remote device has been discovered you can use stopScan() to stop scanning
Expand All @@ -108,26 +114,34 @@ You can connect to remote device by device address or BleDevice object
}

@Override
public void onTimeout(BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {
if(failCode == BleConnectCallback.FAIL_CONNECT_TIMEOUT){
//connection timeout
}else{
//connection fail due to other reasons
}

}

@Override
public void onConnected(BleDevice device) {
@Override
public void onConnected(BleDevice device) {

}
}

@Override
public void onDisconnected(BleDevice device) {
@Override
public void onDisconnected(String info, int status, BleDevice device) {

}
}
};

//connect to the remote device by BleDevice object
bleManager.connect(bleDevice, bleConnectCallback);
//connect with specified connectOptions
bleManager.connect(bleDevice, connectOptions, bleConnectCallback);

//connect with mac address
bleManager.connect(address, bleConnectCallback);
bleManager.connect(address, connectOptions, bleConnectCallback);

//connect to the remote device by address
bleManager.connect(address, bleConnectCallback)
```

Use one of the following methods to disconnect from remote device
Expand All @@ -139,13 +153,13 @@ Use one of the following methods to disconnect from remote device
//disconnect from the specific remote device by address
bleManager.disconnect(address);

//disconenct all connected devices
//disconnect all connected devices
bleManager.disconnectAll();
```


### 4.Notify
Both notification and indication use the following method to set notfication or indication
Both notification and indication use the following method to set notification or indication
```java
bleManager.notify(bleDevice, serviceUuid, notifyUuid, new BleNotifyCallback() {
@Override
Expand All @@ -159,8 +173,16 @@ Both notification and indication use the following method to set notfication or
}

@Override
public void onFail(int failCode, String info, BleDevice device) {

public void onFailure(int failCode, String info, BleDevice device) {
switch (failCode) {
case BleCallback.FAIL_DISCONNECTED://connection has disconnected
break;
case BleCallback.FAIL_OTHER://other reason
break;
default:
break;
}

}
});
```
Expand All @@ -178,7 +200,7 @@ When you want to cancel notification or indication, you can call cancelNotify()
}

@Override
public void onFail(int failCode, String info, BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {

}
});
Expand All @@ -193,7 +215,7 @@ if the length of the data you wanna deliver to remote device is larger than MTU(
}

@Override
public void onFail(int failCode, String info, BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {

}
});
Expand Down
71 changes: 47 additions & 24 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allprojects {
dependencies {
implementation 'com.github.Ficat:EasyBle:v1.0.4'
implementation 'com.github.Ficat:EasyBle:v2.0.1'
}
```

Expand All @@ -34,23 +34,28 @@ dependencies {

### 步骤如下

### 1.获取BleManager对象
### 1.获取BleManager对象并初始化

```java

//获取管理器对象
BleManager bleManager = BleManager.getInstance(this.getApplication());

//设置ble选项,可多次设置,EasyBle将使用最新的options。比如本次扫描周期
//为10s,但你想要下一次扫描周期更长一些,则再次调用本方法去设置即可
BleManager.Options options = new BleManager.Options();
options.loggable = true; //是否打印日志
options.connectTimeout = 10000; //连接超时时间
options.scanPeriod = 12000; //扫描周期
options.scanDeviceName = "targetDeviceName"; //扫描的目标设备名
options.scanDeviceAddress = "targetDeviceAddress"; //扫描目标设备地址如"DD:0D:30:00:0D:9B"
options.scanServiceUuids = serviceUuidArray; //扫描含该服务UUID的目标设备
bleManager.option(options);
// scan/connection不是必须的,若不设置,那么扫描或连接就
// 会使用默认参数
BleManager.ScanOptions scanOptions = BleManager.ScanOptions
.newInstance()
.scanPeriod(10000)
.scanDeviceName(null);

BleManager.ConnectOptions connectOptions = BleManager.ConnectOptions
.newInstance()
.connectTimeout(12000);


BleManager manager = BleManager
.getInstance()
.setScanOptions(scanOptions)//非必须设置项
.setConnectionOptions(connectOptions)
.setLog(true, "TAG")
.init(this.getApplication());//这里需要Context,但注意不要传Activity

```

Expand Down Expand Up @@ -80,6 +85,8 @@ dependencies {
}
});

//使用指定扫描参数扫描
bleManager.startScan(scanOptions, bleScanCallback);
```
当需要结束扫描时用以下方法结束扫描,建议在扫描到目标设备后停止扫描
```java
Expand All @@ -102,7 +109,12 @@ dependencies {
}

@Override
public void onTimeout(BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {
if(failCode == BleConnectCallback.FAIL_CONNECT_TIMEOUT){
//连接超时
}else{
//其他原因导致的连接失败
}

}

Expand All @@ -112,16 +124,19 @@ dependencies {
}

@Override
public void onDisconnected(BleDevice device) {
public void onDisconnected(String info, int status, BleDevice device) {

}
};

//通过BleDevice对象连接设备
bleManager.connect(bleDevice, bleConnectCallback);
//使用指定连接选项参数进行连接
bleManager.connect(bleDevice, connectOptions, bleConnectCallback);

//使用mac地址连接
bleManager.connect(address, bleConnectCallback);
bleManager.connect(address, connectOptions, bleConnectCallback);

//直接通过mac地址连接
bleManager.connect(address, bleConnectCallback)
```

当需要断开与设备的连接时可使用以下任一方法断开设备连接
Expand Down Expand Up @@ -152,8 +167,16 @@ notify和indicate都使用以下方法
}

@Override
public void onFail(int failCode, String info, BleDevice device) {

public void onFailure(int failCode, String info, BleDevice device) {
switch (failCode) {
case BleCallback.FAIL_DISCONNECTED://连接断开
break;
case BleCallback.FAIL_OTHER://其他原因
break;
default:
break;
}

}
});
```
Expand All @@ -171,7 +194,7 @@ notify和indicate都使用以下方法
}

@Override
public void onFail(int failCode, String info, BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {

}
});
Expand All @@ -185,7 +208,7 @@ notify和indicate都使用以下方法
}

@Override
public void onFail(int failCode, String info, BleDevice device) {
public void onFailure(int failCode, String info, BleDevice device) {

}
});
Expand Down
10 changes: 5 additions & 5 deletions easyble/src/main/java/com/ficat/easyble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,10 @@ private BleDevice newBleDevice(BluetoothDevice device) {
}

public static final class ScanOptions {
public int scanPeriod = 12000;
public String scanDeviceName;
public String scanDeviceAddress;
public UUID[] scanServiceUuids;
private int scanPeriod = 12000;
private String scanDeviceName;
private String scanDeviceAddress;
private UUID[] scanServiceUuids;

private ScanOptions() {

Expand Down Expand Up @@ -500,7 +500,7 @@ public ScanOptions scanServiceUuids(UUID[] serviceUuids) {
}

public static final class ConnectOptions {
public int connectTimeout = 10000;
private int connectTimeout = 10000;

private ConnectOptions() {

Expand Down

0 comments on commit 6aad2fe

Please sign in to comment.