使用bluecove将数据从android蓝牙发送到PC [英] Send data from android bluetooth to PC with bluecove

查看:34
本文介绍了使用bluecove将数据从android蓝牙发送到PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Windows 上的 Bluecove 将数据从 android(使用其 SDK 中的 API)发送到 PC,这是服务器的最后一个.

I'm trying to send data from android (using API from its SDK) to a PC using Bluecove on windows, being this last one the server.

我可以让 android 连接到服务器,但是当我写入套接字的输出流时,服务器上没有任何反应.我重写了 onPut 方法,但从未调用过.

I can get the android to connect to the server but when I write to the socket's output stream, nothing happens on server. I have the onPut method overriden but it is never called.

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

Code follows bellow, if anyone could help me I would be very appreciated :

安卓

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 年前做过类似的事情.

I have done similar thing 2 years ago.

对于 Android,我的代码与您的略有不同:

For Android, my code is slightly different from yours:

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

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

socket.close();

我使用 DataOutputStream 将数据发送到 PC.不过这肯定无所谓,仅供参考.

I used DataOutputStream to send data to PC. But surely this doesn't matter, just for your reference.

对于个人电脑,

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

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

I am not sure if the above codes still work today, as this was done 2 years ago. The BlueCove API may have changed a lot. But anyway, these codes work for me. Hope this may help you.

还要注意的是,我必须卸载 PC 中的 Toshiba 蓝牙驱动程序并重新安装 Microsoft 驱动程序才能使用 BlueCove.否则,它不会工作.(不过,最新版本的BlueCove可能已经支持了不同的驱动,如有不对请指正.)

One more note is that, I had to uninstall the Toshiba Bluetooth Driver in my PC and reinstall the Microsoft one in order to make use of BlueCove. Otherwise, it won't work. (However, latest version of BlueCove may have already supported different drivers, please correct me if I said anything wrong.)

这篇关于使用bluecove将数据从android蓝牙发送到PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆