通过蓝牙从Android设备发送字符串到笔记本电脑

时间:2018-01-12 01:18:30

标签: java android bluetooth bluecove

我正在开发一个项目,涉及从Android手机发送一个字符串到运行32位Windows VM(VMWare Fusion)的笔记本电脑。在做了一些关于如何做这样的事情的搜索之后,我让客户端(电话)工作,但是对于服务器(笔记本电脑),它似乎永远不会收到任何东西。

我与VM共享蓝牙,&设备与主机配对,但是当我发送数据时,它连接到主机,但运行“服务器”的VM似乎永远不会收到它。

我从here获得了接收器的代码,但仅仅是为了澄清,下面是我用于接收器/服务器/ VM的代码:

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.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class server {
    // TODO: Update this to use the window, instead of the console
    public void startServer() throws IOException {

        // Create a UUID for SPP
        UUID uuid = new UUID("1101", true);
        //UUID uuid = new UUID("00001101-0000-1000-8000-00805F9B34FB", false);
        // Create the servicve url
        String connectionString = "btspp://localhost:" + uuid + ";name=SpudSPPServer";

        // 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[]) throws IOException {

    display d  = new display();
    server blueToothServer = new server();
    d.makePanel();
    if (selectFile.fileMissing()) {
        d.feed.setText("File missing, creating new file");
        selectFile.makeFile();
        d.feed.setText("File created! Awaiting data...");
    } else {
        d.feed.setText("File found! Awaiting data...");
        d.MACAddress.setText("MAC Address: " + LocalDevice.getLocalDevice().getBluetoothAddress().replace("-", ":").toUpperCase());
    }

    blueToothServer.startServer();

}

如果您需要客户端(电话)的代码,我可以发帖,如果被要求

1 个答案:

答案 0 :(得分:0)

想出来,事实证明它是在VM端,所以我只是正常安装Windows而不是VM,&修好了它

相关问题