//package xn.hxp.blu
//
//import android.bluetooth.BluetoothAdapter
//import android.bluetooth.BluetoothDevice
//import android.bluetooth.BluetoothSocket
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.withContext
//import java.io.IOException
//import java.util.*
//
//object BluetoothConnectionManager {
//    private val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
//    private val uuid: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") // SPP UUID
//    private const val connectionTimeoutMillis: Long = 3000 // 连接超时时间3秒钟
////    suspend fun getDeviceAddress(deviceName: String): String? {
//    suspend fun getDeviceAddress(): String? {
//        return withContext(Dispatchers.IO) {
//            val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
////            pairedDevices?.find { it.name == deviceName }?.address
//            pairedDevices?.find { it.name == "HF-18" }?.address
//        }
//    }
//
//
//    suspend fun connectToDevice(deviceAddress: String): BluetoothSocket? {
//        val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
//        val device: BluetoothDevice? = bluetoothAdapter?.getRemoteDevice(deviceAddress)
//
//        return withContext(Dispatchers.IO) {
//            var socket: BluetoothSocket? = null
//            val thread = Thread {
//                try {
//                    socket = device?.createRfcommSocketToServiceRecord(uuid)
//                    socket?.connect()
//                } catch (e: IOException) {
//                    e.printStackTrace()
//                    try {
//                        socket?.close()
//                    } catch (closeException: IOException) {
//                        closeException.printStackTrace()
//                    }
//                    socket = null
//                }
//            }
//
//            thread.start()
//            try {
//                thread.join(connectionTimeoutMillis)
//            } catch (e: InterruptedException) {
//                e.printStackTrace()
//            }
//
//            if (thread.isAlive) {
//                socket?.close()
//                thread.interrupt()
//                socket = null
//            }
//
//            socket  // 返回连接成功或为null（失败或超时）
//        }
//    }
//
//
//}