当前位置: 首页 > 知识库问答 >
问题:

蓝牙配对设备连接问题

万修然
2023-03-14

我在连接时遇到问题。起初它有效,而不是不起作用,除非我取消配对设备。我已经得到了所有可能发生的异常,套接字关闭,管道关闭,连接被拒绝,端口已经在使用中,等等。

我知道android 4.2之前版本的蓝牙存在问题(https://code.google.com/p/android/issues/detail?id=37725).

我在连接这些设备时遇到问题的设备:

  • Htc one(Android4.2)
  • 三星galaxy s2(Android4.1.2)
  • nexus 4(4.3)
  • 三星galaxy s4(4.2)

另一个小问题是,配对的设备不存储(主要在nexus 4和sgs2上)。

以下是我的代码:

private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //this is the other one that I've tried: fa87c0d0-afac-11de-8a39-0800200c9a66");

private static final String NAME = "BluetoothConnector";

public void listenForConnection() throws IOException, BluetoothException {
//first close the socket if it is open
closeSocket();

BluetoothServerSocket mServerSocket = null;
try {
    mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID_SECURE); //ioexception here!         
} catch (IOException e) {
    if (Build.VERSION.SDK_INT >= 9) {
        try { //this is a stupid hack, http://stackoverflow.com/questions/6480480/rfcomm-connection-between-two-android-devices
            Method m = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
            mServerSocket = (BluetoothServerSocket) m.invoke(mBluetoothAdapter, PORT);
        } catch (Exception ex) {
            Log.e(ex);
            throw e;
        }
    } else {
        throw e;
    }
}

while (!isCancelled) {
    try {
        socket = mServerSocket.accept();
    } catch (IOException e) {
        if (socket != null) {
            try {
                socket.close();
            } finally {
                socket = null;
            }
        }
        throw e;
    }

    if (socket == null) {
        throw new BluetoothException("Socket connection connected, but null");
    } else {
        isConnected = true;
        break; // everything is ok
    }
}
}



public void connect(String address) throws IOException, BluetoothException {
mBluetoothAdapter.cancelDiscovery();

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

try {
    socket = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
} catch (IOException e1) {
    Log.e(e1);

    if (Build.VERSION.SDK_INT >= 9) {
        try {
            Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
            socket = (BluetoothSocket) m.invoke(device, PORT);
        } catch (Exception e) {
            Log.e(e);
            throw e1;
        }
    } else {
        throw e1;
    }
}

// Make a connection to the BluetoothSocket
try {
    // This is a blocking call and will only return on a
    // successful connection or an exception
    socket.connect();
} catch (IOException e) {
    Log.e(e);
    // Close the socket
    try {
        socket.close();
    } catch (IOException e2) {
        Log.e(e2);
        Log.wtf("unable to close() socket during connection failure");
    }
    throw e;
}

}

private void closeSocket() {
    try {
        if (socket != null) {
            socket.close();
            socket = null;
            Log.d("Socket closed");
        }
    } catch (IOException e) {
        Log.e(e);
        Log.wtf("close() of connect socket failed");
    }
}

我尝试更改uuid(也是随机的),尝试查看旧的sdk样本。那么这里会有什么问题呢?

编辑:试图澄清:问题通常会出现,当2个设备已经配对,连接,做了一些成功的通信,断开连接(由用户)。之后,除非重新启动或手动取消配对,否则无法重新连接它们。

共有2个答案

谢麒
2023-03-14

似乎此时蓝牙在android上坏了。

没有一种可靠的方法可以连接两台设备,这种方法可以一直工作。一些人正在使用非官方的方式来做这件事,但这并不适用于所有设备。

我做了一些内部测试,使用目前市场上的前10款设备,所以在大约90次测试运行后,黑客方法在75%的时间里都有效,这还不够好。

例如,htc oneX将只处理传入的蓝牙请求,作为一个蓝牙免提设备(它连接成功!),但却无法发送消息。

在实现了完整的蓝牙功能后,我们决定将它从我们的应用程序中删除,并在发布时取消它。我们将在以后的版本中切换到wifi。

夔建章
2023-03-14

您正在尝试以这种方式配对:

private void TwitPairedDevice() {
    buttonTwitPairDevice.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Set<BluetoothDevice> fetchPairedDevices=bluetooth.getBondedDevices();
            Iterator<BluetoothDevice> iterator=fetchPairedDevices.iterator();
            while(iterator.hasNext())
            {
                final BluetoothDevice pairBthDevice=iterator.next();
                final String addressPairedDevice=pairBthDevice.getAddress();
                AsyncTask<Integer, Void, Void> asynchPairDevice=new AsyncTask<Integer, Void, Void>() {

                    @Override
                    protected Void doInBackground(Integer... params) {
                        try {
                            socket=pairBthDevice.createRfcommSocketToServiceRecord(uuid);
                            socket.connect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    }
                };asynchPairDevice.execute();
            }
        }
    });
}

连接Pired设备:

    private void FetchPairedDevices() {
        Set<BluetoothDevice> pairedDevices=bluetooth.getBondedDevices();
        for(BluetoothDevice pairedBthDevice:pairedDevices)
        {
            listPairedDevice.add(pairedBthDevice.getName());
        }
        listviewPairedDevice.setAdapter(adapterPairedDevice);
        listviewPairedDevice.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Object listPairedName=arg0.getItemAtPosition(arg2);
                String selectedPairedName=listPairedName.toString();
                Set<BluetoothDevice> bthDeviceChecking=bluetooth.getBondedDevices();
                for(final BluetoothDevice bthDevice:bthDeviceChecking)
                {
                    if(bthDevice.getName().contains(selectedPairedName))
                    {
                        listPairDevice.clear();
                        listPairDevice.add(bthDevice);
                        final String addressPairedDevice=bthDevice.getAddress();
                        AsyncTask<Integer, Void, Void> asynTask=new AsyncTask<Integer,Void,Void>() {
                            @Override
                            protected Void doInBackground(Integer... params) {
                                try {
                                    socket=bthDevice.createRfcommSocketToServiceRecord(uuid);
                                    socket.connect();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }
};
                        asynTask.execute(arg2);
                    }
                }
            }
        });
    }
 类似资料:
  • 连接设备 接口说明 用于连接 扫描出来的蓝牙设备。 参数说明 字段 类型 必须? 说明 device RKBLEDevice 是 蓝牙设备 示例代码 Swift: RokidMobileSDK.binder.connect(device: RKBLEDevice) Objc: [RokidMobileSDK.binder connect:device]; 断开设备 接口说明 用于断开已经连接的

  • 连接蓝牙设备 接口说明 接口需传入蓝牙名称(蓝牙address重启后会变) 参数说明 字段 类型 必须? 说明 name String 是 设备名称 举个大栗子 RokidMobileSDK.binder.connectBT(name, new IBTConnectCallBack() { @Override public void onConnectSucceed(BTDevic

  • 我如何获得Android所有已连接蓝牙设备的列表,而不考虑配置文件? 或者,我看到您可以通过BluetoothManager获取特定配置文件的所有连接设备。获取连接的设备。 我想我可以通过ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED监听连接/断开来查看哪些设备连接...似乎容易出错。 但我想知道是否有更简单的方法来获取所有已连接蓝牙设备的列表。

  • 我正在开发一款连接蓝牙物联网设备的Flitter应用程序。我正在使用Flatter_blue图书馆。该库允许扫描附近的蓝牙设备。基于该扫描,您可以“连接”到设备。没有与设备配对的概念。 根据我以前在手机上使用蓝牙的经验(连接到我的汽车和蓝牙扬声器时),我必须在Android操作系统上配对设备。 我很好奇,从高层次上讲,配对设备和连接设备之间有什么区别。此外,更具体地说,在操作系统内配对设备与扫描并

  • 连接和断开蓝牙设备在Windows Phone/Desktop 8.1上产生了各种结果。我一直在使用命名空间和我尝试连接具有不同蓝牙版本/类的几个设备。 版本1.2(1级和2级) 每当我尝试连接到2.0或2.1设备时,都会出现问题。第一次尝试连接到每个设备时,一切都会顺利,连接也会打开。当我随后关闭连接并重新连接设备时,问题就开始了。在重新连接期间,连接将永远不会打开,调试器将抛出一个系统。例外:

  • 嗨,我要开发一个应用程序,所以我有一个设备(服务器)与3个客户端。我做了所有的验证,打开蓝牙,找到设备,所有的工作都很好。但当我要连接一个设备时,我不知道会发生什么。 我正在使用下一个代码,当我单击一个我想连接它的设备时。我只有我的应用程序在母设备中。 这里我有一个问题,如果它没有配对会发生什么?如果我尝试连接,它会自动配对吗? 我的UUID是:“00001101-0000-1000-8000-0