Android的蓝牙:配对的设备列表 [英] Android bluetooth: Paired devices list

查看:345
本文介绍了Android的蓝牙:配对的设备列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有SPP轮廓和蓝牙2.1版蓝牙设备。结果
我有连接到该设备并与其通信的应用程序。该设备采用只是工作配对技术。

I have a bluetooth device with SPP profile and bluetooth version 2.1.
I have an app which connects to that device and communicates with it. The device uses "Just Works" pairing technique.

我面对某些手机像三星Galaxy平板电脑,Galaxy S的。

I am facing a problem on certain phones like Samsung Galaxy tablet, Galaxy S.

问题是用户从应用程序退出后,我关闭插座和从设备断开连接。成功断开后,可以观察到该设备的条目从所述配对的设备列表中删除。

The problem is after the user exits from the app, I am closing the sockets and disconnecting from the device. After successful disconnection, it is observed that the device's entry is removed from the paired devices list.

推荐答案

我还没有与片的工作,但我确实写SPP使用Android手机的应用程序。我发现的是,为了获得蓝牙是稳定的,我必须手动与我想与之通信的设备保证金。我们用下面发起粘接code从应用程序内,它应该preserve的结合,就好像您手动通过设置菜单配对。

I haven't worked with tablets, but I did write an app that used SPP for Android phones. What I found was that in order to get Bluetooth to be stable, I have to manually bond with the device I want to communicate with. We used the code below to initiated bonding from within the app, and it should preserve the bonding just as if you manually paired through the settings menu.

下面是一般流程:
1)注册一个BroadcastReceiver来监听BluetoothDevice.ACTION_BOND_STATE_CHANGED结果
2)设备发现后,你应该有一个BluetoothDevice类的对象。结果
在BluetoothDeviceObject结果3)使用反射来调用createBond'的方法
   3A)打开插座前,等待债券状态更改事件

Here is the general flow: 1) Register a BroadcastReceiver to listen for BluetoothDevice.ACTION_BOND_STATE_CHANGED
2) After device discovery you should have a BluetoothDevice object.
3) Use reflection to call 'createBond' method on a BluetoothDeviceObject
3a) Wait for bond state change events before opening sockets

BluetoothDevice device = {obtained from device discovery};
Method m = device.getClass().getMethod("createBond", (Class[])null);
m.invoke(device, (Object[])null);

int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_NONE || bondState == BluetoothDevice.BOND_BONDING)
{
    waitingForBonding = true; // Class variable used later in the broadcast receiver

    // Also...I have the whole bluetooth session running on a thread.  This was a key point for me.  If the bond state is not BOND_BONDED, I wait here.  Then see the snippets below
    synchronized(this)
    {
        wait();
    }
}

4)等待债券状态从BOND_BONDING到BOND_BONDED结果修改

4) Wait for the bond state to change from BOND_BONDING to BOND_BONDED

里面一个BroadcastReciever:

Inside a BroadcastReciever:

public void onReceive(Context context, Intent intent)
{
    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction()))
    {
        int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
        int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

        if (waitingForBonding)
        {
            if (prevBondState == BluetoothDevice.BOND_BONDING)
            {
                // check for both BONDED and NONE here because in some error cases the bonding fails and we need to fail gracefully.
                if (bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_NONE)
                {
                    // safely notify your thread to continue
                }
            }
        }
    }
}

5)打开插座和沟通

5) Open sockets and communicate

您还可以通过你的反思removeBond方法从配对列表中删除您的设备。

You can also you the 'removeBond' method via reflection to remove your device from the pairing list.

希望这有助于!

这篇关于Android的蓝牙:配对的设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆