我正在努力为我的应用程序添加蓝牙功能,最终我想要使用的设备是耳机/听筒。我已经开始组装代码并使用它进行部分功能。当我获得通过服务器设置蓝牙连接的代码时,添加代码时出现错误。我尝试通过将鼠标悬停在错误上并自动更正来解决问题,但是每次我解决一个问题时都会出现不同的问题。这让我相信我错过了自动更正不知道的东西。我需要一些帮助来修复错误。对于第一次设置蓝牙编码的有用建议也将不胜感激。错误用 ||#| 括起来 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) {
}
}
}