te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Android- implemented Bluetooth connection code but does not work on other Android device - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android- implemented Bluetooth connection code but does not work on other Android device - Stack Overflow

programmeradmin5浏览0评论

I am working on an Android app that connects to a Bluetooth device using BluetoothSocket. However, when I attempt to connect, I get the following error: it works when i try to connect with laptop's bluetooth from but fail when try connect with another android device(redmi a2 (Androdi os 13))

Attempting to connect... Attempt 3
connect() for device XX:XX:XX:XX:69:7D called by pid: 5069
read failed, socket might be closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:1079)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:588)
at com.sample.BluetoothUtil$ConnectThread.run(BluetoothUtil.kt:156)

Current Implementation

Here is the ConnectThread I am using to establish the Bluetooth connection:

private inner class ConnectThread(val device: BluetoothDevice?) : Thread() {
private val connectionUUID = device?.getUuids()?.get(0)?.getUuid()
private var bluetoothSocket: BluetoothSocket? = null

override fun run() {
    dataObjects.bluetoothAdapter?.cancelDiscovery()

    bluetoothSocket = device?.createInsecureRfcommSocketToServiceRecord(connectionUUID)
    bluetoothSocket?.let { socket ->
        try {
            val maxRetries = 3
            var attemptCount = 0

            while (!connectionAttempted && attemptCount < maxRetries) {
                try {
                    Log.d("BLUETOOTH_CONNECT", "Attempting to connect... Attempt ${attemptCount + 1}")
                    isConnecting = true

                    // Wait for bonding to complete if in progress
                    while (device?.bondState == BluetoothDevice.BOND_BONDING) {
                        Thread.sleep(100)
                    }

                    socket.connect()
                    connectionAttempted = true
                    Log.d("BLUETOOTH_CONNECT", "Connection successful")
                    showToast("Connected to ${device?.name ?: "device"}")
                    return
                } catch (e: IOException) {
                    Log.e("BLUETOOTH_CONNECT", "Connection attempt failed", e)
                    attemptCount++
                    isConnecting = false
                    if (attemptCount < maxRetries) {
                        Thread.sleep(1000)
                    }
                }
            }

            if (!connectionAttempted) {
                Log.e("BLUETOOTH_CONNECT", "Failed to connect after $maxRetries attempts")
                showToast("Failed to connect after several attempts")
                cancel()
            }
        } catch (e: Exception) {
            Log.e("BLUETOOTH_CONNECT", "Connection error", e)
            showToast("Connection error: ${e.localizedMessage}")
            isConnecting = false
            cancel()
        }
    } ?: run {
        Log.e("BLUETOOTH_CONNECT", "Socket is null")
        showToast("Error: Could not create Bluetooth socket")
    }
}

fun cancel() {
    try {
        isConnecting = false
        connectionAttempted = false
        bluetoothSocket?.close()
    } catch (e: Exception) {
        Log.e("BLUETOOTH_CONNECT", "Could not close socket", e)
    }
}
}

fun connectToDevice(device: BluetoothDevice?) {
var connectThread: ConnectThread? = null
connectThread?.cancel()
connectThread = ConnectThread(device)
connectThread.start()
}
发布评论

评论列表(0)

  1. 暂无评论