javaandroidbluetoothbluecove

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). 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.

I got the code for the receiver from here, but just for clarifying, heres the code im using for the receiver/server/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


Solution

  • Figured it out, turns out it was on the VMs end, so I just installed windows normally as opposed to a VM, & that fixed it