Skip to content

Commit 248f481

Browse files
committed
fix bug that scan result is null cause null pointer exception
1 parent ce2f3ed commit 248f481

5 files changed

Lines changed: 18 additions & 19 deletions

File tree

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ BleLib 库已上传至 jcenter、maven central 仓库
1919
因此,在你项目 Module 中的 build.gradle 文件中添加库依赖即可,如下:
2020
Gradle:
2121

22-
```gradle
22+
```.gradle
2323
dependencies {
24-
compile 'com.junkchen.blelib:blelib:1.2.2'
24+
compile 'com.junkchen.blelib:blelib:1.2.3'
2525
}
2626
```
2727

2828
只此一句即可使用 BleLib 库,方便吧,要的就是这效果。
2929

3030
BleLib 中的 Ble 继承了 Service,因此建议绑定服务进行使用。
3131

32-
```java
32+
```.java
3333
private BleService mBleService;
3434
private boolean mIsBind;
3535
private ServiceConnection serviceConnection = new ServiceConnection() {
@@ -71,7 +71,7 @@ if (mIsBind) {
7171

7272
当服务绑定后就可以进行如下操作了:
7373

74-
```java
74+
```.java
7575
mBleService.initialize();//Ble初始化操作
7676
mBleService.enableBluetooth(boolean enable);//打开或关闭蓝牙
7777
mBleService.scanLeDevice(boolean enable, long scanPeriod);//启动或停止扫描Ble设备
@@ -86,7 +86,7 @@ mBleService.writeCharacteristic(BluetoothGattCharacteristic characteristic, byte
8686

8787
设置监听回调接口,获取相应返回数据,获取扫描Ble结果、连接等操作也可以以接收广播的方式获取,但我个人觉得用监听的方式更好,广播有的值无法传递,而接口传递过来的是原始数据,在我的样例中有使用广播来接收扫描的结果和连接状态的改变。
8888

89-
```java
89+
```.java
9090
//Ble扫描回调
9191
mBleService.setOnLeScanListener(new BleService.OnLeScanListener() {
9292
@Override
@@ -132,6 +132,11 @@ mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener()
132132

133133
## **Release Notes** ##
134134

135+
- **blelib-1.2.3**(2016-09-23)
136+
137+
- 修复扫描结果返回时出现空指针的 bug 。
138+
139+
135140
- **blelib-1.2.2**(2016-09-08)
136141

137142
- 修复调用 scanLeDevice(false) 不能停止扫描的 bug 。
@@ -157,7 +162,7 @@ mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener()
157162
##*License*
158163

159164
BleLib is released under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0).
160-
```html
165+
```.html
161166
Copyright 2016 Junk Chen.
162167
163168
Licensed under the Apache License, Version 2.0 (the "License");

blelib/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'com.android.library'
22

33
ext {
4-
libraryVersion = '1.2.2'
4+
libraryVersion = '1.2.3'
55

66
bintrayRepo = 'maven'
77
bintrayName = 'blelib'
@@ -33,7 +33,7 @@ android {
3333
minSdkVersion 18
3434
targetSdkVersion 23
3535
versionCode 1
36-
versionName "1.2.2"
36+
versionName "1.2.3"
3737
}
3838
buildTypes {
3939
release {

blelib/src/main/java/com/junkchen/blelib/BleService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class BleService extends Service implements Constants, BleListener {
5151
private BluetoothManager mBluetoothManager;
5252
private BluetoothAdapter mBluetoothAdapter;
5353
private BluetoothGatt mBluetoothGatt;
54-
private List<BluetoothDevice> mScanLeDeviceList;
54+
private List<BluetoothDevice> mScanLeDeviceList = new ArrayList<>();
5555
private boolean isScanning;
5656
private boolean isConnect;
5757
private String mBluetoothDeviceAddress;
@@ -182,9 +182,6 @@ public void run() {
182182
// mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
183183
}
184184
}, scanPeriod);
185-
if (mScanLeDeviceList == null) {
186-
mScanLeDeviceList = new ArrayList<>();
187-
}
188185
mScanLeDeviceList.clear();
189186
isScanning = true;
190187
mBluetoothAdapter.startLeScan(mScanCallback);
@@ -553,7 +550,7 @@ public List<BluetoothDevice> getConnectDevices() {
553550
private final BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
554551
@Override
555552
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
556-
if (mScanLeDeviceList.contains(device)) return;
553+
if (device == null || mScanLeDeviceList.contains(device)) return;
557554
mScanLeDeviceList.add(device);
558555
if (mOnLeScanListener != null) {
559556
mOnLeScanListener.onLeScan(device, rssi, scanRecord);

blelib/src/main/java/com/junkchen/blelib/MultipleBleService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class MultipleBleService extends Service implements Constants, BleListene
5656
private BluetoothManager mBluetoothManager;
5757
private BluetoothAdapter mBluetoothAdapter;
5858
private Map<String, BluetoothGatt> mBluetoothGattMap;
59-
private List<BluetoothDevice> mScanLeDeviceList;
59+
private List<BluetoothDevice> mScanLeDeviceList = new ArrayList<>();
6060
private boolean isScanning;
6161
private List<String> mConnectedAddressList;//Already connected remote device address
6262
//Stop scanning after 10 seconds.
@@ -190,9 +190,6 @@ public void run() {
190190
// mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
191191
}
192192
}, scanPeriod);
193-
if (mScanLeDeviceList == null) {
194-
mScanLeDeviceList = new ArrayList<>();
195-
}
196193
mScanLeDeviceList.clear();
197194
isScanning = true;
198195
if (Build.VERSION.SDK_INT >= 21) {
@@ -610,7 +607,7 @@ public List<BluetoothGattService> getSupportedGattServices(String address) {
610607
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
611608
Log.i(TAG, "device name: " + device.getName() + ", address: " + device.getAddress());
612609
// Log.i(TAG, "mScanLeDeviceList.contains(device): " + mScanLeDeviceList.contains(device));
613-
if (mScanLeDeviceList.contains(device)) return;
610+
if (device == null || mScanLeDeviceList.contains(device)) return;
614611
mScanLeDeviceList.add(device);
615612
if (mOnLeScanListener != null) {
616613
mOnLeScanListener.onLeScan(device, rssi, scanRecord);

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.3'
8+
classpath 'com.android.tools.build:gradle:2.2.0'
99
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3'
1010
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1111

0 commit comments

Comments
 (0)