Skip to content

Commit

Permalink
Update version 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Ficat committed Jan 17, 2025
1 parent 0ff8dda commit 350780e
Show file tree
Hide file tree
Showing 24 changed files with 1,661 additions and 923 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ dependencies {
.scanPeriod(10000)
.scanDeviceName(null);

BleManager.ConnectOptions connectOptions = BleManager.ConnectOptions
BleManager.ConnectOptions connectionOptions = BleManager.ConnectOptions
.newInstance()
.connectTimeout(12000);

BleManager bleManager = BleManager
.getInstance()
.setScanOptions(scanOptions)//it is not necessary
.setConnectionOptions(connectOptions)//like scan options
.setConnectionOptions(connectionOptions)//like scan options
.setLog(true, "TAG")
.init(this.getApplication());//Context is needed here,do not use Activity,which can cause Activity leak

Expand Down Expand Up @@ -130,12 +130,12 @@ You can connect to remote device by device address or BleDevice object
};

bleManager.connect(bleDevice, bleConnectCallback);
//connect with specified connectOptions
bleManager.connect(bleDevice, connectOptions, bleConnectCallback);
//connect with specified connectionOptions
bleManager.connect(bleDevice, connectionOptions, bleConnectCallback);

//connect with mac address
bleManager.connect(address, bleConnectCallback);
bleManager.connect(address, connectOptions, bleConnectCallback);
bleManager.connect(address, connectionOptions, bleConnectCallback);

```

Expand Down
8 changes: 4 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ dependencies {
.scanPeriod(10000)
.scanDeviceName(null);

BleManager.ConnectOptions connectOptions = BleManager.ConnectOptions
BleManager.ConnectOptions connectionOptions = BleManager.ConnectOptions
.newInstance()
.connectTimeout(12000);

BleManager manager = BleManager
.getInstance()
.setScanOptions(scanOptions)//非必须设置项
.setConnectionOptions(connectOptions)
.setConnectionOptions(connectionOptions)
.setLog(true, "TAG")
.init(this.getApplication());//这里需要Context,但注意不要传Activity

Expand Down Expand Up @@ -126,11 +126,11 @@ dependencies {

bleManager.connect(bleDevice, bleConnectCallback);
//使用指定连接选项参数进行连接
bleManager.connect(bleDevice, connectOptions, bleConnectCallback);
bleManager.connect(bleDevice, connectionOptions, bleConnectCallback);

//使用mac地址连接
bleManager.connect(address, bleConnectCallback);
bleManager.connect(address, connectOptions, bleConnectCallback);
bleManager.connect(address, connectionOptions, bleConnectCallback);

```

Expand Down
1 change: 1 addition & 0 deletions easyble/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<!-- 2.Android6 ~ Android11(api23~30) -->
<!-- Android6 ~ 9(api23 ~ 28): ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION -->
<!-- Android10 ~ 11(api29 ~ 30): ACCESS_FINE_LOCATION -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- 3.Android12(api31) or higher -->
Expand Down
57 changes: 48 additions & 9 deletions easyble/src/main/java/com/ficat/easyble/BleDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import android.os.Parcelable;
import android.text.TextUtils;


/**
* Created by pw on 2018/9/13.
*/

public class BleDevice implements Parcelable {
public final class BleDevice implements Parcelable {
private static final String DEFAULT_NAME = "unknown";

/**
Expand All @@ -25,9 +24,23 @@ public class BleDevice implements Parcelable {
*/
private volatile int connState = DISCONNECTED;

/**
* Device name
*/
private String name;

/**
* Original bluetooth device
*/
private final BluetoothDevice device;

/**
* Extra info, you can use this to save what you want
*/
private String parcelableExtraClassName;//extra info class name
private Parcelable parcelableExtra;//extra info


BleDevice(BluetoothDevice device) {
this.device = device;
tryToGetName();
Expand All @@ -52,26 +65,38 @@ public boolean isConnecting() {
return connState == CONNECTING;
}

public void setConnectionState(int newState) {
synchronized (this) {
this.connState = newState;
public int getConnectionState() {
return this.connState;
}

public void setParcelableExtra(Parcelable parcelableExtra) {
if (parcelableExtra == null) {
return;
}
this.parcelableExtra = parcelableExtra;
this.parcelableExtraClassName = parcelableExtra.getClass().getName();
}

public int getConnectionState() {
return this.connState;
public Parcelable getParcelableExtra() {
return parcelableExtra;
}

public BluetoothDevice getDevice() {
public BluetoothDevice getBluetoothDevice() {
return device;
}

void setConnectionState(int newState) {
synchronized (this) {
this.connState = newState;
}
}

private void tryToGetName() {
//Android12(api31) or higher,Bluetooth#getName() needs 'BLUETOOTH_CONNECT' permission
try {
this.name = device.getName();
} catch (Exception e) {
Logger.d("Failed to call BluetoothDevice#getName() because of no 'BLUETOOTH_CONNECT' permission");
Logger.e("Failed to call BluetoothDevice#getName(), error msg: " + e.getMessage());
} finally {
//Device name got from BluetoothDevice#getName() may be null, so check it
if (TextUtils.isEmpty(this.name)) {
Expand All @@ -86,6 +111,8 @@ public String toString() {
"connState=" + connState +
", name='" + name + '\'' +
", device=" + device +
", parcelableExtraClassName='" + parcelableExtraClassName + '\'' +
", parcelableExtra=" + parcelableExtra +
'}';
}

Expand All @@ -99,12 +126,24 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.connState);
dest.writeString(this.name);
dest.writeParcelable(this.device, flags);
dest.writeString(this.parcelableExtraClassName);
dest.writeParcelable(this.parcelableExtra, flags);
}

protected BleDevice(Parcel in) {
this.connState = in.readInt();
this.name = in.readString();
this.device = in.readParcelable(BluetoothDevice.class.getClassLoader());
this.parcelableExtraClassName = in.readString();
if (!TextUtils.isEmpty(this.parcelableExtraClassName)) {
try {
Class<?> claze = Class.forName(this.parcelableExtraClassName);
this.parcelableExtra = in.readParcelable(claze.getClassLoader());
} catch (Exception e) {
Logger.e("Failed to read parcelable extra info while creating BleDevice, error msg: "
+ e.getMessage());
}
}
}

public static final Creator<BleDevice> CREATOR = new Creator<BleDevice>() {
Expand Down
45 changes: 45 additions & 0 deletions easyble/src/main/java/com/ficat/easyble/BleDeviceAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ficat.easyble;

import android.bluetooth.BluetoothDevice;

import com.ficat.easyble.gatt.BleGattImpl;
import com.ficat.easyble.scan.BleScanner;


public class BleDeviceAccessor {
/**
* Create a BleDevice instance
*
* @param device BluetoothDevice
* @param key access key
* @return BleDevice instance
*/
public static BleDevice newBleDevice(BluetoothDevice device, Object key) {
// Thread.currentThread().getStackTrace();//not a good solution (performance / code-obfuscation)
if (device == null) {
throw new IllegalArgumentException("BluetoothDevice is null");
}
if (!(key instanceof BleScanner.AccessKey)) {
throw new SecurityException("Invalid key");
}
return new BleDevice(device);
}

/**
* Access the method {@link BleDevice#setConnectionState(int)} to set connection state
*
* @param device target BleDevice
* @param newConnState new connection state
* @param key access key
*/
public static void setBleDeviceConnection(BleDevice device, int newConnState, Object key) {
if (device == null) {
throw new IllegalArgumentException("BleDevice is null");
}
if (!(key instanceof BleGattImpl.AccessKey)) {
throw new SecurityException("Invalid key");
}
device.setConnectionState(newConnState);
}

}
Loading

0 comments on commit 350780e

Please sign in to comment.