Java上的蓝牙连接异常

iugsix8n  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(127)

我想建立蓝牙连接。我可以扫描,但不能配对设备。我得到这样的错误“读取失败,套接字可能关闭或超时”。
在其他标题中写到使用“createRfcommSocketToServiceRecord”而不是“createInsecureRfcommSocketToServiceRecord”,然而,这并不是解决方案。

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(!mBTAdapter.isEnabled()) {
            Toast.makeText(getBaseContext(), getString(R.string.BTnotOn), Toast.LENGTH_SHORT).show();
            return;
        }

        mBluetoothStatus.setText(getString(R.string.cConnet));
        // Get the device MAC address, which is the last 17 chars in the View
        String info = ((TextView) view).getText().toString();
        final String address = info.substring(info.length() - 17);
        final String name = info.substring(0,info.length() - 17);

        // Spawn a new thread to avoid blocking the GUI one
        new Thread()
        {
            @Override
            public void run() {
                boolean fail = false;

                try {
                    mBTSocket = createBluetoothSocket(device);
                } catch (IOException e) {
                    fail = true;
                    Toast.makeText(getBaseContext(), getString(R.string.ErrSockCrea), Toast.LENGTH_SHORT).show();
                }
                // Establish the Bluetooth socket connection.
                try {
                    mBTSocket.connect();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                    try {
                        fail = true;
                        mBTSocket.close();
                        mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
                                .sendToTarget();
                    } catch (IOException e2) {
                        //insert code to deal with this
                        Toast.makeText(getBaseContext(), getString(R.string.ErrSockCrea), Toast.LENGTH_SHORT).show();
                    }
                }
                if(!fail) {
                    mConnectedThread = new ConnectedThread(mBTSocket, mHandler);
                    mConnectedThread.start();

                    mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
                            .sendToTarget();
                }
            }
        }.start();
    }
};

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
    try {
        final Method m = device.getClass().getMethod("createRfcommSocketToServiceRecord", UUID.class); 
        return (BluetoothSocket) m.invoke(device, BT_MODULE_UUID);
    } catch (Exception e) {
        Log.e(TAG, "Could not create Insecure RFComm Connection",e);
    }
    return  device.createRfcommSocketToServiceRecord(BT_MODULE_UUID);
}
xuo3flqw

xuo3flqw1#

验证码:

if(!fail) {
    mConnectedThread = new ConnectedThread(mBTSocket, mHandler);
    mConnectedThread.start();
    mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name).sendToTarget();
    mBTSocket.close();
}

相关问题