簡體   English   中英

Android藍牙與遠程設備連接

[英]android bluetooth connect with remote device

我想從我的Android設備中的應用連接到遠程設備(已配對)。 遠程設備是模塊HC-05。 我的鱈魚是:

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;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server
            // code
            tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
        }
        mmSocket = tmp;
    }

    @Override
    public void run() {
        // Cancel discovery because it will slow down the connection
        ba.cancelDiscovery();

        try {
            mmSocket.connect();
        } catch (IOException e) {}

        // Do work to manage the connection (in a separate thread)
        // manageConnectedSocket(mmSocket);
        // connected();
        tv1.setText("connect");

    }

    /** Will cancel an in-progress connection, and close the socket */
    @SuppressWarnings("unused")
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }
}

但是在mSocket.connect()行中出現錯誤。 當運行我的應用程序時,出現消息:不幸的是(應用程序名稱)已停止。

請幫忙。

不要默默地忽略IOException 如果得到IOException ,則意味着您。 由於任何原因,無法創建套接字。 然后,mSocket保持為空,因此您將獲得異常。 可能是您清單中沒有藍牙許可。

暫無
暫無

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

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