java中的bluecove库出错

java中的bluecove库出错,java,bluetooth,bluecove,Java,Bluetooth,Bluecove,我想用JAVA编写一个简单的服务器和客户端,通过蓝牙交换字符串(我想将字符串从笔记本电脑a发送到笔记本电脑B,两者都有64位windows 7)。通过谷歌搜索,我找到了bluecove java库。基于实例 我为我的服务器写了这段代码,它的运行没有问题,但我的客户端有问题。当我想运行客户机时,我得到了这个错误 BlueCove 2.1.1版—winsock上的快照 java.lang.ClassCastException:com.intel.bluetooth.BluetoothRFCommCo

我想用JAVA编写一个简单的服务器和客户端,通过蓝牙交换字符串(我想将字符串从笔记本电脑a发送到笔记本电脑B,两者都有64位windows 7)。通过谷歌搜索,我找到了bluecove java库。基于实例

我为我的服务器写了这段代码,它的运行没有问题,但我的客户端有问题。当我想运行客户机时,我得到了这个错误

BlueCove 2.1.1版—winsock上的快照 java.lang.ClassCastException:com.intel.bluetooth.BluetoothRFCommConnectionNotifier无法强制转换为javax.microedition.io.StreamConnection 位于bluetooch.Main.Main(Main.java:84)

这是我的服务器:

package MainPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.bluetooth.*;
import javax.microedition.io.*;

//Class that implements an SPP Server which accepts single line of
// message from an SPP client and sends a single line of response to the  
//client

public class BluetoothServer {

//start server
private void startServer() throws IOException{

    //Create a UUID for SPP
    UUID uuid = new UUID("1101", true);
    //Create the servicve url
    String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP       Server";

    //open server url
   StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

    //Wait for client connection
    System.out.println("\nServer Started. Waiting for clients to connect...");
    StreamConnection connection = streamConnNotifier.acceptAndOpen();

    RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
    System.out.println("Remote device address: "+dev.getBluetoothAddress());
    System.out.println("Remote device name: "+dev.getFriendlyName(true));

    //read string from spp client
    InputStream inStream=connection.openInputStream();
    BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
    String lineRead=bReader.readLine();
    System.out.println(lineRead);

    //send response to spp client
    OutputStream outStream=connection.openOutputStream();
    PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
    pWriter.write("Response String from SPP Server\r\n");
    pWriter.flush();
    pWriter.close();
    streamConnNotifier.close();
}


public static void main(String[] args) {

    try {
        //display local device address and name
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        System.out.println("Address: "+localDevice.getBluetoothAddress());
        System.out.println("Name: "+localDevice.getFriendlyName());
        System.out.println("localDevice.getDiscoverable() " + localDevice.getDiscoverable());

        System.out.println("bluetooth is discoverable " + localDevice.setDiscoverable(DiscoveryAgent.LIAC));
        BluetoothServer sampleSPPServer=new BluetoothServer();
        sampleSPPServer.startServer();

    } catch (Exception e) {

        e.printStackTrace();
    } 

}
}

这位是客户

package bluetooch;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class Main {

public static final Vector devicesDiscovered = new Vector();
public static void main(String[] args){

    try {            


            StreamConnection streamConnection =        (StreamConnection)Connector.open("btspp://localhost:" + "1101" +";name=Sample  SPP Server");      
            //send string
            OutputStream outStream = streamConnection.openOutputStream();
            PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
            pWriter.write("Test String from SPP Client\r\n");
            pWriter.flush();

            //read response
            InputStream inStream=streamConnection.openInputStream();
            BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
            String lineRead=bReader2.readLine();
            System.out.println(lineRead);



        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter an integer");
        int myint = keyboard.nextInt();

    } catch (Exception e) {
        System.out.println("Error Occured! below is the message\r\n" + e.getMessage());
    }
}
}


对这个问题有什么想法吗?

嗯,这条消息说明了一切

您正在将
连接器.open()
返回的
连接
强制转换为
流连接

但是返回的连接不是
StreamConnection
的实例。它是
com.intel.bluetooth.BluetoothRFCommConnectionNotifier
的一个实例,如果您阅读javadoc或源代码,它将实现
StreamConnectionNotifier
,而不是
StreamConnection