Skip to content

Commit

Permalink
Merge pull request #1 from githubwwj/master
Browse files Browse the repository at this point in the history
  • Loading branch information
yizems committed Feb 3, 2023
2 parents c37ee36 + 7d98705 commit 916b532
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 463 deletions.
4 changes: 4 additions & 0 deletions FastBleLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
37 changes: 35 additions & 2 deletions FastBleLib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.clj.fastble">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<!-- Android 12以下配置的三个蓝牙权限 -->
<!-- 这个权限允许程序连接到已配对的蓝牙设备, 请求连接/接收连接/传输数据需要改权限, 主要用于对配对后进行操作 -->
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />

<!-- 这个权限允许程序发现和配对蓝牙设备, 该权限用来管理蓝牙设备, 有了这个权限, 应用才能使用本机的蓝牙设备, 主要用于对配对前的操作 -->
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />

<!-- Android 6.0以后,12.0以下,这两个权限是必须的,蓝牙扫描周围的设备需要获取模糊的位置信息。
这两个权限属于同一组危险权限,在清单文件中声明之后,还需要再运行时动态获取。 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Android 12以下配置的两个蓝牙权限 -->




<!-- Android 12 及以上版本配置的三个权限 -->
<!-- 您的应用查找蓝牙设备(如蓝牙低功耗 (BLE) 外围设备)-->
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />

<!-- 应用程序使手机蓝牙可被其它蓝牙设备发现时才需要-->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- 仅应用程序与已配对的蓝牙设备通信时才需要 -->
<uses-permission
android:name="android.permission.BLUETOOTH_CONNECT"
tools:targetApi="s" />
<!-- Android 12 及以上版本配置的三个权限 -->

</manifest>
28 changes: 22 additions & 6 deletions FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.clj.fastble;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
Expand Down Expand Up @@ -381,8 +382,8 @@ public BluetoothGatt connect(String mac, BleGattCallback bleGattCallback) {
/**
* Cancel scan
*/
public void cancelScan() {
BleScanner.getInstance().stopLeScan();
public void cancelScan(boolean isCallbackScanFinish) {
BleScanner.getInstance().stopLeScan(isCallbackScanFinish);
}

/**
Expand Down Expand Up @@ -746,13 +747,13 @@ public boolean requestConnectionPriority(BleDevice bleDevice, int connectionPrio
* @return
*/
public boolean isSupportBle() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& context.getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
return context.getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}

/**
* Open bluetooth
*/
@SuppressLint("MissingPermission")
public void enableBluetooth() {
if (bluetoothAdapter != null) {
bluetoothAdapter.enable();
Expand Down Expand Up @@ -848,6 +849,13 @@ public void removeNotifyCallback(BleDevice bleDevice, String uuid_notify) {
bleBluetooth.removeNotifyCallback(uuid_notify);
}

public boolean isHasNotifyCallback(BleDevice bleDevice, String uuid_notify) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
return bleBluetooth.isHasNotifyCallback(uuid_notify);
return false;
}

public void removeIndicateCallback(BleDevice bleDevice, String uuid_indicate) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
Expand Down Expand Up @@ -914,6 +922,14 @@ public boolean isConnected(String mac) {
return false;
}

public boolean isConnected() {
List<BleDevice> list = getAllConnectedDevice();
if (list != null && list.size() > 0) {
return true;
}
return false;
}

public void disconnect(BleDevice bleDevice) {
if (multipleBluetoothController != null) {
multipleBluetoothController.disconnect(bleDevice);
Expand All @@ -926,9 +942,9 @@ public void disconnectAllDevice() {
}
}

public void destroy() {
public void destroy(String mac) {
if (multipleBluetoothController != null) {
multipleBluetoothController.destroy();
multipleBluetoothController.destroy(mac);
}
}

Expand Down
Loading

0 comments on commit 916b532

Please sign in to comment.