主动请求蓝牙配对(Android)

我现在已经得到了附近蓝牙设备BluetoothDevice,怎么主动发送配对请求,在网上找了许多答案,但是好像都是一些聊天方面的内容要用到BluetoothServerSocket和BluetoothSocket方面的,难道我请问配对也要用到这些,能顺便原理或者思路吗?谢谢!

createInsecureRfcommSocketToServiceRecord or createRfcommSocketToServiceRecord, then BluetoothSocket.connect()

可以直接通过 device.connect() 来尝试让系统自动去配对

可以尝试一下隐藏的 api 哦,亲

static public boolean createBond(Class btClass,BluetoothDevice btDevice) throws Exception {  
    Method createBondMethod = btClass.getMethod("createBond");  
    Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
    return returnValue.booleanValue();  
} 

用上面的方法来请求配对,更多信息,详见:
http://blog.csdn.net/hellogv/article/details/6042091

你已经找到了附近的蓝牙,那你选一个蓝牙,然后取得他的地址

BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
m_ChatService.connect(device);

至于m_ChatService,是BluetoothChat这个代码(网上找)里的BluetoothChatService对象,我就是从这聊天代码里修改后拿来用

// 自动配对设置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
        throws Exception {
    Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
    Boolean result = (Boolean) autoBondMethod
            .invoke(device, new Object[] { strPin.getBytes() });
    return result;
}

// 开始配对
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
    Method createBondMethod = btClass.getMethod("createBond");
    Boolean returnValue = (Boolean) createBondMethod.invoke(device);
    return returnValue.booleanValue();
}

更详细的你可以看下蓝牙自动配对