显示附近蓝牙设备
This commit is contained in:
@@ -8,6 +8,8 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.ckkj.water.AppConfig;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -21,11 +23,20 @@ import no.nordicsemi.android.ble.BleManager;
|
||||
public class WaterDeviceManager extends BleManager {
|
||||
private BluetoothGattCharacteristic writeCharacteristic;
|
||||
private BluetoothGattCharacteristic notifyCharacteristic;
|
||||
private OnDataReceivedListener dataListener;
|
||||
|
||||
public interface OnDataReceivedListener {
|
||||
void onDataReceived(byte[] data);
|
||||
}
|
||||
|
||||
public WaterDeviceManager(@NonNull Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public void setOnDataReceivedListener(OnDataReceivedListener listener) {
|
||||
this.dataListener = listener;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected BleManagerGattCallback getGattCallback() {
|
||||
@@ -39,35 +50,88 @@ public class WaterDeviceManager extends BleManager {
|
||||
|
||||
setNotificationCallback(notifyCharacteristic)
|
||||
.with((device, data) -> {
|
||||
|
||||
byte[] bytes = data.getValue();
|
||||
Log.e("TEST", "收到数据: " + Arrays.toString(bytes));
|
||||
|
||||
Log.e("TEST", "收到数据: "
|
||||
+ Arrays.toString(bytes));
|
||||
if (dataListener != null) {
|
||||
dataListener.onDataReceived(bytes);
|
||||
}
|
||||
});
|
||||
|
||||
enableNotifications(notifyCharacteristic)
|
||||
.enqueue();
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public boolean isRequiredServiceSupported(@NonNull BluetoothGatt gatt) {
|
||||
//
|
||||
// BluetoothGattService service =
|
||||
// gatt.getService(UUID.fromString(
|
||||
// AppConfig.BLUETOOTH_DEVICE_UUID));
|
||||
//
|
||||
// if (service == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// writeCharacteristic =
|
||||
// service.getCharacteristic(UUID.fromString(
|
||||
// AppConfig.BLUETOOTH_DEVICE_UUID));
|
||||
//
|
||||
// notifyCharacteristic =
|
||||
// service.getCharacteristic(UUID.fromString(
|
||||
// AppConfig.BLUETOOTH_DEVICE_UUID));
|
||||
//
|
||||
// return writeCharacteristic != null
|
||||
// && notifyCharacteristic != null;
|
||||
// }
|
||||
@Override
|
||||
public boolean isRequiredServiceSupported(@NonNull BluetoothGatt gatt) {
|
||||
public boolean isRequiredServiceSupported(
|
||||
@NonNull BluetoothGatt gatt) {
|
||||
|
||||
BluetoothGattService service =
|
||||
gatt.getService(UUID.fromString(
|
||||
"0000ffff-0000-1000-8000-00805f9b34fb"));
|
||||
for (BluetoothGattService service :
|
||||
gatt.getServices()) {
|
||||
|
||||
if (service == null) {
|
||||
return false;
|
||||
Log.e("TEST",
|
||||
"Service=" + service.getUuid());
|
||||
|
||||
for (BluetoothGattCharacteristic c :
|
||||
service.getCharacteristics()) {
|
||||
|
||||
int properties = c.getProperties();
|
||||
|
||||
Log.e("TEST",
|
||||
"Characteristic="
|
||||
+ c.getUuid()
|
||||
+ " props="
|
||||
+ properties);
|
||||
|
||||
if ((properties &
|
||||
BluetoothGattCharacteristic.PROPERTY_WRITE)
|
||||
!= 0) {
|
||||
|
||||
writeCharacteristic = c;
|
||||
}
|
||||
|
||||
if ((properties &
|
||||
BluetoothGattCharacteristic.PROPERTY_NOTIFY)
|
||||
!= 0) {
|
||||
|
||||
notifyCharacteristic = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeCharacteristic =
|
||||
service.getCharacteristic(UUID.fromString(
|
||||
"0000ff01-0000-1000-8000-00805f9b34fb"));
|
||||
Log.e("TEST",
|
||||
"write="
|
||||
+ (writeCharacteristic != null
|
||||
? writeCharacteristic.getUuid()
|
||||
: "null"));
|
||||
|
||||
notifyCharacteristic =
|
||||
service.getCharacteristic(UUID.fromString(
|
||||
"0000ff02-0000-1000-8000-00805f9b34fb"));
|
||||
Log.e("TEST",
|
||||
"notify="
|
||||
+ (notifyCharacteristic != null
|
||||
? notifyCharacteristic.getUuid()
|
||||
: "null"));
|
||||
|
||||
return writeCharacteristic != null
|
||||
&& notifyCharacteristic != null;
|
||||
@@ -83,10 +147,23 @@ public class WaterDeviceManager extends BleManager {
|
||||
public void send(byte[] data) {
|
||||
|
||||
if (writeCharacteristic == null) {
|
||||
Log.e("TEST", "send: Write characteristic is null");
|
||||
return;
|
||||
}
|
||||
|
||||
writeCharacteristic(writeCharacteristic, data)
|
||||
.enqueue();
|
||||
}
|
||||
|
||||
public boolean isBlConnected() {
|
||||
// 通过反射或状态检查来判断是否已连接
|
||||
// BleManager 没有直接的 isConnected 方法,需要检查内部状态
|
||||
try {
|
||||
// 检查 writeCharacteristic 是否可用(连接后会初始化)
|
||||
return writeCharacteristic != null;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user