使用java将数据从pc发送到蓝牙芯片BlueSMiRF

使用java将数据从pc发送到蓝牙芯片BlueSMiRF,java,bluetooth,bluesmirf,Java,Bluetooth,Bluesmirf,我想将数据从笔记本电脑中的蓝牙设备发送到BlueSMiRF蓝牙芯片。我以前从未处理过蓝牙编程,所以我需要关于如何启动和使用的指导。我正在研究java,但如果有更好的替代方案,我会研究它。这是一篇老文章,但我最近在arduino上设置了蓝牙模块,并将其与运行在Ubuntu上的java程序连接。因此,让我分享一些我觉得有用的链接 默认情况下,BlueSMiRF蓝牙模块在SPP(串行端口协议)模式下工作。您可以使用新的SoftwareSerial库编写用于蓝牙通信的arduino代码。此库允许您通过蓝

我想将数据从笔记本电脑中的蓝牙设备发送到BlueSMiRF蓝牙芯片。我以前从未处理过蓝牙编程,所以我需要关于如何启动和使用的指导。我正在研究java,但如果有更好的替代方案,我会研究它。

这是一篇老文章,但我最近在arduino上设置了蓝牙模块,并将其与运行在Ubuntu上的java程序连接。因此,让我分享一些我觉得有用的链接

默认情况下,BlueSMiRF蓝牙模块在SPP(串行端口协议)模式下工作。您可以使用新的SoftwareSerial库编写用于蓝牙通信的arduino代码。此库允许您通过蓝牙通过串行端口发送和接收数据。在这方面有很多例子可以让你开始

对于计算机上运行的Java程序,可以使用RXTX库或Java.comm库。这是一个包含使用RXTX库的各种示例的链接:。使用RXTX库,您可以通过串行端口发送和接收数据

使用RXTX库通过串口发送数据的java代码:(我还没有测试过)

接收数据的相应arduino代码(同样未测试):

#包括
int bluetoothTx=2;//蓝牙配对的TX-O引脚,Arduino D2
int bluetoothRx=3;//蓝牙配对的RX-I引脚,Arduino D3
软件串行蓝牙(bluetoothTx、bluetoothRx);
无效设置()
{
Serial.begin(9600);//以9600bps开始串行监视器
bluetooth.begin(115200);//蓝牙配对默认为115200bps
bluetooth.print(“$$”;//进入命令模式
延迟(100);//短暂延迟,等待大副发回指令
bluetooth.println(“U,9600,N”);//暂时将波特率更改为9600,无奇偶校验
//115200有时可能太快,NewSoftSerial无法可靠地中继数据
bluetooth.begin(9600);//在9600启动bluetooth串行
}
void循环()
{
if(bluetooth.available())//如果蓝牙发送了任何字符
{
//将蓝牙打印的任何字符发送到串行监视器
Serial.print((char)bluetooth.read());
}
/*if(Serial.available())//如果在串行监视器中键入了内容
{
//将串行监视器打印的任何字符发送到蓝牙
bluetooth.print((char)Serial.read());
}*/
}

这是一篇老文章,但我最近在arduino上设置了蓝牙模块,并将其与运行在Ubuntu上的Java程序连接。因此,让我分享一些我觉得有用的链接

默认情况下,BlueSMiRF蓝牙模块在SPP(串行端口协议)模式下工作。您可以使用新的SoftwareSerial库编写用于蓝牙通信的arduino代码。此库允许您通过蓝牙通过串行端口发送和接收数据。在这方面有很多例子可以让你开始

对于计算机上运行的Java程序,可以使用RXTX库或Java.comm库。这是一个包含使用RXTX库的各种示例的链接:。使用RXTX库,您可以通过串行端口发送和接收数据

使用RXTX库通过串口发送数据的java代码:(我还没有测试过)

接收数据的相应arduino代码(同样未测试):

#包括
int bluetoothTx=2;//蓝牙配对的TX-O引脚,Arduino D2
int bluetoothRx=3;//蓝牙配对的RX-I引脚,Arduino D3
软件串行蓝牙(bluetoothTx、bluetoothRx);
无效设置()
{
Serial.begin(9600);//以9600bps开始串行监视器
bluetooth.begin(115200);//蓝牙配对默认为115200bps
bluetooth.print(“$$”;//进入命令模式
延迟(100);//短暂延迟,等待大副发回指令
bluetooth.println(“U,9600,N”);//暂时将波特率更改为9600,无奇偶校验
//115200有时可能太快,NewSoftSerial无法可靠地中继数据
bluetooth.begin(9600);//在9600启动bluetooth串行
}
void循环()
{
if(bluetooth.available())//如果蓝牙发送了任何字符
{
//将蓝牙打印的任何字符发送到串行监视器
Serial.print((char)bluetooth.read());
}
/*if(Serial.available())//如果在串行监视器中键入了内容
{
//将串行监视器打印的任何字符发送到蓝牙
bluetooth.print((char)Serial.read());
}*/
}
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TwoWaySerialComm
{
    public TwoWaySerialComm()
    {
    super();
    }

    void connect ( String portName ) throws Exception
    {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if ( portIdentifier.isCurrentlyOwned() )
    {
        System.out.println("Error: Port is currently in use");
    }
    else
    {
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

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

            //InputStream in = serialPort.getInputStream();
            OutputStream out = serialPort.getOutputStream();

            //(new Thread(new SerialReader(in))).start();
            (new Thread(new SerialWriter(out))).start();

        }
        else
        {
            System.out.println("Error: Only serial ports are handled by this example.");
        }
    }     
    }

    /** */
    /*public static class SerialReader implements Runnable 
    {
    InputStream in;

    public SerialReader ( InputStream in )
    {
        this.in = in;
    }

    public void run ()
    {
        byte[] buffer = new byte[1024];
        int len = -1;
        try
        {
            while ( ( len = this.in.read(buffer)) > -1 )
            {
                System.out.print(new String(buffer,0,len));
            }
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }            
    }
    }*/

    /** */
    public static class SerialWriter implements Runnable 
    {
    OutputStream out;

    public SerialWriter ( OutputStream out )
    {
        this.out = out;
    }

    public void run ()
    {
        try
        {                
            int c = 0;
            while ( ( c = System.in.read()) > -1 )
            {
                this.out.write(c);
            }                
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }            
    }
    }

    public static void main ( String[] args )
    {
    try
    {
        (new TwoWaySerialComm()).connect("COM3");
    }
    catch ( Exception e )
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}
#include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  /*if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }*/

}