我正在努力为我的应用添加蓝牙功能,最终我想要使用的设备是耳机/听筒。我已经开始组装代码和我的部分功能。当我到达服务器设置蓝牙连接的代码时,添加代码时出错。我已经尝试通过悬停错误和自动更正来解决问题,但每次我修复一个问题时出现不同的问题。这让我相信我错过了自动更正不知道的东西。我需要一些帮助修复错误。第一次设置蓝牙密码的有用建议也将受到赞赏。错误被||#|包围xxx |||。错误1:无法解决。错误2:无法解析为变量。错误3:未定义AcceptSocket类型。
import java.io.IOException;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
public class AcceptSocket extends Thread {
private static final String MY_UUID = null;
BluetoothServerSocket mmServerSocket;
public void AcceptThread() {
// Use a temporary object that is later asssigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = ||1|mBluetoothAdapter|||.listenUsingRfcommWithServiceRecord(||2|NAME|||,
MY_UUID);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
||3|manageConnectedSocket|||(socket);
mmServerSocket.close();
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) {
}
}
}
答案 0 :(得分:1)
错误1,2:班级中没有任何名为NAME
的常数。
错误3:类中没有名为manageConnectedSocket()
的方法。
您不能只是从开发人员的页面复制和粘贴某些内容并期望它能够正常工作。它引导你走向正确的方向,你必须填补缺失的部分。