【问题标题】:Android bluetooth socket connection not created未创建 Android 蓝牙套接字连接
【发布时间】:2014-11-25 17:47:55
【问题描述】:

我正在创建一个可以向蓝牙设备发送数据的应用程序。 我使用以下代码创建和连接套接字:

package com.example.bluetooth;

import java.io.IOException;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice bd = ba.getRemoteDevice("20:13:10:15:39:84");
        Toast.makeText(getApplicationContext(), bd.getName(), Toast.LENGTH_SHORT).show();
        BluetoothSocket bs = null;
        try{
            bs = bd.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }
        catch(IOException io){
            Toast.makeText(getApplicationContext(), "Socket Create : " + io.toString() , Toast.LENGTH_SHORT).show();
        }
        try{
            ba.cancelDiscovery();
            bs.connect();
        }
        catch(IOException io){
            Log.e("Socket Connect", io.toString());
            Toast.makeText(getApplicationContext(), "Socket Connect : " + io.toString() , Toast.LENGTH_LONG).show();
        }
    }

}

我的问题是套接字没有被连接。显示的消息是“java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).”

我使用的是 android 4.2 联想设备。

使用的蓝牙模块是HC-05,微控制器是Arduino-Uno。

我参考过类似的帖子,但没有一个可以解决我的问题。

提前致谢

【问题讨论】:

    标签: android sockets arduino android-bluetooth


    【解决方案1】:

    我已使用这段代码使我与蓝牙设备(即蓝牙打印机)的连接稳定。现在它在 10 次中连接 9.9 次。如果仍然发生一些错误,我以编程方式重置我的蓝牙再次调用这段代码,然后它在 10 次中连接 10 次。

    public boolean connectToPrinter(String printerName) throws IOException 
        {
            BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
    
            BluetoothDevice device = getPrinterByName(printerName);
    
            if (bluetoothSocket != null) 
            {
                bluetoothSocket.close();
            }
    
    
            try {
    
                Method m=device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
                bluetoothSocket=    (BluetoothSocket) m.invoke(device, 1);          
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    
            if (bluetoothSocket == null)
                return false;
    
            bluetoothSocket.connect();
    
            return true;
        }
    

    这里是getPrinterByName()的代码:

    private BluetoothDevice getPrinterByName(String printerName) 
        {
            Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    
            for (BluetoothDevice device : pairedDevices) 
            {
    
                Log.e("","device name: "+device.getName());
    
                if (device.getName() == null)
                    continue;
                if (device.getName().contains(printerName)) 
                {
                    remoteDevice = device;
                    //              pairPrinter(printerName);
                    return remoteDevice;
                }
            }
            return null;
        }
    

    【讨论】:

    • 我已经编辑了我的答案。我已经粘贴了链接的所有解决方案。如果您仍然有任何问题,请随时询问。如果喜欢我的帖子,请投票。谢谢。
    • 是的,现在看起来不错。
    【解决方案2】:

    我首先重新启动了我的平板电脑,然后尝试使用我的代码连接蓝牙套接字。我只需要使用:

    public void onStop(){
        super.onStop();
        mSocket.close();
        mOutputStream.close();
     }
    

    在最后。

    成功了!

    问题是我从来没有尝试过关闭套接字。

    【讨论】:

    • '问题是我从来没有尝试在最后关闭套接字。'。你的意思是你没有完成 mSocket.close() 或 mOutputStream.close() 吗?
    • 是的,这正是我的问题。
    • 嗯,我不明白这如何解决您的问题。我猜是 mSocket.close();你的意思是 bs.close(),但你需要 mOutputSteam 做什么?或者您的代码中有没有上面没有包含的部分?
    • 我相信他想说的是,当不再需要 BT 连接时,应用程序必须关闭它。如果 BT 套接字由于任何原因(例如应用程序崩溃或调试时应用程序重新启动)未关闭,则 BT 堆栈将保持连接打开,并且通常不允许与同一设备进行第二次连接。这种行为的效果是 BT 连接只会在“第一次”工作。重新启动 BT 设备或 Android BT 将“修复”此问题。我可以在装有 Android 4.0.4 的设备上重现此问题,而装有 Android 6.0.1 的 Nexus 5 似乎可以正确处理此问题。
    【解决方案3】:

    首先从您的手机设置中配对 arduino 蓝牙。然后尝试从您的应用程序连接到蓝牙。试试下面的代码:

        private BluetoothAdapter mBluetoothAdapter;
    private BluetoothDevice mBluetoothDevice;
    private BluetoothSocket mBluetoothSocket;
    private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    
        try{
    
            if(mBluetoothAdapter == null){
                Log.d("bluetooth:", "device does not support bluetooth");
            }
            if(!mBluetoothAdapter.isEnabled()){
                Intent enableBt = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                enableBt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(enableBt);
            }
    
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    

    连接:

       mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("xx:xx:xx:xx:xx:xx");
       try {
        mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
        mBluetoothSocket.connect();
        } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
        }
    

    【讨论】:

    • 我首先配对设备并尝试连接,然后它也不起作用。
    • 感谢您的帮助。
    猜你喜欢
    • 2014-03-27
    • 1970-01-01
    • 2012-12-12
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多