0

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

1 回答 1

1

错误 1,2:类中的任何地方都没有调用常量NAME

错误3:类中没有调用方法manageConnectedSocket()

您不能只是从开发人员页面复制和粘贴某些内容并期望它能够正常工作。它引导您朝着正确的方向前进,您必须填写缺失的部分。

于 2012-08-02T14:31:35.597 回答