Skip to content

Commit 916b532

Browse files
authored
Merge pull request #1 from githubwwj/master
MR Jasonchenlijian#547
2 parents c37ee36 + 7d98705 commit 916b532

17 files changed

+478
-463
lines changed

FastBleLib/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ android {
1616
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1717
}
1818
}
19+
compileOptions {
20+
sourceCompatibility JavaVersion.VERSION_1_8
21+
targetCompatibility JavaVersion.VERSION_1_8
22+
}
1923
}
2024

2125
dependencies {
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
23
package="com.clj.fastble">
34

4-
<uses-permission android:name="android.permission.BLUETOOTH" />
5-
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5+
6+
<!-- Android 12以下配置的三个蓝牙权限 -->
7+
<!-- 这个权限允许程序连接到已配对的蓝牙设备, 请求连接/接收连接/传输数据需要改权限, 主要用于对配对后进行操作 -->
8+
<uses-permission
9+
android:name="android.permission.BLUETOOTH"
10+
android:maxSdkVersion="30" />
11+
12+
<!-- 这个权限允许程序发现和配对蓝牙设备, 该权限用来管理蓝牙设备, 有了这个权限, 应用才能使用本机的蓝牙设备, 主要用于对配对前的操作 -->
13+
<uses-permission
14+
android:name="android.permission.BLUETOOTH_ADMIN"
15+
android:maxSdkVersion="30" />
16+
17+
<!-- Android 6.0以后,12.0以下,这两个权限是必须的,蓝牙扫描周围的设备需要获取模糊的位置信息。
18+
这两个权限属于同一组危险权限,在清单文件中声明之后,还需要再运行时动态获取。 -->
619
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
720
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
21+
<!-- Android 12以下配置的两个蓝牙权限 -->
22+
23+
24+
25+
26+
<!-- Android 12 及以上版本配置的三个权限 -->
27+
<!-- 您的应用查找蓝牙设备(如蓝牙低功耗 (BLE) 外围设备)-->
28+
<uses-permission
29+
android:name="android.permission.BLUETOOTH_SCAN"
30+
android:usesPermissionFlags="neverForLocation"
31+
tools:targetApi="s" />
32+
33+
<!-- 应用程序使手机蓝牙可被其它蓝牙设备发现时才需要-->
34+
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
35+
36+
<!-- 仅应用程序与已配对的蓝牙设备通信时才需要 -->
37+
<uses-permission
38+
android:name="android.permission.BLUETOOTH_CONNECT"
39+
tools:targetApi="s" />
40+
<!-- Android 12 及以上版本配置的三个权限 -->
841

942
</manifest>

FastBleLib/src/main/java/com/clj/fastble/BleManager.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clj.fastble;
22

3+
import android.annotation.SuppressLint;
34
import android.annotation.TargetApi;
45
import android.app.Application;
56
import android.bluetooth.BluetoothAdapter;
@@ -381,8 +382,8 @@ public BluetoothGatt connect(String mac, BleGattCallback bleGattCallback) {
381382
/**
382383
* Cancel scan
383384
*/
384-
public void cancelScan() {
385-
BleScanner.getInstance().stopLeScan();
385+
public void cancelScan(boolean isCallbackScanFinish) {
386+
BleScanner.getInstance().stopLeScan(isCallbackScanFinish);
386387
}
387388

388389
/**
@@ -746,13 +747,13 @@ public boolean requestConnectionPriority(BleDevice bleDevice, int connectionPrio
746747
* @return
747748
*/
748749
public boolean isSupportBle() {
749-
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
750-
&& context.getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
750+
return context.getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
751751
}
752752

753753
/**
754754
* Open bluetooth
755755
*/
756+
@SuppressLint("MissingPermission")
756757
public void enableBluetooth() {
757758
if (bluetoothAdapter != null) {
758759
bluetoothAdapter.enable();
@@ -848,6 +849,13 @@ public void removeNotifyCallback(BleDevice bleDevice, String uuid_notify) {
848849
bleBluetooth.removeNotifyCallback(uuid_notify);
849850
}
850851

852+
public boolean isHasNotifyCallback(BleDevice bleDevice, String uuid_notify) {
853+
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
854+
if (bleBluetooth != null)
855+
return bleBluetooth.isHasNotifyCallback(uuid_notify);
856+
return false;
857+
}
858+
851859
public void removeIndicateCallback(BleDevice bleDevice, String uuid_indicate) {
852860
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
853861
if (bleBluetooth != null)
@@ -914,6 +922,14 @@ public boolean isConnected(String mac) {
914922
return false;
915923
}
916924

925+
public boolean isConnected() {
926+
List<BleDevice> list = getAllConnectedDevice();
927+
if (list != null && list.size() > 0) {
928+
return true;
929+
}
930+
return false;
931+
}
932+
917933
public void disconnect(BleDevice bleDevice) {
918934
if (multipleBluetoothController != null) {
919935
multipleBluetoothController.disconnect(bleDevice);
@@ -926,9 +942,9 @@ public void disconnectAllDevice() {
926942
}
927943
}
928944

929-
public void destroy() {
945+
public void destroy(String mac) {
930946
if (multipleBluetoothController != null) {
931-
multipleBluetoothController.destroy();
947+
multipleBluetoothController.destroy(mac);
932948
}
933949
}
934950

0 commit comments

Comments
 (0)