bt_btif_sock_rfcomm:find_rfc_slot_by_id无法找到RFCOMM插槽ID

时间:2017-01-18 08:27:21

标签: java android bluetooth rfcomm

连接blutooth RfComm时遇到问题。我能够连接到蓝牙并进行通信,但有时它无法连接。我使用的是标准uuid-00001101-0000-1000-8000-00805F9B34FB

请帮帮我:)。

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

    private ConnectThread(BluetoothDevice device) {
        Log.d(TAG, "in ConnectThread");
        // 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 {
            Log.d(TAG, "createRfcommSocketToServiceRecord for " + mmDevice.getName());
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
            //tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.d(TAG, "in ConnectThread : run");
        // Cancel discovery because it will slow down the connection
        bluetoothAdapter.cancelDiscovery();

        try {
            Log.d(TAG, "Try to Connect Socket");
            // Connect the device through the socket. This will block
            mmSocket.connect();
        } catch (IOException connectException) {

            try {
                Log.i(TAG, "Trying fallback...");
                mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1);
                mmSocket.connect();
            } catch (Exception e2) {
                Log.e(TAG, "Couldn't establish Bluetooth connection!");
                try {
                    mmSocket.close();
                } catch (IOException e3) {
                    Log.e(TAG, "unable to close()  socket during connection failure", e3);
                }
                return;
            }
            // Do work to manage the connection (in a separate thread)
            mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
        }
    }

    // Will cancel an in-progress connection, and close the socket
    private void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class ConnectedThread extends Thread implements Runnable {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "in ConnectedThread");
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        Log.d(TAG, "in ConnectedThread : run");
        byte[] buffer;  // buffer store for the stream
        int bytes; // bytes returned from read()
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            //Log.d(TAG,"in ConnectedThread : run -- while");
            try {
                try {
                    sleep(25);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // Read from the InputStream
                buffer = new byte[1024];
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }
  

01-18 09:19:09.661 15692-16231/iconcreator.pidroid I/Main Activity :: Trying fallback...
01-18 09:19:09.661 2809-2873/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 14
01-18 09:19:09.662 15692-16231/iconcreator.pidroid W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
01-18 09:19:09.743 16236-16236/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-18 09:19:09.846 2809-2859/? W/bt_rfcomm: port_rfc_closed RFCOMM connection in state 1 closed: Closed (res: 19)
01-18 09:19:09.846 15692-16231/iconcreator.pidroid E/Main Activity :: Couldn't establish Bluetooth connection!
01-18 09:19:09.847 2809-2873/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 15

0 个答案:

没有答案