2012-01-08 23 views
1

私のラップトップのBluetoothデバイスからBlueSMiRF Bluetoothチップにデータを送信します。私は前にブルートゥースのプログラミングを扱ったことがないので、私はどのように起動し、何を使用するかについてのガイダンスが必要です。私はJavaで作業していますが、より良い選択肢があれば、私はそれに取り組んでいます。PCからBluetoothチップにデータを送信するBlueSMiRF with java

答えて

0

これは古い投稿ですが、私は最近、arduinoでbluetoothモジュールを設定し、Ubuntu上で実行されているJavaプログラムとインターフェイスしていました。だから私が助けてくれたリンクをいくつか共有してみましょう。

デフォルトでは、BlueSMiRF BluetoothモジュールはSPP(シリアルポートプロトコル)モードで動作します。新しいSoftwareSerialライブラリを使用して、ブルートゥース通信用のarduinoコードを記述することができます。このライブラリを使用すると、ブルートゥース経由でシリアルポート経由でデータを送受信できます。 http://arduino.cc/en/Reference/SoftwareSerial多くの例が用意されています。

コンピュータで実行されているJavaプログラムでは、RXTXライブラリまたはjava.commライブラリを使用できます。これは、RXTXライブラリを使用したさまざまな例を含むリンクです:http://rxtx.qbang.org/wiki/index.php/Examples。 RXTXライブラリを使用すると、シリアルポート経由でデータを送受信できます。

Javaコード、シリアルポート経由でデータを送信するためにRXTXライブラリを使用して:(私はそれをテストしていない)

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()); 
    }*/ 

} 
関連する問題