我想将笔记本电脑中蓝牙设备的数据发送到 BlueSMiRF 蓝牙芯片。我以前从未处理过蓝牙编程,所以我需要有关如何开始和使用什么的指导。我正在研究 java,但如果有更好的选择,我会研究它。
问问题
1106 次
1 回答
0
这是一篇旧文章,但我最近在 arduino 上设置蓝牙模块并将其与在 Ubuntu 上运行的 Java 程序连接。所以让我分享一些我觉得有帮助的链接。
默认情况下,BlueSMiRF 蓝牙模块工作在 SPP(串行端口协议)模式。您可以使用新的 SoftwareSerial 库来编写用于蓝牙通信的 arduino 代码。该库允许您通过蓝牙通过串行端口发送和接收数据。http://arduino.cc/en/Reference/SoftwareSerial有很多示例可以帮助您入门。
对于在计算机上运行的 Java 程序,您可以使用 RXTX 库或 java.comm 库。这是一个包含使用 RXTX 库的各种示例的链接:http ://rxtx.qbang.org/wiki/index.php/Examples 。使用 RXTX 库,您可以通过串行端口发送和接收数据。
使用RXTX库通过串口发送数据的java代码:(我没有测试过)
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();
}
}
}
用于接收数据的相应arduino代码(再次未经测试):
#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());
}*/
}
于 2013-03-16T22:58:57.927 回答