简体   繁体   English

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

[英]Sending string from android device to laptop via bluetooth

Im working on a project that involves sending a string from an android phone to my laptop running a 32 bit windows VM (VMWare Fusion). 我正在处理一个项目,该项目涉及从Android手机向运行32位Windows VM(VMWare Fusion)的笔记本电脑发送字符串。 After doing some searching on how to do such a thing, I get the client (phone) working, but for the server (laptop) end, it never seems to receive anything. 在进行了某种操作的搜索之后,我让客户端(电话)工作了,但是对于服务器(笔记本电脑)端,它似乎从未收到任何东西。

Im sharing bluetooth with the VM, & the device is paired to the host, yet when I send the data it connects to the host, but the VM running the "server" never seems to receive it. 我与VM共享蓝牙,并且设备已与主机配对,但是当我发送数据时,它已连接到主机,但是运行“服务器”的VM似乎从未收到它。

I got the code for the receiver from here , but just for clarifying, heres the code im using for the receiver/server/VM: 我从这里获得了接收器的代码,但是为了澄清起见,以下是用于接收器/服务器/ 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();

    }
}

& in the main file: &在主文件中:

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();

}

If you need the code for the client (phone), I can post that if asked 如果您需要客户端(电话)的代码,可以在询问时将其发布

弄清楚了,原来是在VM端,所以我通常只是安装Windows而不是VM,并修复了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM