簡體   English   中英

Android BluetoothSocket無法連接

[英]Android BluetoothSocket can't connect

我正在用android上的藍牙做一些事情,我想連接到發現的設備之一,並向它打開套接字連接。

我已授予所有必需的權限:Bluetooth,Bluetooth_Admin,Access_Fine_Location和Access_Coarse_Location,並在我對藍牙進行任何操作之前先詢問它們。

現在,我發現了一些帶有adapter.startDiscovery();設備adapter.startDiscovery(); activity.registerReceiver(receiver, filter);

在接收器中找到某個名稱的設備,我嘗試像這樣連接到該設備:

            adapter.cancelDiscovery();
            Log.d(TAG, "Create Bond");
            device.createBond();
            try {
                socket = device.createRfcommSocketToServiceRecord(uuid);
                Log.d(TAG, "Sleep 10");
                sleep(10000);
                Log.d(TAG, "Create Socket");
                //socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
                Log.d(TAG, "Connect socket");
                socket.connect();
                Log.d(TAG, "Connecting Done");
            } catch (Exception e) {
                Log.d(TAG, "Failed to connect to device", e);
                try {
                    socket.close();
                } catch (Exception e2) {
                    Log.d(TAG, "Failed to close socket", e2);
                }
            }

這是一個測試代碼,我正在嘗試使用該代碼創建一個套接字並打開一個連接。

我在.connect()上收到以下異常:

java.io.IOException:讀取失敗,套接字可能關閉或超時,讀取ret:android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:684)-1在android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:696)在android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:373)

我究竟做錯了什么。

我連接的藍牙設備是Android移動設備,但是我計划在設法獲得連接時使用其他設備。

Update1:​​Android版本為7.0

使用fetchUuidsWithSdp()getUuids()查找所有已發布的服務及其關聯的UUID值。

您不需要調用device.createBond(); 連接到藍牙設備。

嘗試刪除此行。 另外,請檢查您的手機是否尚未與您要連接的設備配對。 您可以在“藍牙設置”屏幕上進行檢查(長按智能手機上的“藍牙”圖標將其打開。

這是啟動藍牙連接的示例代碼:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket
        // because mmSocket is final.
        BluetoothSocket tmp = null;
        mmDevice = device;

        try {
            // Get a BluetoothSocket to connect with the given BluetoothDevice.
            // MY_UUID is the app's UUID string, also used in the server code.
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "Socket's create() method failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it otherwise slows down the connection.
        bluetoothAdapter.cancelDiscovery();

        try {
            // Connect to the remote device through the socket. This call blocks
            // until it succeeds or throws an exception.
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and return.
            try {
                mmSocket.close();
            } catch (IOException closeException) {
                Log.e(TAG, "Could not close the client socket", closeException);
            }
            return;
        }

        // The connection attempt succeeded. Perform work associated with
        // the connection in a separate thread.
        manageMyConnectedSocket(mmSocket);
    }

    // Closes the client socket and causes the thread to finish.
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "Could not close the client socket", e);
        }
    }
}

該代碼來自Android官方文檔: https//developer.android.com/guide/topics/connectivity/bluetooth#ConnectAsAClient

我寫了另一個代碼,而不是我在服務器端使用的代碼。

            Log.d(TAG,"Start server");
            BluetoothServerSocket serverSocket = null;
            try {
                serverSocket = adapter.listenUsingRfcommWithServiceRecord("ime", uuid);
            } catch (Exception e) {
                e.printStackTrace();
            }

            while (true) {
                try {
                    serverSocket.accept();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }

我在啟動線程的內部使用了此代碼,而不是從問題中調用代碼。

在一個應用程序上使用服務器代碼安裝該應用程序並在套接字上調用“ connect”就可以了。 我使用了相同的UUID(上一個是隨機生成的,新的是從字符串中靜態生成的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM