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

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

问题描述

我想从机器人(使用API​​从SDK)将数据发送到在Windows上使用Bluecove,这是最后一个服务器的PC。

我可以在Android连接到服务器,但是当我写入套接字的输出流,什么都不会发生在服务器上。我已经重写了onPut方法,但它永远不会被调用。

code如下波纹管,如果有人可以帮助我,我会非常AP preciated:

安卓

 公共类BluetoothWorker {

私有静态UUID generalUuid = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);

私有静态的BluetoothSocket插座;


私有静态的BluetoothSocket getBluetoothSocket(){

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.cancelDiscovery();
    设置< BluetoothDevice类> pairedDevices = mBluetoothAdapter.getBondedDevices();
    //如果有配对设备
    如果(pairedDevices.size()大于0){
        //循环配对设备
        对于(BluetoothDevice类设备:pairedDevices){
            //添加姓名和地址到一个数组适配器在ListView显示
            如果(device.getName()。equalsIgnoreCase((MIGUEL-PC))){
                尝试 {
                    返回device.createRfcommSocketToServiceRecord(generalUuid);
                }赶上(IOException异常E){
                    返回null;
                }
            }
        }
    }
    返回null;
}

公共静态布尔送出数据(字符串状态){

        插座= getBluetoothSocket();
        尝试 {
            Socket.connect()的;
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            插座= NULL;
        }

    如果(插座!= NULL){
        尝试 {

            。socket.getOutputStream()写(status.getBytes());
            socket.getOutputStream()平齐()。
            socket.close();
            返回true;
        }赶上(IOException异常E){
            插座= NULL;
            返回false;
        }
    }其他{
        返回false;
    }
}
}
 

PC code

 公共类AISHIntegrationBTBridge {

静态最后弦乐serverUUID =0000110100001000800000805F9B34FB;

公共静态无效的主要(字串[] args)抛出IOException异常{

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

    SessionNotifier serverConnection =(SessionNotifier)Connector.open(btgoep://本地主机
            + serverUUID +;名称= ObexExample);

    诠释计数= 0;
    而(计数2){
        RequestHandler的处理程序=新的RequestHandler的();
        serverConnection.acceptAndOpen(处理);

        的System.out.println(接收OBEX连接+(++数));
    }
}

私有静态类RequestHandler的扩展ServerRequestHandler {

    公众诠释onPut(操作op){
        尝试 {
            HeaderSet HS = op.getReceivedHeaders();
            字符串名称=(字符串)hs.getHeader(HeaderSet.NAME);
            如果(名字!= NULL){
                的System.out.println(把名字:+姓名);
            }

            InputStream的是= op.openInputStream();

            StringBuffer的BUF =新的StringBuffer();
            int数据;
            而((数据= is.​​read())!= -1){
                buf.append((char)的数据);
            }

            的System.out.println(有:+ buf.toString());

            op.close();
            返回响应codes.OBEX_HTTP_OK;
        }赶上(IOException异常E){
            e.printStackTrace();
            返回响应codes.OBEX_HTTP_UNAVAILABLE;
        }
    }
}
}
 

解决方案

我已经2年前做过类似的事情。

有关Android的,我的code是你的略有不同:

 的BluetoothSocket插座= Device.createRfcommSocketToServiceRecord(device_UUID);
Socket.connect()的;
DataOutputStream类DOS =新DataOutputStream类(socket.getOutputStream());

dos.writeChar('×'); // 例如

socket.close();
 

我用 DataOutputStream类将数据发送到PC。但可以肯定这没有关系,只是供大家参考。

有关PC,

  LocalDevice的LocalDevice的= LocalDevice.getLocalDevice();

localDevice.setDiscoverable(DiscoveryAgent.GIAC); //广告服务

字符串URL =btspp://本地主机:+ device_UUID +;名称= BlueToothServer;
StreamConnectionNotifier服务器=(StreamConnectionNotifier)Connector.open(URL);

的StreamConnection连接= server.acceptAndOpen(); //等到客户端连接
// ===至此,两个设备应连接=== //
DIS的DataInputStream = connection.openDataInputStream();

焦炭℃;
而(真){
    C = dis.readChar();
    如果(C =='X')
        打破;
}

connection.close()时;
 

我不知道如果上述codeS今天仍然有效,因为这样做是2年前。该BlueCove API可能已经发生了很大变化。但无论如何,对我来说这些codeS的工作。希望这可以帮助你。

还要说明一点的是,我不得不卸载在我的电脑东芝的蓝牙驱动程序,并重新安装Microsoft之一,以利用BlueCove的。否则,将无法正常工作。 (但是,可能已经支持BlueCove的最新版本不同的驱动程序,请纠正我,如果我说的任何事。)

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.

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 :

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;
    }
}
}

PC code

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;
        }
    }
}
}

解决方案

I have done similar thing 2 years ago.

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

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

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

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.

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

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

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