我想將筆記本電腦中藍牙設備的數據發送到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. 通過藍牙將圖像從android發送到PC
- 3. 如何發送短信從PC到移動使用Java藍牙OBEX(bluecove api)?
- 4. 發送文件通過藍牙從PC到移動用C#
- 5. 從藍牙芯片讀取數據需要很長時間
- 6. 如何通過藍牙從芯片傳輸數據?
- 7. 使用藍牙從PC或手機發送命令的差異
- 8. 發送數據到藍牙打印機
- 9. 通過藍牙發送文件從PC到Android手機
- 10. 通過藍牙將數據從android傳輸到PC
- 11. Android藍牙編程:通過藍牙將事件/數據從藍牙耳機發送到Android手機。
- 12. 藍牙4.0設備可以將數據發送到藍牙2.0設備嗎?
- 13. 發送文件從PC到手機通過藍牙在Java中沒有配對
- 14. 如何通過藍牙從PC發送圖像到Android使用C#
- 15. 如何使用藍牙從android手機發送數據?
- 16. 通用藍牙功能使用藍牙設備發送數據android
- 17. 發送finger.fingerID到藍牙
- 18. 從Android設備發送數據到ESP8266 Wi-Fi芯片
- 19. 如何將藍牙數據發送到未知設備?
- 20. Movilizer - 將數據發送到藍牙打印機
- 21. 核心藍牙發送數據包
- 22. 通過藍牙發送數據包
- 23. 通過藍牙發送數據
- 24. 從iPhone 4S發送數據到藍牙設備
- 25. 使用USB將數據從PC發送到Android設備OTG
- 26. 使用帶PIC16F887的HC-06藍牙芯片?
- 27. 從遠程android藍牙接收文本數據到Windows XP PC
- 28. 使用Android + Arduino進行藍牙通信。 (僅將數據發送到Arduino)
- 29. 如何從LE藍牙芯片獲取UUID?
- 30. 使用藍牙連接並從Android設備發送數據到計算機?