Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进FastBle库 #547

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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