Android蓝牙无法配对

时间:2010-03-13 07:42:57

标签: android bluetooth

我在让我的设备在Android中配对时遇到问题。如果我进入设置并手动配对,我可以使用以下代码连接它们:

服务器

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connect);

    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == REQUEST_ENABLE_BT)
    {
        if(resultCode == RESULT_CANCELED)
        {
            new AlertDialog.Builder(this)
            .setTitle(R.string.error)
            .setMessage(R.string.bluetooth_unavailable)
            .setPositiveButton(android.R.string.ok, AndroidUtils.DismissListener)
            .create()
            .show();
        }
        else
        {
            mServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("Moo Productions Bluetooth Server", mUUID);
            mState = State.ACCEPTING;
            BluetoothSocket socket = mServerSocket.accept();
            mServerSocket.close();
            connected(socket);
        }
    }
}

客户端

Set<BluetoothDevice> pairedDevices = mAdapter.getBondedDevices();
BluetoothSocket socket = null;

// Search the list of paired devices for the right one
for(BluetoothDevice device : pairedDevices)
{
    try
    {
        mState = State.SEARCHING;
        socket = device.createRfcommSocketToServiceRecord(mUUID);
        mState = State.CONNECTING;
        socket.connect();
        connected(socket);
        break;
    }
    catch (IOException e)
    {
        socket = null;
        continue;
    }
}

但是如果设备尚未配对,则无需连接到有效套接字即可脱离foreach。在那种情况下,我开始发现。

// If that didn't work, discover
if(socket == null)
{
    mState = State.SEARCHING;
    mReceiver = new SocketReceiver();
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    mAdapter.startDiscovery();
}

// ... Later ...

private class SocketReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
        {
            try
            {
                // Get the device and try to open a socket
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                BluetoothSocket socket = device.createRfcommSocketToServiceRecord(mUUID);
                mState = State.CONNECTING;
                socket.connect();

                // This is our boy, so stop looking
                mAdapter.cancelDiscovery();
                mContext.unregisterReceiver(mReceiver);

                connected(socket);
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
        }
    }
}

但它永远找不到其他设备。我从来没有得到配对对话框,当我单步执行时,我发现它发现了正确的设备,但它无法连接此异常java.io.IOException: Service discovery failed。关于我缺少的任何想法?

2 个答案:

答案 0 :(得分:3)

如果服务器实际上不可发现,您的客户端将永远不会发现服务器。您的服务器代码的评论说“确保设备可被发现”,但是监听套接字并不意味着该设备是可发现的。您可以通过调用:

使服务器可被发现
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

这是一个Intent,因为必须由系统启用可发现性,并且只有用户批准才能启动(因此这会引发一个对话框,请求发现300秒)。只有在300秒的这个窗口内,服务器才能被发现。在此期间,您的客户端代码应该可以正常运行,并且您将获得配对请求。

Android开发指南中详细介绍了这一点: http://developer.android.com/guide/topics/wireless/bluetooth.html

答案 1 :(得分:0)

不知道它是否仍然相关,但我是这样做的并且有效:

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
}

if (discoveredDeviceName.equals("your_device_name")) { 
  mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
}

另外,我会尝试在之前取消发现:

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(mUUID);
socket.connect();