简体   繁体   English

为什么我能够关闭套接字但无法连接到 Android 中的设备?

[英]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:我可以使用 Android Studio 在应用程序上将设备与手机配对,但我得到:

D/tag: CouldNotConnectToSocket D/标签:CouldNotConnectToSocket

And later down I get:后来我得到:

D/BluetoothSocket: close() D/蓝牙套接字:关闭()

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?我需要某种延迟,还是在某种线程中有BTSocket.connect()

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.我发现的问题是我尝试连接以开始发送/接收数据的设备需要在应用程序中实现 BLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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