Skip to content

Commit 2764bf5

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

9 files changed

Lines changed: 116 additions & 103 deletions

File tree

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#*BleLib*
1+
# **BleLib** #
22

3-
----------
43
BleLib 是 Android 低功耗蓝牙 4.0 及以上开发的辅助库,一行代码解决 Ble 初始化、扫描、连接、特性读写、设置通知等操作。
54

65
BleLib 中的关键类:
@@ -10,18 +9,23 @@ BleLib 中的关键类:
109
- MultipleBleService 类是可多个蓝牙设备同时连接的服务类
1110

1211

13-
##*Usage*
12+
## **Screenshots** ##
13+
14+
![](sample/screenshots/connect_single_device.png) ![](sample/screenshots/connect_multiple_devices.png)
15+
16+
17+
## **Usage** ##
1418

1519
可看博客: [使用 BleLib 的轻松搞定 Android 低功耗蓝牙 Ble 4.0 开发详解](http://blog.csdn.net/kjunchen/article/details/50909410)
1620

17-
###*引入*
21+
## **引入** ##
1822
BleLib 库已上传至 jcenter、maven central 仓库
1923
因此,在你项目 Module 中的 build.gradle 文件中添加库依赖即可,如下:
2024
Gradle:
2125

2226
```.gradle
2327
dependencies {
24-
compile 'com.junkchen.blelib:blelib:1.2.3'
28+
compile 'com.junkchen.blelib:blelib:1.2.4'
2529
}
2630
```
2731

@@ -132,6 +136,11 @@ mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener()
132136

133137
## **Release Notes** ##
134138

139+
- **blelib-1.2.4**(2017-05-31)
140+
141+
- 修复上个版本未解决的 bug,并优化代码。
142+
143+
135144
- **blelib-1.2.3**(2016-09-23)
136145

137146
- 修复扫描结果返回时出现空指针的 bug 。
@@ -154,14 +163,15 @@ mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener()
154163
- 在 OnDataAvailableListener 接口中新增 onDescriptorRead() 方法 。
155164

156165

157-
##*Reference*
166+
## **Reference** ##
158167

159168
[https://developer.android.com/guide/topics/connectivity/bluetooth-le.html](https://developer.android.com/guide/topics/connectivity/bluetooth-le.html)
160169

161170

162-
##*License*
171+
## **License** ##
163172

164173
BleLib is released under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0).
174+
165175
```.html
166176
Copyright 2016 Junk Chen.
167177

blelib/build.gradle

Lines changed: 4 additions & 4 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.3'
4+
libraryVersion = '1.2.4'
55

66
bintrayRepo = 'maven'
77
bintrayName = 'blelib'
@@ -27,13 +27,13 @@ ext {
2727

2828
android {
2929
compileSdkVersion 23
30-
buildToolsVersion "23.0.2"
30+
buildToolsVersion '26.0.0 rc2'
3131

3232
defaultConfig {
3333
minSdkVersion 18
3434
targetSdkVersion 23
3535
versionCode 1
36-
versionName "1.2.3"
36+
versionName "1.2.4"
3737
}
3838
buildTypes {
3939
release {
@@ -50,7 +50,7 @@ android {
5050
}
5151

5252
dependencies {
53-
compile fileTree(dir: 'libs', include: ['*.jar'])
53+
compile fileTree(include: ['*.jar'], dir: 'libs')
5454
testCompile 'junit:junit:4.12'
5555
compile 'com.android.support:appcompat-v7:23.1.1'
5656
}

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

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import android.bluetooth.BluetoothGattService;
2828
import android.bluetooth.BluetoothManager;
2929
import android.bluetooth.BluetoothProfile;
30+
import android.bluetooth.le.ScanCallback;
31+
import android.bluetooth.le.ScanResult;
3032
import android.content.Context;
3133
import android.content.Intent;
3234
import android.content.pm.PackageManager;
@@ -173,28 +175,31 @@ public void scanLeDevice(final boolean enable, long scanPeriod) {
173175
@Override
174176
public void run() {
175177
isScanning = false;
176-
mBluetoothAdapter.stopLeScan(mScanCallback);
177-
broadcastUpdate(ACTION_SCAN_FINISHED);
178-
if (mScanLeDeviceList != null) {
179-
mScanLeDeviceList.clear();
180-
mScanLeDeviceList = null;
178+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
179+
mBluetoothAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
180+
} else {
181+
mBluetoothAdapter.stopLeScan(mLeScanCallback);
181182
}
182-
// mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
183+
broadcastUpdate(ACTION_SCAN_FINISHED);
184+
mScanLeDeviceList.clear();
183185
}
184186
}, scanPeriod);
185187
mScanLeDeviceList.clear();
186188
isScanning = true;
187-
mBluetoothAdapter.startLeScan(mScanCallback);
188-
// mBluetoothAdapter.getBluetoothLeScanner().startScan(mLeScanCallback);
189+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
190+
mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback);
191+
} else {
192+
mBluetoothAdapter.startLeScan(mLeScanCallback);
193+
}
189194
} else {
190195
isScanning = false;
191-
mBluetoothAdapter.stopLeScan(mScanCallback);
192-
broadcastUpdate(ACTION_SCAN_FINISHED);
193-
if (mScanLeDeviceList != null) {
194-
mScanLeDeviceList.clear();
195-
mScanLeDeviceList = null;
196+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
197+
mBluetoothAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
198+
} else {
199+
mBluetoothAdapter.stopLeScan(mLeScanCallback);
196200
}
197-
// mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
201+
broadcastUpdate(ACTION_SCAN_FINISHED);
202+
mScanLeDeviceList.clear();
198203
}
199204
}
200205

@@ -216,15 +221,6 @@ public boolean isScanning() {
216221
return isScanning;
217222
}
218223

219-
/**
220-
* Get scan ble devices
221-
*
222-
* @return scan le device list
223-
*/
224-
public List<BluetoothDevice> getScanLeDevice() {
225-
return mScanLeDeviceList;
226-
}
227-
228224
/**
229225
* Connects to the GATT server hosted on the Bluetooth LE device.
230226
*
@@ -503,6 +499,7 @@ public boolean readRemoteRssi() {
503499
* whether this operation was successful.
504500
*
505501
* <p>Requires {@link Manifest.permission#BLUETOOTH} permission.
502+
*
506503
* @param mtu mtu
507504
* @return true, if the new MTU value has been requested successfully
508505
*/
@@ -545,26 +542,47 @@ public List<BluetoothDevice> getConnectDevices() {
545542
}
546543

547544
/**
548-
* Device scan callback
545+
* Device scan callback.
546+
* <p>
547+
* Use mScanCallback if Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP,
548+
* else use mLeScanCallback.
549549
*/
550-
private final BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
551-
@Override
552-
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
553-
if (device == null || mScanLeDeviceList.contains(device)) return;
554-
mScanLeDeviceList.add(device);
555-
if (mOnLeScanListener != null) {
556-
mOnLeScanListener.onLeScan(device, rssi, scanRecord);
557-
}
558-
broadcastUpdate(ACTION_BLUETOOTH_DEVICE, device);
559-
}
560-
};
550+
private ScanCallback mScanCallback;
551+
private BluetoothAdapter.LeScanCallback mLeScanCallback;
561552

562-
/*private final ScanCallback mLeScanCallback = new ScanCallback() {
563-
@Override
564-
public void onScanResult(int callbackType, ScanResult result) {
565-
super.onScanResult(callbackType, result);
553+
{
554+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
555+
mScanCallback = new ScanCallback() {
556+
@Override
557+
public void onScanResult(int callbackType, ScanResult result) {
558+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
559+
if (mScanLeDeviceList.contains(result.getDevice())) return;
560+
mScanLeDeviceList.add(result.getDevice());
561+
if (mOnLeScanListener != null) {
562+
mOnLeScanListener.onLeScan(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes());
563+
}
564+
broadcastUpdate(ACTION_BLUETOOTH_DEVICE, result.getDevice());
565+
Log.i(TAG, "onScanResult: name: " + result.getDevice().getName() +
566+
", address: " + result.getDevice().getAddress() +
567+
", rssi: " + result.getRssi() + ", scanRecord: " + result.getScanRecord());
568+
}
569+
}
570+
};
571+
} else {
572+
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
573+
@Override
574+
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
575+
Log.i(TAG, "device name: " + device.getName() + ", address: " + device.getAddress());
576+
if (device == null || mScanLeDeviceList.contains(device)) return;
577+
mScanLeDeviceList.add(device);
578+
if (mOnLeScanListener != null) {
579+
mOnLeScanListener.onLeScan(device, rssi, scanRecord);
580+
}
581+
broadcastUpdate(ACTION_BLUETOOTH_DEVICE, device);
582+
}
583+
};
566584
}
567-
};*/
585+
}
568586

569587
/**
570588
* Implements callback methods for GATT events that the app cares about. For example,

0 commit comments

Comments
 (0)