lezilisy 2021-01-24 23:27 采纳率: 42.9%
浏览 814

蓝牙连接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的理解就不是很明白,如果有语法什么的低级错误也欢迎指出来,能够解决我的问题最好了

  • 写回答

1条回答 默认 最新

  • 秃头批风侠 2023-05-19 13:11
    关注

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

    评论 编辑记录

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号