Android 仅将蓝牙与用户确认配对
我只想将蓝牙和来自另一端的确认配对..但我实现了一个代码,这里并没有向另一端发送确认消息..所以现在我如何在通过蓝牙配对两个设备之前向另一端发送确认消息 我使用以下代码:Android 仅将蓝牙与用户确认配对,android,Android,我只想将蓝牙和来自另一端的确认配对..但我实现了一个代码,这里并没有向另一端发送确认消息..所以现在我如何在通过蓝牙配对两个设备之前向另一端发送确认消息 我使用以下代码: private class AcceptThread extends Thread { // The local server socket private final BluetoothServerSocket mmServerSocket; public AcceptThread() {
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter
.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the given
// BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
@Override
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG,"unable to close() socket during connection failure",
e2);
}
// Start the service over to restart listening mode
BluetoothChatService.this.start();
return;
}
因此,请查看我的代码,并建议我仅当从远程端进行身份验证时,如何对2台设备进行配对…我使用的是api级别7,只要这两台设备未配对,您就无法向另一台设备发送消息,因为您需要一个BluetoothSocket,除非您拥有设备的MAC地址,否则无法获得该消息。 但是配对请求有什么问题?它将仅第一次显示,然后您始终可以在配对设备中找到该设备(mBluetoothAdapter.getBondedDevices()) Buuut,如果您绝对有必要摆脱两部分开头显示的配对请求对话框,那么您可以尝试使用不安全的通道(使用ListenusingSecureRFCommWithServiceRecord创建服务器套接字,使用CreateSecureRFCommsocketToServiceRecord创建客户端套接字)。这里的问题是,每次需要连接到设备时都必须执行扫描,因为设备没有配对
希望它适合你,祝您好运。hi gabrela…是否可以在api级别10以下使用IStenusingSecureRFCommwith服务记录?您好gabrjela..在两台设备配对后,如何启动带有授权请求的交换配置文件信息会收到通知..在接受后,仅交换配置文件信息..我从3天起就被困在这里了,请帮忙…谢谢你的耽搁。。。ListenusingSecureRFCommwithServiceRecord从api级别10开始引入。关于第二个问题,我必须说你们的问题并不十分清楚,若你们仍然需要帮助,请尽量说得更清楚。