简体   繁体   English

将数据从BLED112发送到BLE设备

[英]Send data from BLED112 to BLE device

I'm trying to implement a java application on windows 10 to be able to send data to a Feather 32u4 Bluefruit using bluetooth low energy. 我正在尝试在Windows 10上实现Java应用程序,以便能够使用低功耗蓝牙将数据发送到Feather 32u4 Bluefruit。

I bought the BLED112 dongle to be able to use the bglib and bgapi 我购买了BLED112加密狗以能够使用bglib和bgapi

I succeed to connect my java code to the dongle by using serial port but I have no idea how to connect to my device... 我通过使用串口成功地将Java代码连接到加密狗,但是我不知道如何连接到设备...

Thank you for your help, 谢谢您的帮助,

PS: this is my actual code : PS:这是我的实际代码:

static BGAPITransport bgapi;
static BLEDevice bledevice = null;

public static void main(String[] args) {
    System.out.println("Program started");
    bgapi=connectBLED112();
    bledevice=bgapi.
    System.out.println("Finished successfully");
}

public static BGAPITransport connectBLED112() {
    SerialPort port = connectSerial();
    try {
        return new BGAPITransport(port.getInputStream(), port.getOutputStream());
    } catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
    return null;
}

public static String selectSerialPort() {

    CommPortIdentifier serialPortId = null;   
    Enumeration enumComm;

    enumComm = CommPortIdentifier.getPortIdentifiers();
    while (enumComm.hasMoreElements()) {
        serialPortId = (CommPortIdentifier) enumComm.nextElement();
        if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            System.out.println(serialPortId.getName());
        }
    }

    return serialPortId.getName();
}

public static SerialPort connectSerial() {
    try {

        String portName = selectSerialPort();

        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);

        if (portIdentifier.isCurrentlyOwned()) {
            System.err.println("Error: Port " + portName + " is currently in use");
        } 
        else {
            CommPort commPort = portIdentifier.open("BLED112", 2000);

            System.out.println("port = " + commPort);

            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
                serialPort.setRTS(true);


                System.out.println("serial port = " + serialPort);

                return serialPort;

            } else {
                System.err.println("Error: Port " + portName + " is not a valid serial port.");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

This may be late, but here goes: 这可能已经晚了,但是这里有:

First, look up the BLED112 Api. 首先,查找BLED112 Api。 It's online and fairly easy to find. 它是在线的,相当容易找到。 That's what you're going to want to keep, and reference a lot. 这就是您要保留的内容,并提供很多参考。

It's also here: https://www.silabs.com/documents/login/reference-manuals/Bluetooth_Smart_Software-BLE-1.6-API-RM.pdf 它也在这里: https : //www.silabs.com/documents/login/reference-manuals/Bluetooth_Smart_Software-BLE-1.6-API-RM.pdf

Second, start listening to advertisements on the BLED112. 其次,开始收听BLED112上的广告。 There's a command for that, found within the Api document. 在Api文档中可以找到一个命令。 The connection command is also listed in the api reference document. api参考文档中也列出了连接命令。

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

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