//package xn.hxp.utils.bluetooth;
//
//import android.annotation.SuppressLint;
//import android.bluetooth.BluetoothAdapter;
//import android.bluetooth.BluetoothDevice;
//import android.bluetooth.BluetoothManager;
//import android.bluetooth.BluetoothSocket;
//import android.util.Log;
//
//import com.blankj.utilcode.util.LogUtils;
//import com.blankj.utilcode.util.Utils;
//
//import java.io.InputStream;
//import java.nio.charset.StandardCharsets;
//import java.util.Set;
//import java.util.UUID;
//
//import xn.hxp.utils.Tool;
//
//public enum BluetoothTool {
//    INSTANCE;
//    BluetoothAdapter bluetoothAdapter;
//    private UUID uuid;
//    private byte[] buffer = new byte[1024];
//    private BluetoothDevice bluetoothDevice;
//    private BluetoothSocket bluetoothSocket;
//    private InputStream inputStream;
//
//    private BluetoothTool() {
//        uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//        BluetoothManager bluetoothManager = Utils.getApp().getSystemService(BluetoothManager.class);
//        bluetoothAdapter = bluetoothManager.getAdapter();
//    }
//
//    @SuppressLint("MissingPermission")
//    public void connect(BluetoothCallBack bluetoothCallBack) {
//        if (!bluetoothAdapter.isEnabled()) {
//            Tool.INSTANCE.setBluetooth(true);
//        }
//        bluetoothDevice = getWeighDevice();
//        if (null == bluetoothDevice) {
//            bluetoothCallBack.connectFailed("未找到蓝牙称");
//            return;
//        }
//        try {
//            bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
//            LogUtils.d("连接蓝牙前");
//            bluetoothSocket.connect();
//            inputStream = bluetoothSocket.getInputStream();
//            int bytesRead;
//            String weight = "";
//            LogUtils.d("读取蓝牙数据前");
//            while (null != bluetoothSocket && bluetoothSocket.isConnected() && null != inputStream && (bytesRead = inputStream.read(buffer)) != -1) {
//                // 处理读取的数据
//                String data = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
//                String[] split = data.split("\n", -1);
//                LogUtils.d("处理前的蓝牙数据", data, split);
//                if (split.length > 0) {
//                    String line = split[0].trim();
//                    if (!weight.equals(line)) {
//                        weight = line;
//                        bluetoothCallBack.callBack(weight);
//                    }
//                }
//                Thread.sleep(5);
//            }
//        } catch (Exception e) {
//            disconnect();
//            LogUtils.e(Log.getStackTraceString(e));
//        }
//    }
//
//    public void disconnect() {
//        if (null != bluetoothDevice) {
//            bluetoothDevice = null;
//        }
//        if (null != bluetoothSocket && bluetoothSocket.isConnected()) {
//            try {
//                inputStream.close();
//                inputStream = null;
//                bluetoothSocket.close();
//                bluetoothSocket = null;
//            } catch (Exception e) {
//                LogUtils.e(Log.getStackTraceString(e));
//            }
//        }
//    }
//
//    public interface BluetoothCallBack {
//        void callBack(String weight);
//
//        void connectFailed(String msg);
//    }
//
//    @SuppressLint("MissingPermission")
//    private BluetoothDevice getWeighDevice() {
//        if (null != bluetoothDevice) {
//            return bluetoothDevice;
//        }
//        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
//        if (!pairedDevices.isEmpty()) {
//            // There are paired devices. Get the name and address of each paired device.
//            for (BluetoothDevice device : pairedDevices) {
//                String deviceName = device.getName();
//                if ("HF-18".equals(deviceName)) {
//                    return device;
//                }
//            }
//        }
//        return null;
//    }
//}
