我想将笔记本电脑中蓝牙设备的数据发送到BlueSMiRF蓝牙芯片。我从来没有处理过蓝牙编程,所以我需要关于如何启动和使用的指导。我正在研究java,但如果有更好的选择,我会努力。使用java将数据从pc发送到蓝牙芯片BlueSMiRF
1
A
回答
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代码:(I没有测试它)
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());
}*/
}
相关问题
- 1. 使用Core蓝牙将数据从计算机发送到iOS
- 2. 如何发送短信从PC到移动使用Java蓝牙OBEX(bluecove api)?
- 3. 发送文件通过蓝牙从PC到移动用C#
- 4. 从蓝牙芯片读取数据需要很长时间
- 5. 通过蓝牙将数据从android传输到PC
- 6. 蓝牙4.0设备可以将数据发送到蓝牙2.0设备吗?
- 7. 如何通过蓝牙从PC发送图像到Android使用C#
- 8. 通用蓝牙功能使用蓝牙设备发送数据android
- 9. 发送finger.fingerID到蓝牙
- 10. 从Android设备发送数据到ESP8266 Wi-Fi芯片
- 11. Movilizer - 将数据发送到蓝牙打印机
- 12. 通过蓝牙发送数据包
- 13. 从iPhone 4S发送数据到蓝牙设备
- 14. 使用带PIC16F887的HC-06蓝牙芯片?
- 15. 使用蓝牙连接并从Android设备发送数据到计算机?
- 16. 如何使用蓝牙将文件发送到手机?
- 17. 从WP7发送数据到PC
- 18. 从android手机发送数据到PC?
- 19. 如何将数据从一个活动发送到另一个活动。 (蓝牙)
- 20. 如何通过蓝牙将实时数据流从Arduino发送到手机
- 21. 将数据从IntentService发送到片段
- 22. 使用蓝牙来接收文件 - 从手机到PC
- 23. 使用蓝牙将数据从Arduino传输到iPhone
- 24. 使用Android设备通过SPP通过蓝牙发送数据
- 25. 通过蓝牙使用GameKit崩溃发送数据
- 26. 如何在iphone中使用蓝牙发送和接收数据?
- 27. 如何通过蓝牙将通知从arduino发送到iPhone?
- 28. 蓝牙文件发送
- 29. Android蓝牙发送文字
- 30. Android蓝牙文件发送