|
@@ -1,231 +1,231 @@
|
|
|
-package xn.hxp.utils.bluetooth;
|
|
|
-
|
|
|
-import android.bluetooth.BluetoothGatt;
|
|
|
-import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
-import android.bluetooth.BluetoothGattService;
|
|
|
-import android.util.Log;
|
|
|
-
|
|
|
-import com.blankj.utilcode.util.AppUtils;
|
|
|
-import com.blankj.utilcode.util.LogUtils;
|
|
|
-import com.blankj.utilcode.util.SPUtils;
|
|
|
-import com.clj.fastble.BleManager;
|
|
|
-import com.clj.fastble.callback.BleGattCallback;
|
|
|
-import com.clj.fastble.callback.BleNotifyCallback;
|
|
|
-import com.clj.fastble.callback.BleScanCallback;
|
|
|
-import com.clj.fastble.data.BleDevice;
|
|
|
-import com.clj.fastble.data.BleScanState;
|
|
|
-import com.clj.fastble.exception.BleException;
|
|
|
-
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-public enum BleTool {
|
|
|
- INSTANCE;
|
|
|
-
|
|
|
- private final String SERVICE_UUID = "0000fff0-0000-1000-8000-00805f9b34fb";
|
|
|
- private final String CHARACTERISTIC_UUID = "0000fff1-0000-1000-8000-00805f9b34fb";
|
|
|
-
|
|
|
- private BleScanCallback bleScanCallback;
|
|
|
- private BleCallback bleCallback;
|
|
|
- private BleDevice bleDevice;
|
|
|
- private BleNotifyCallback bleNotifyCallback;
|
|
|
-
|
|
|
- private String weight = "";
|
|
|
- private long lastWeightTime = 0;
|
|
|
-
|
|
|
- public interface BleCallback {
|
|
|
- void onSuccess();
|
|
|
-
|
|
|
- void onNotifyFailure(Exception exception);
|
|
|
-
|
|
|
- void onChanged(String data);
|
|
|
- }
|
|
|
-
|
|
|
- public void setBleCallback(BleCallback bleCallback) {
|
|
|
- weight = "";
|
|
|
- this.bleCallback = bleCallback;
|
|
|
- }
|
|
|
-
|
|
|
- public synchronized void connect() {
|
|
|
- if (BleManager.getInstance().getScanSate() != BleScanState.STATE_SCANNING) {
|
|
|
- if (null == bleScanCallback) {
|
|
|
- bleScanCallback = new BleScanCallback() {
|
|
|
- @Override
|
|
|
- public void onScanFinished(List<BleDevice> scanResultList) {
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onScanStarted(boolean success) {
|
|
|
- if (null != bleCallback) {
|
|
|
- LogUtils.d("蓝牙", success);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onScanning(BleDevice bleDevice) {
|
|
|
- try {
|
|
|
- String bleMac = SPUtils.getInstance().getString("bleMac", "");
|
|
|
- LogUtils.d("蓝牙", bleMac);
|
|
|
- if (bleDevice.getMac().equals(bleMac)) {
|
|
|
- BleManager.getInstance().cancelScan();
|
|
|
- BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
|
|
|
- @Override
|
|
|
- public void onStartConnect() {
|
|
|
- LogUtils.d("蓝牙", "开始连接", bleDevice.getName(), bleDevice.getMac());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onConnectFail(BleDevice bleDevice, BleException exception) {
|
|
|
- LogUtils.e(bleDevice.getMac(), bleDevice.getName(), exception.getDescription());
|
|
|
- if (null != bleCallback) {
|
|
|
- LogUtils.d("蓝牙", exception.getDescription());
|
|
|
- bleCallback.onNotifyFailure(new Exception(exception.getDescription()));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
|
|
|
- BleTool.INSTANCE.bleDevice = bleDevice;
|
|
|
- LogUtils.d("蓝牙", "连接成功", bleDevice.getName(), bleDevice.getMac());
|
|
|
- if (null != gatt) {
|
|
|
- List<BluetoothGattService> bluetoothGattServiceList = gatt.getServices();
|
|
|
- if (null != bluetoothGattServiceList) {
|
|
|
- BluetoothGattService bluetoothGattService = null;
|
|
|
- for (int i = 0; i < bluetoothGattServiceList.size(); i++) {
|
|
|
- BluetoothGattService gattService = bluetoothGattServiceList.get(i);
|
|
|
- if (SERVICE_UUID.equals(gattService.getUuid().toString())) {
|
|
|
- bluetoothGattService = gattService;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (null != bluetoothGattService) {
|
|
|
- List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = bluetoothGattService.getCharacteristics();
|
|
|
- if (null != bluetoothGattCharacteristicList) {
|
|
|
- BluetoothGattCharacteristic bluetoothGattCharacteristic = null;
|
|
|
- for (int i = 0; i < bluetoothGattCharacteristicList.size(); i++) {
|
|
|
- BluetoothGattCharacteristic gattCharacteristic = bluetoothGattCharacteristicList.get(i);
|
|
|
- if (null != gattCharacteristic) {
|
|
|
- if (CHARACTERISTIC_UUID.equals(gattCharacteristic.getUuid().toString())) {
|
|
|
- bluetoothGattCharacteristic = gattCharacteristic;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (bluetoothGattCharacteristic != null) {
|
|
|
- if (null == bleNotifyCallback) {
|
|
|
- bleNotifyCallback = new BleNotifyCallback() {
|
|
|
- @Override
|
|
|
- public void onNotifySuccess() {
|
|
|
- if (null != bleCallback) {
|
|
|
- bleCallback.onSuccess();
|
|
|
- }
|
|
|
- LogUtils.d("蓝牙", "订阅成功");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onNotifyFailure(BleException exception) {
|
|
|
- if (null != bleCallback) {
|
|
|
- bleCallback.onNotifyFailure(new Exception(exception.getDescription()));
|
|
|
- }
|
|
|
- LogUtils.e("蓝牙", "订阅失败", exception);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onCharacteristicChanged(byte[] data) {
|
|
|
- String string = new String(data, StandardCharsets.UTF_8);
|
|
|
- String[] split = string.split("\n", -1);
|
|
|
- if (AppUtils.isAppDebug()) {
|
|
|
- LogUtils.d("蓝牙", "处理前的蓝牙数据", data, split);
|
|
|
- }
|
|
|
- if (null != bleCallback) {
|
|
|
- if (split.length > 0) {
|
|
|
- String line = split[0].trim();
|
|
|
- if (!weight.equals(line) || (System.currentTimeMillis() - lastWeightTime > 3000)) {
|
|
|
- lastWeightTime = System.currentTimeMillis();
|
|
|
- weight = line;
|
|
|
- LogUtils.d("蓝牙", "处理前的蓝牙数据", data, split);
|
|
|
- if (weight.startsWith("=")) {
|
|
|
- try {
|
|
|
- String wD = new StringBuilder(weight.replace("=", "")).reverse().toString();
|
|
|
- bleCallback.onChanged(wD);
|
|
|
- } catch (Exception e) {
|
|
|
- LogUtils.e(e.getMessage());
|
|
|
- }
|
|
|
- } else {
|
|
|
- bleCallback.onChanged(weight);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- }
|
|
|
- BleManager.getInstance().notify(bleDevice, SERVICE_UUID, CHARACTERISTIC_UUID, bleNotifyCallback);
|
|
|
- } else {
|
|
|
- if (null != bleCallback) {
|
|
|
- Exception exception = new Exception("ble_gatt_service is not found");
|
|
|
- LogUtils.d("蓝牙", exception.getMessage());
|
|
|
- bleCallback.onNotifyFailure(exception);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (null != bleCallback) {
|
|
|
- Exception exception = new Exception("ble_gatt_service is null");
|
|
|
- LogUtils.d("蓝牙", exception.getMessage());
|
|
|
- bleCallback.onNotifyFailure(exception);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (null != bleCallback) {
|
|
|
- Exception exception = new Exception("ble_gatt_service is not found");
|
|
|
- LogUtils.d("蓝牙", exception.getMessage());
|
|
|
- bleCallback.onNotifyFailure(exception);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (null != bleCallback) {
|
|
|
- Exception exception = new Exception("ble_service is null");
|
|
|
- LogUtils.d("蓝牙", exception.getMessage());
|
|
|
- bleCallback.onNotifyFailure(exception);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
|
|
- LogUtils.e(isActiveDisConnected, device.getMac(), device.getName(), status);
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- if (null != bleCallback) {
|
|
|
- Exception exception = new Exception("ble_device is not found");
|
|
|
- LogUtils.d("蓝牙", exception.getMessage());
|
|
|
- bleCallback.onNotifyFailure(exception);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- LogUtils.e(Log.getStackTraceString(e));
|
|
|
- if (null != bleCallback) {
|
|
|
- LogUtils.d("蓝牙", e.getMessage());
|
|
|
- bleCallback.onNotifyFailure(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- }
|
|
|
- BleManager.getInstance().scan(bleScanCallback);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public synchronized void disconnect() {
|
|
|
- try {
|
|
|
- BleManager.getInstance().removeNotifyCallback(bleDevice, CHARACTERISTIC_UUID);
|
|
|
- BleManager.getInstance().disconnect(bleDevice);
|
|
|
- } catch (Exception e) {
|
|
|
- LogUtils.e(Log.getStackTraceString(e));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+//package xn.hxp.utils.bluetooth;
|
|
|
+//
|
|
|
+//import android.bluetooth.BluetoothGatt;
|
|
|
+//import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
+//import android.bluetooth.BluetoothGattService;
|
|
|
+//import android.util.Log;
|
|
|
+//
|
|
|
+//import com.blankj.utilcode.util.AppUtils;
|
|
|
+//import com.blankj.utilcode.util.LogUtils;
|
|
|
+//import com.blankj.utilcode.util.SPUtils;
|
|
|
+//import com.clj.fastble.BleManager;
|
|
|
+//import com.clj.fastble.callback.BleGattCallback;
|
|
|
+//import com.clj.fastble.callback.BleNotifyCallback;
|
|
|
+//import com.clj.fastble.callback.BleScanCallback;
|
|
|
+//import com.clj.fastble.data.BleDevice;
|
|
|
+//import com.clj.fastble.data.BleScanState;
|
|
|
+//import com.clj.fastble.exception.BleException;
|
|
|
+//
|
|
|
+//import java.nio.charset.StandardCharsets;
|
|
|
+//import java.util.List;
|
|
|
+//
|
|
|
+//public enum BleTool {
|
|
|
+// INSTANCE;
|
|
|
+//
|
|
|
+// private final String SERVICE_UUID = "0000fff0-0000-1000-8000-00805f9b34fb";
|
|
|
+// private final String CHARACTERISTIC_UUID = "0000fff1-0000-1000-8000-00805f9b34fb";
|
|
|
+//
|
|
|
+// private BleScanCallback bleScanCallback;
|
|
|
+// private BleCallback bleCallback;
|
|
|
+// private BleDevice bleDevice;
|
|
|
+// private BleNotifyCallback bleNotifyCallback;
|
|
|
+//
|
|
|
+// private String weight = "";
|
|
|
+// private long lastWeightTime = 0;
|
|
|
+//
|
|
|
+// public interface BleCallback {
|
|
|
+// void onSuccess();
|
|
|
+//
|
|
|
+// void onNotifyFailure(Exception exception);
|
|
|
+//
|
|
|
+// void onChanged(String data);
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void setBleCallback(BleCallback bleCallback) {
|
|
|
+// weight = "";
|
|
|
+// this.bleCallback = bleCallback;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public synchronized void connect() {
|
|
|
+// if (BleManager.getInstance().getScanSate() != BleScanState.STATE_SCANNING) {
|
|
|
+// if (null == bleScanCallback) {
|
|
|
+// bleScanCallback = new BleScanCallback() {
|
|
|
+// @Override
|
|
|
+// public void onScanFinished(List<BleDevice> scanResultList) {
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onScanStarted(boolean success) {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// LogUtils.d("蓝牙", success);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onScanning(BleDevice bleDevice) {
|
|
|
+// try {
|
|
|
+// String bleMac = SPUtils.getInstance().getString("bleMac", "");
|
|
|
+// LogUtils.d("蓝牙", bleMac);
|
|
|
+// if (bleDevice.getMac().equals(bleMac)) {
|
|
|
+// BleManager.getInstance().cancelScan();
|
|
|
+// BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
|
|
|
+// @Override
|
|
|
+// public void onStartConnect() {
|
|
|
+// LogUtils.d("蓝牙", "开始连接", bleDevice.getName(), bleDevice.getMac());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onConnectFail(BleDevice bleDevice, BleException exception) {
|
|
|
+// LogUtils.e(bleDevice.getMac(), bleDevice.getName(), exception.getDescription());
|
|
|
+// if (null != bleCallback) {
|
|
|
+// LogUtils.d("蓝牙", exception.getDescription());
|
|
|
+// bleCallback.onNotifyFailure(new Exception(exception.getDescription()));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
|
|
|
+// BleTool.INSTANCE.bleDevice = bleDevice;
|
|
|
+// LogUtils.d("蓝牙", "连接成功", bleDevice.getName(), bleDevice.getMac());
|
|
|
+// if (null != gatt) {
|
|
|
+// List<BluetoothGattService> bluetoothGattServiceList = gatt.getServices();
|
|
|
+// if (null != bluetoothGattServiceList) {
|
|
|
+// BluetoothGattService bluetoothGattService = null;
|
|
|
+// for (int i = 0; i < bluetoothGattServiceList.size(); i++) {
|
|
|
+// BluetoothGattService gattService = bluetoothGattServiceList.get(i);
|
|
|
+// if (SERVICE_UUID.equals(gattService.getUuid().toString())) {
|
|
|
+// bluetoothGattService = gattService;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (null != bluetoothGattService) {
|
|
|
+// List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = bluetoothGattService.getCharacteristics();
|
|
|
+// if (null != bluetoothGattCharacteristicList) {
|
|
|
+// BluetoothGattCharacteristic bluetoothGattCharacteristic = null;
|
|
|
+// for (int i = 0; i < bluetoothGattCharacteristicList.size(); i++) {
|
|
|
+// BluetoothGattCharacteristic gattCharacteristic = bluetoothGattCharacteristicList.get(i);
|
|
|
+// if (null != gattCharacteristic) {
|
|
|
+// if (CHARACTERISTIC_UUID.equals(gattCharacteristic.getUuid().toString())) {
|
|
|
+// bluetoothGattCharacteristic = gattCharacteristic;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (bluetoothGattCharacteristic != null) {
|
|
|
+// if (null == bleNotifyCallback) {
|
|
|
+// bleNotifyCallback = new BleNotifyCallback() {
|
|
|
+// @Override
|
|
|
+// public void onNotifySuccess() {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// bleCallback.onSuccess();
|
|
|
+// }
|
|
|
+// LogUtils.d("蓝牙", "订阅成功");
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onNotifyFailure(BleException exception) {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// bleCallback.onNotifyFailure(new Exception(exception.getDescription()));
|
|
|
+// }
|
|
|
+// LogUtils.e("蓝牙", "订阅失败", exception);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onCharacteristicChanged(byte[] data) {
|
|
|
+// String string = new String(data, StandardCharsets.UTF_8);
|
|
|
+// String[] split = string.split("\n", -1);
|
|
|
+// if (AppUtils.isAppDebug()) {
|
|
|
+// LogUtils.d("蓝牙", "处理前的蓝牙数据", data, split);
|
|
|
+// }
|
|
|
+// if (null != bleCallback) {
|
|
|
+// if (split.length > 0) {
|
|
|
+// String line = split[0].trim();
|
|
|
+// if (!weight.equals(line) || (System.currentTimeMillis() - lastWeightTime > 3000)) {
|
|
|
+// lastWeightTime = System.currentTimeMillis();
|
|
|
+// weight = line;
|
|
|
+// LogUtils.d("蓝牙", "处理前的蓝牙数据", data, split);
|
|
|
+// if (weight.startsWith("=")) {
|
|
|
+// try {
|
|
|
+// String wD = new StringBuilder(weight.replace("=", "")).reverse().toString();
|
|
|
+// bleCallback.onChanged(wD);
|
|
|
+// } catch (Exception e) {
|
|
|
+// LogUtils.e(e.getMessage());
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// bleCallback.onChanged(weight);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// };
|
|
|
+// }
|
|
|
+// BleManager.getInstance().notify(bleDevice, SERVICE_UUID, CHARACTERISTIC_UUID, bleNotifyCallback);
|
|
|
+// } else {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// Exception exception = new Exception("ble_gatt_service is not found");
|
|
|
+// LogUtils.d("蓝牙", exception.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(exception);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// Exception exception = new Exception("ble_gatt_service is null");
|
|
|
+// LogUtils.d("蓝牙", exception.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(exception);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// Exception exception = new Exception("ble_gatt_service is not found");
|
|
|
+// LogUtils.d("蓝牙", exception.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(exception);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// Exception exception = new Exception("ble_service is null");
|
|
|
+// LogUtils.d("蓝牙", exception.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(exception);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
|
|
+// LogUtils.e(isActiveDisConnected, device.getMac(), device.getName(), status);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// } else {
|
|
|
+// if (null != bleCallback) {
|
|
|
+// Exception exception = new Exception("ble_device is not found");
|
|
|
+// LogUtils.d("蓝牙", exception.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(exception);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// LogUtils.e(Log.getStackTraceString(e));
|
|
|
+// if (null != bleCallback) {
|
|
|
+// LogUtils.d("蓝牙", e.getMessage());
|
|
|
+// bleCallback.onNotifyFailure(e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// };
|
|
|
+// }
|
|
|
+// BleManager.getInstance().scan(bleScanCallback);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public synchronized void disconnect() {
|
|
|
+// try {
|
|
|
+// BleManager.getInstance().removeNotifyCallback(bleDevice, CHARACTERISTIC_UUID);
|
|
|
+// BleManager.getInstance().disconnect(bleDevice);
|
|
|
+// } catch (Exception e) {
|
|
|
+// LogUtils.e(Log.getStackTraceString(e));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|