|
|
@ -28,6 +28,7 @@ import androidx.core.app.ActivityCompat; |
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
import com.iflytop.profilometer.core.migration.channel.BleDeviceUartChannel; |
|
|
|
import com.iflytop.profilometer.core.system.SystemService; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
@ -54,7 +55,7 @@ public class BleManager { |
|
|
|
|
|
|
|
//【通信部分】请根据你的设备替换下列 UUID |
|
|
|
public static final UUID SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); |
|
|
|
public static final UUID WRITE_CHARACTERISTIC_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb"); |
|
|
|
public static final UUID WRITE_CHARACTERISTIC_UUID = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"); |
|
|
|
public static final UUID NOTIFY_CHARACTERISTIC_UUID = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); |
|
|
|
public static final UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); |
|
|
|
|
|
|
@ -149,7 +150,7 @@ public class BleManager { |
|
|
|
// 收到通知数据后,将数据通过回调接口传递出去 |
|
|
|
if (NOTIFY_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { |
|
|
|
byte[] data = characteristic.getValue(); |
|
|
|
Log.d(TAG, "收到通知数据: " + new String(data)); |
|
|
|
// Log.d(TAG, "收到通知数据: " + new String(data)); |
|
|
|
if (bleDataListener != null) { |
|
|
|
bleDataListener.onDataReceived(data); |
|
|
|
} |
|
|
@ -191,6 +192,7 @@ public class BleManager { |
|
|
|
} |
|
|
|
return instance; |
|
|
|
} |
|
|
|
|
|
|
|
public static synchronized BleManager getInstance() { |
|
|
|
return instance; |
|
|
|
} |
|
|
@ -351,7 +353,7 @@ public class BleManager { |
|
|
|
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress); |
|
|
|
bluetoothGatt = device.connectGatt(context, false, gattCallback); |
|
|
|
|
|
|
|
BleDeviceUartChannel channel = new BleDeviceUartChannel(); |
|
|
|
BleDeviceUartChannel channel = SystemService.getInstance().getChannel(); |
|
|
|
channel.init(); |
|
|
|
channel.setDeviceOnReportListener(new BleDeviceListener()); |
|
|
|
|
|
|
@ -362,11 +364,9 @@ public class BleManager { |
|
|
|
|
|
|
|
/** |
|
|
|
* 向当前已连接的 BLE 设备发送数据。 |
|
|
|
* |
|
|
|
* @param message 要发送的字符串数据(根据设备协议,可转换为其他格式) |
|
|
|
*/ |
|
|
|
@SuppressLint("MissingPermission") |
|
|
|
public void sendMessage(String message) { |
|
|
|
public void sendMessage(byte[] rawpacket) { |
|
|
|
if (bluetoothGatt == null) { |
|
|
|
Log.e(TAG, "没有连接的设备,无法发送消息"); |
|
|
|
return; |
|
|
@ -381,7 +381,7 @@ public class BleManager { |
|
|
|
Log.e(TAG, "未发现写特征,请确认写特征 UUID 是否正确"); |
|
|
|
return; |
|
|
|
} |
|
|
|
writeCharacteristic.setValue(message.getBytes()); |
|
|
|
writeCharacteristic.setValue(rawpacket); |
|
|
|
boolean result = bluetoothGatt.writeCharacteristic(writeCharacteristic); |
|
|
|
if (!result) { |
|
|
|
Log.e(TAG, "调用 writeCharacteristic 失败"); |
|
|
|