BluetoothSPPLibrary icon indicating copy to clipboard operation
BluetoothSPPLibrary copied to clipboard

Connect with address

Open ALAxHxC opened this issue 10 years ago • 1 comments

What are the reason for connect directly dont work with the device? i Try with bt = new BluetoothSPP(this); bt.setupService() ; bt.startService(BluetoothState.DEVICE_OTHER); bt.connect("F4:B8:5E:8C:BB:11");

Note: the device is not paried and already define ReceivedListener , ConnectionListener

ALAxHxC avatar Dec 01 '15 15:12 ALAxHxC

I encountered with this problem. The first time connect failed, the second time succeeded. This situation will happened when device is already connected, but your application is dead or not started. When application starting, it will start a connection, but actually the connection is already built up.

I fix it by connecting it again. Just modify BluetoothService->ConnectThread.run().

        public void run() {
            // Always cancel discovery because it will slow down a connection
            mAdapter.cancelDiscovery();

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                }
                //try again
                try {
                    mmSocket.connect();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    connectionFailed();
                }
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice, mSocketType);
        }

Wish it helps.

qq20160110-0 2x

waylife avatar Jan 09 '16 21:01 waylife