蓝牙连接BluetoothSocket.connect()总是不成功怎么解决?

本人小白,刚学习蓝牙开发。我已经实现了蓝牙配对,对配对的设备进行连接,已经关闭了扫描而且利用了子线程进行连接,但还是不行。我用手机配对上了电脑,想把手机当做一个服务端就接收数据,但是bluetoothSocket.connect()没有用,请各位大佬看一下问题在哪。

我的活动

public class ThirdActivity extends AppCompatActivity {
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice bluetoothDevice;
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        ButterKnife.bind(this);
        //之前已经配对上了,获取到地址后接下来要对设备进行连接
        Intent intent=getIntent();
        String address = intent.getStringExtra("address");
        bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        bluetoothDevice=bluetoothAdapter.getRemoteDevice(address);
        textView5.setText("正在与"+bluetoothDevice.getName()+"进行连接。。。");
        //按照一些例子写的连接子线程
        ConnectThread connectThread = new ConnectThread(bluetoothDevice);
        connectThread.start();
        //按照一些例子写的接收服务子线程,但是也失败了
//        AcceptThread acceptThread=new AcceptThread(bluetoothAdapter);
//        acceptThread.start();
}

按照网上的例子写的连接子线程

public class ConnectThread extends Thread{
    private  BluetoothSocket bluetoothSocket;
    private  BluetoothDevice bluetoothDevice;
    //这条是蓝牙串口通用的UUID
    private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public ConnectThread(BluetoothDevice device) {
        this.bluetoothDevice=device;
        try{
        //图省事就直接获取了            this.bluetoothSocket=bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
            Log.e("connectthread", "获取socket");
        }catch (IOException e){
            e.printStackTrace();
            bluetoothSocket=null;
        }
    }
    @Override
    public void run(){
        //连接不成功,就一直循环
        while(bluetoothSocket!=null) {
            try {
                bluetoothSocket.connect();
                Log.e("connectthread", "连接成功");
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("connectthread", "连接失败");
            }

            if(bluetoothSocket.isConnected()){
                try {
                    bluetoothSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    
    public void cancel() {
        try {
            bluetoothSocket.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

按照网上的例子写的接收连接线程——用了也失败了

public class AcceptThread extends Thread{
    private  BluetoothServerSocket bluetoothServerSocket;
    private BluetoothSocket bluetoothSocket=null;
    //这条是蓝牙串口通用的UUID
    private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public AcceptThread(BluetoothAdapter bluetoothAdapter) {
        bluetoothServerSocket=null;
        try {
            this.bluetoothServerSocket=bluetoothAdapter.listenUsingRfcommWithServiceRecord("accept",uuid);
            Log.e("accept", "获取服务socket");
        }catch (IOException e){
            e.printStackTrace();
            Log.e("accept", "未获取服务socket");
        }
    }
    @Override
    public void run() {
        while (bluetoothServerSocket!=null) {
            try {
                bluetoothSocket = bluetoothServerSocket.accept();
                Log.e("accept","服务已连接" );
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("accept", "服务未连接");
            }
            if (bluetoothSocket!= null) {
                try {
                    bluetoothServerSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    public void cancel() throws IOException {
        bluetoothServerSocket.close();
    }
}

我的logcat

说实话我对线程的理解以及socket的理解就不是很明白,如果有语法什么的低级错误也欢迎指出来,能够解决我的问题最好了

好像是权限的事,检查一下访问权限,好像是安卓高版本需要申请一些权限
android 蓝牙开发之权限_android 蓝牙权限_旭日初扬的博客-CSDN博客