简体   繁体   English

即使连接失败,Android蓝牙也会配对设备

[英]Android Bluetooth is pairing a device even if the connection fails

I'm writing a Bluetooth app targeting API 7 (my device is API 8 - 2.2). 我正在编写针对API 7的蓝牙应用程序(我的设备是API 8-2.2)。 When I attempt to connect the Bluetooth socket I get the pop-up prompting the user to pair with the device, but sometimes the connection times out or fails. 当我尝试连接蓝牙插座时,弹出窗口提示用户与设备配对,但是有时连接超时或失败。 In these cases, the pop-up disappears without clicking anything but the device shows as "Paired but not connected" in the Bluetooth settings. 在这些情况下,弹出窗口会消失而不显示任何内容,但设备在蓝牙设置中显示为“配对但未连接”。

If I connect to the Bluetooth device directly from the Bluetooth settings then this doesn't happen; 如果我直接从Bluetooth设置连接到Bluetooth设备,则不会发生这种情况。 the device only shows "Paired but not connected" if the user actually clicks "Pair". 如果用户实际单击“配对”,则设备仅显示“已配对但未连接”。

Here's the code that's creating the connection, it seems simple enough: 这是创建连接的代码,看起来很简单:

    try {
        BluetoothDevice btDevice = _btAdapter.getRemoteDevice(address);
        _btAdapter.cancelDiscovery();
        sock = btDevice.createRfcommSocketToServiceRecord(SafConstants.BT_UUID);
        sock.connect();
    } catch (IOException e) {
        logger.error("socket connection failed", e);
        if (sock != null) {
            try {sock.close();}
            catch (IOException e1) {}
        }
    }

Is this an Android issue or am I supposed to be doing something else? 这是Android问题还是我应该做其他事情? I searched the API to see if I could remove the device from the set of bonded devices (if the connection fails) but it seems this isn't possible. 我搜索了API,以查看是否可以从绑定设备集中删除该设备(如果连接失败),但这似乎是不可能的。

EDIT: One thing I notice is that when I try connecting through my app, I get prompted on my laptop that it's trying to access the Headset service before pairing. 编辑:我注意到的一件事是,当我尝试通过我的应用程序进行连接时,我在笔记本电脑上得到提示,它正在尝试在配对之前访问耳机服务。 After a few seconds I'll eventually get the prompt on my laptop that a device is trying to pair. 几秒钟后,我最终会在笔记本电脑上收到设备正在尝试配对的提示。

If I connect through bluetooth settings then I get the prompt to pair the device first and then after it's successful I get the prompt that a device is attempting to access the Headset service. 如果我通过蓝牙设置进行连接,则系统会提示您先配对设备,然后在成功配对后,系统会提示您设备正在尝试访问耳机服务。 I'm not sure if that's significant or not... 我不确定这是否重要...

The UUID you are using is for SPP (Serial Port Profile), and being a headset, would require the Bluetooth profile - A2DP (Advanced Audio Distribution ). 您正在使用的UUID用于SPP(串行端口配置文件),并且作为耳机,需要蓝牙配置文件-A2DP(高级音频分发)。 Currently, the Android implementation of the Bluetooth stack, does not have support for Bluetooth profiles and is only a bare bones implementation. 当前,蓝牙堆栈的Android实现不支持蓝牙配置文件,而仅仅是实现。

For Bluetooth profile support to be implemented on Android, there is a project called “Sybase-iAnywhere-Blue-SDK-for-Android”, which replaces Android's version, and provides all interfaces into the underlying Bluetooth profiles and protocols. 为了在Android上实现蓝牙配置文件支持,有一个名为“ Sybase-iAnywhere-Blue-SDK-for-Android”的项目,该项目替代了Android的版本,并提供了到基础蓝牙配置文件和协议的所有接口。 Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK. 使用此工具,可以使用此SDK提供的BPP配置文件在您的Android手机上通过蓝牙进行打印。

See links below for more details: 有关更多详细信息,请参见下面的链接:

link 1: http://www.sybase.com/detail?id=1064424 链接1: http//www.sybase.com/detail?id = 1064424

Link 2: http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks 链接2: http//www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM