Skip to content

Commit

Permalink
1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed Mar 19, 2017
1 parent 7f10025 commit c141723
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 142 deletions.
17 changes: 16 additions & 1 deletion FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ public boolean scanMacAndConnect(String deviceMac,
return bleBluetooth.scanMacAndConnect(deviceMac, time_out, autoConnect, callback);
}

/**
* fuzzy search name
*
* @param fuzzyName
* @param time_out
* @param autoConnect
* @param callback
* @return
*/
public boolean fuzzySearchNameAndConnect(String fuzzyName,
long time_out,
boolean autoConnect,
BleGattCallback callback) {
return bleBluetooth.fuzzySearchNameAndConnect(fuzzyName, time_out, autoConnect, callback);
}

/**
* notify
*
Expand Down Expand Up @@ -227,7 +243,6 @@ public boolean isBlueEnable() {
return bleBluetooth != null && bleBluetooth.isBlueEnable();
}


public boolean isInScanning() {
return bleBluetooth.isInScanning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean scanNameAndConnect(String name, long time_out, final boolean auto
}
return false;
}
return startLeScan(new NameScanCallback(name, time_out) {
return startLeScan(new NameScanCallback(name, time_out, false) {

@Override
public void onDeviceFound(final BluetoothDevice device, int rssi, byte[] scanRecord) {
Expand Down Expand Up @@ -199,6 +199,35 @@ public void onDeviceNotFound() {
});
}

public boolean fuzzySearchNameAndConnect(String name, long time_out, final boolean autoConnect, final BleGattCallback callback) {
if (TextUtils.isEmpty(name)) {
if (callback != null) {
callback.onNotFoundDevice();
}
return false;
}
return startLeScan(new NameScanCallback(name, time_out, true) {

@Override
public void onDeviceFound(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnMainThread(new Runnable() {
@Override
public void run() {
connect(device, autoConnect, callback);
}
});
}

@Override
public void onDeviceNotFound() {
if (callback != null) {
callback.onNotFoundDevice();
}
}
});
}


public boolean refreshDeviceCache() {
try {
final Method refresh = BluetoothGatt.class.getMethod("refresh");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import java.util.List;

/**
* Created by 陈利健 on 2016/9/2.
* 一段限制时间内搜索所有设备
* scan for a period of time
*/
public abstract class ListScanCallback extends PeriodScanCallback {

/**
* 所有被发现的设备集合
*/
private List<BluetoothDevice> deviceList = new ArrayList<>();

public ListScanCallback(long timeoutMillis) {
Expand Down
23 changes: 8 additions & 15 deletions FastBleLib/src/main/java/com/clj/fastble/scan/MacScanCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Created by 陈利健 on 2016/11/25.
* 一段限制时间内搜索符合mac的设备,取第一个搜索到的设备
* scan a known mac device, then connect
*/
public abstract class MacScanCallback extends PeriodScanCallback{

/**
* 设备名
*/
private String mac;
/**
* 是否发现
*/
private AtomicBoolean hasFound = new AtomicBoolean(false);
public abstract class MacScanCallback extends PeriodScanCallback {


private String mMac;
private AtomicBoolean hasFound = new AtomicBoolean(false);

public MacScanCallback(String mac, long timeoutMillis) {
super(timeoutMillis);
this.mac = mac;
this.mMac = mac;
if (mac == null) {
throw new IllegalArgumentException("start scan, mac can not be null!");
onDeviceNotFound();
}
}

Expand All @@ -39,7 +32,7 @@ public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
}

if (!hasFound.get()) {
if (mac.equalsIgnoreCase(device.getAddress())) {
if (mMac.equalsIgnoreCase(device.getAddress())) {
hasFound.set(true);
bleBluetooth.stopScan(MacScanCallback.this);
onDeviceFound(device, rssi, scanRecord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Created by 陈利健 on 2016/8/12.
* 一段限制时间内搜索符合name的设备,取第一个搜索到的设备
* scan a known name device, then connect
*/
public abstract class NameScanCallback extends PeriodScanCallback {

/**
* 设备名
*/
private String name;
/**
* 是否发现
*/
private AtomicBoolean hasFound = new AtomicBoolean(false);

private String mName;
private boolean mFuzzy;
private AtomicBoolean hasFound = new AtomicBoolean(false);

public NameScanCallback(String name, long timeoutMillis) {
public NameScanCallback(String name, long timeoutMillis, boolean fuzzy) {
super(timeoutMillis);
this.name = name;
this.mName = name;
this.mFuzzy = fuzzy;
if (name == null) {
throw new IllegalArgumentException("start scan, name can not be null!");
onDeviceNotFound();
}
}

Expand All @@ -38,7 +33,7 @@ public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
}

if (!hasFound.get()) {
if (name.equalsIgnoreCase(device.getName())) {
if (mFuzzy ? mName.contains(device.getName()) : mName.equalsIgnoreCase(device.getName())) {
hasFound.set(true);
bleBluetooth.stopScan(NameScanCallback.this);
onDeviceFound(device, rssi, scanRecord);
Expand Down
94 changes: 0 additions & 94 deletions FastBleLib/src/main/java/com/clj/fastble/utils/BluetoothUtil.java

This file was deleted.

14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ Android Bluetooth Low Energy 蓝牙快速开发框架。

## Gradle
dependencies {
compile 'com.clj.fastble:FastBleLib:1.0.4'
compile 'com.clj.fastble:FastBleLib:1.0.5'
}

## Maven
<dependency>
<groupId>com.clj.fastble</groupId>
<artifactId>FastBleLib</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<type>pom</type>
</dependency>

Expand Down Expand Up @@ -106,7 +106,7 @@ Android Bluetooth Low Energy 蓝牙快速开发框架。

- ####扫描指定名称的设备、并连接
如果你确定周围有已知名称的蓝牙设备,或只需要连接指定名称的蓝牙设备,而忽略其他名称的设备,可以选择直接对指定名称进行搜索,搜索到即连接,搜索不到则回调超时接口
如果你确定周围有已知名称的蓝牙设备,或只需要连接指定名称的蓝牙设备,而忽略其他名称的设备,可以选择直接对指定名称进行搜索,搜索到即连接。

bleManager.scanNameAndConnect(
DEVICE_NAME,
Expand Down Expand Up @@ -138,7 +138,7 @@ Android Bluetooth Low Energy 蓝牙快速开发框架。
});

- ####扫描指定MAC地址的设备、并连接
如果你确定周围有已知地址的蓝牙设备,或只需要连接指定地址的蓝牙设备,而忽略其他地址的设备,可以选择直接对指定名称进行搜索,搜索到即连接,搜索不到则回调超时接口
如果你确定周围有已知地址的蓝牙设备,或只需要连接指定地址的蓝牙设备,而忽略其他地址的设备,可以选择直接对指定名称进行搜索,搜索到即连接。

bleManager.scanMacAndConnect(
DEVICE_MAC,
Expand Down Expand Up @@ -255,12 +255,6 @@ Android Bluetooth Low Energy 蓝牙快速开发框架。
bleManager.stopListenCharacterCallback(UUID_NOTIFY);


- #### 获取当前连接的状态
bleManager.isInScanning();
bleManager.isConnectingOrConnected();
bleManager.isConnected();
bleManager.isServiceDiscovered();

- ####复位(断开此次蓝牙连接,移除所有回调)
bleManager.closeBluetoothGatt();

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/clj/blesample/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DemoActivity extends AppCompatActivity {
private static final String UUID_WRITE = "0000fff1-0000-1000-8000-00805f9b34fb";
private static final String SAMPLE_WRITE_DATA = "000000000000000"; // 要写入设备某一个character的指令

private static final long TIME_OUT = 10000; // 扫描超时时间
private static final long TIME_OUT = 5000; // 扫描超时时间
private static final String DEVICE_NAME = "这里写你的设备名"; // 符合连接规则的蓝牙设备名
private static final String DEVICE_MAC = "这里写你的设备地址"; // 符合连接规则的蓝牙设备地址
private static final String TAG = "ble_sample";
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/clj/blesample/OperateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void scanDevice() {

progressDialog.show();

bleManager.scanDevice(new ListScanCallback(10000) {
bleManager.scanDevice(new ListScanCallback(5000) {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
super.onLeScan(device, rssi, scanRecord);
Expand Down

0 comments on commit c141723

Please sign in to comment.