gpt4 book ai didi

java - Android应用程序仅在配对后才通过蓝牙连接到java应用程序服务器

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:05 24 4
gpt4 key购买 nike

大家好!所以我有这个 android 应用程序,它充当客户端,并尝试连接到我的 PC 上有一个蓝牙服务器组件的 java 应用程序。

我面临的问题是,仅当我的电脑不在 Nexus 5 的配对设备列表中时才会建立连接。换句话说,它们仅在配对时才会连接。因此,每次我想要建立连接时,我都被迫从配对设备列表中删除我的电脑,否则连接就会失败。

我用过这个Simple Android and Java Bluetooth Application服务器端应用程序的基础:

import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class WaitThread implements Runnable{

public WaitThread() { }

@Override
public void run() {
waitForConnection();
}

/** Waiting for connection from devices */
private void waitForConnection() {
// retrieve the local Bluetooth device object
LocalDevice local = null;

StreamConnectionNotifier notifier;
StreamConnection connection = null;

// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);

UUID uuid = new UUID("04c6032b00004000800000805f9b34fc", false);

System.out.println(uuid.toString());

String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);

} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}

// waiting for connection
while(true) {
try {
System.out.println("waiting for connection...");

connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();

} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}

并使用 Android 示例项目中的 BluetoothChat 作为客户端的基础:

import java.io.IOException;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
Handler mHandler;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static final UUID MY_UUID = UUID
.fromString("04c6032b-0000-4000-8000-00805f9b34fc");

public ConnectThread(BluetoothDevice device, Handler handler) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmpSocket = null;
mmDevice = device;
mHandler = handler;

// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
tmpSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Message msgException = new Message();
msgException.setTarget(mHandler);
msgException.what = Constants.DISCONNECTED_HANDLER;
msgException.sendToTarget();
}
mmSocket = tmpSocket;

}

public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();

Message msg1 = new Message(); // handler. we use it to know when de
// device has been connected or
// disconnected in the UI activity
msg1.setTarget(mHandler);

msg1.what = Constants.CONNECTING_HANDLER;
msg1.sendToTarget();

try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();

// handler
Message msg2 = new Message();
msg2.setTarget(mHandler);
msg2.what = Constants.CONNECTED_HANDLER;
msg2.setTarget(mHandler);
msg2.sendToTarget();

} catch (IOException connectException) {
// Unable to connect; close the socket and get out
System.out.println("alex - NO connected");
Message msgException = new Message();
msgException.setTarget(mHandler);
msgException.what = Constants.DISCONNECTED_HANDLER;
msgException.sendToTarget();

try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}

public BluetoothSocket getBTSocket() {
return mmSocket;
}
}

我有一种感觉,这可能是因为我正在使用的 UUID。对我有用的 UUID 如下:

  1. 04c6032b00004000800000805f9b34fc
  2. 0000110500001000800000805f9b34fb

我在两个应用程序中使用相似的 UUID。知道可能导致此问题的原因吗?

最佳答案

奇怪的是,今天当我使用蓝牙适配器而不是我自己笔记本电脑的蓝牙时,问题得到了解决。因此,这不是软件组件的问题。

关于java - Android应用程序仅在配对后才通过蓝牙连接到java应用程序服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com