简体   繁体   中英

Why I am able to close socket but not able to connect to device in Android?

I am able to pair device with phone on the app using Android Studio, but I get:

D/tag: CouldNotConnectToSocket

And later down I get:

D/BluetoothSocket: close()

Here is the section of code dealing with connecting to socket and closing it.

public void run()
{
    try
    {
        BTSocket = device.createRfcommSocketToServiceRecord(BTMODULEUUID);
        BA.cancelDiscovery();
        BTSocket.connect();
        mHandler.sendEmptyMessage(0);
    }
    catch (IOException eConnectException)
    {
        Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
        closeSocket(BTSocket);
        return;
    }
}

private void closeSocket(BluetoothSocket nOpenSocket)
{
    try
    {
        nOpenSocket.close();
        Log.d(TAG, "SocketClosed");
    }
    catch (IOException ex)
    {
        Log.d(TAG, "CouldNotCloseSocket");
    }
}

private Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        //mBluetoothConnectProgressDialog.dismiss();
        Toast.makeText(MainActivity4.this, "DeviceConnected", 5000).show();
    }
};

Do I need some kind of delay, or have BTSocket.connect() in some kind of thread?

The issue I found out was that the devices I was trying to connect to start sending/receiving data require BLE implementation in the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM