使用Bluecove将数据从Android蓝牙发送到PC

12

我正在尝试使用Android SDK的API通过Bluecove将数据从Android发送到作为服务器的Windows PC。

我可以使Android连接到服务器,但是当我写入套接字的输出流时,服务器上没有任何反应。我已经重写了onPut方法,但它从未被调用。

以下是代码,如果有人能帮忙,我将不胜感激:

Android

public class BluetoothWorker {

private static UUID generalUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private static BluetoothSocket socket;


private static BluetoothSocket getBluetoothSocket(){

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.cancelDiscovery();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            if(device.getName().equalsIgnoreCase(("MIGUEL-PC"))){
                try {
                    return device.createRfcommSocketToServiceRecord(generalUuid);
                } catch (IOException e) {
                    return null;
                }
            }
        }
    }
    return null;
}

public static boolean sendData(String status){

        socket = getBluetoothSocket();
        try {
            socket.connect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            socket = null;
        }

    if(socket != null){
        try {       

            socket.getOutputStream().write(status.getBytes());
            socket.getOutputStream().flush();
            socket.close();
            return true;
        } catch (IOException e) {
            socket = null;
            return false;
        }
    }else{
        return false;
    }
}
}

个人电脑的代码

public class AISHIntegrationBTBridge {

static final String serverUUID = "0000110100001000800000805F9B34FB";

public static void main(String[] args) throws IOException {

    LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);

    SessionNotifier serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:"
            + serverUUID + ";name=ObexExample");

    int count = 0;
    while(count < 2) {
        RequestHandler handler = new RequestHandler();
        serverConnection.acceptAndOpen(handler);

        System.out.println("Received OBEX connection " + (++count));
    }
}

private static class RequestHandler extends ServerRequestHandler {

    public int onPut(Operation op) {
        try {
            HeaderSet hs = op.getReceivedHeaders();
            String name = (String) hs.getHeader(HeaderSet.NAME);
            if (name != null) {
                System.out.println("put name:" + name);
            }

            InputStream is = op.openInputStream();

            StringBuffer buf = new StringBuffer();
            int data;
            while ((data = is.read()) != -1) {
                buf.append((char) data);
            }

            System.out.println("got:" + buf.toString());

            op.close();
            return ResponseCodes.OBEX_HTTP_OK;
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
        }
    }
}
}
2个回答

8

我两年前做过类似的事情。

对于Android,我的代码与你的稍有不同:

BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

dos.writeChar('x'); // for example

socket.close();

我使用了DataOutputStream来向PC发送数据。但是这不重要,只是为了参考。

对于PC,

LocalDevice localDevice = LocalDevice.getLocalDevice();

localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service

String url = "btspp://localhost:" + device_UUID + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);

StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();

char c;
while (true) {
    c = dis.readChar();
    if (c == 'x')
        break;
}

connection.close();

我不确定上述代码是否仍然适用于今天,因为这是两年前完成的。BlueCove API可能已经发生了很大变化。但无论如何,这些代码对我有效。希望这可以帮助你。

还有一点需要注意的是,我不得不在我的电脑上卸载Toshiba蓝牙驱动程序并重新安装Microsoft的驱动程序,才能使用BlueCove。否则,它将无法正常工作。(然而,最新版本的BlueCove可能已经支持不同的驱动程序,请纠正我是否说错了什么。)


0
尝试在您的Android sendData方法中使用“println”而不是“write”!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接