最佳实践-Java串行蓝牙连接HC-05
将Java应用程序连接到HC-05蓝牙模块(Arduino)的最佳实践是什么? 蓝湾还是别的什么?最佳实践-Java串行蓝牙连接HC-05,java,bluetooth,arduino,Java,Bluetooth,Arduino,将Java应用程序连接到HC-05蓝牙模块(Arduino)的最佳实践是什么? 蓝湾还是别的什么? 如果是这样的话,有没有什么例子可以推荐使用HC-05连接的Bluecove?不久前我也有同样的问题。与此同时,我找到了一种通过java与HC-05通信的方法。我正在使用blueCove-也许还有其他库,但这对我来说很有用。以下是我所做的: import java.io.IOException; import java.io.InputStream; import java.io.OutputStr
如果是这样的话,有没有什么例子可以推荐使用HC-05连接的Bluecove?不久前我也有同样的问题。与此同时,我找到了一种通过java与HC-05通信的方法。我正在使用blueCove-也许还有其他库,但这对我来说很有用。以下是我所做的:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class HC05 {
boolean scanFinished = false;
RemoteDevice hc05device;
String hc05Url;
public static void main(String[] args) {
try {
new HC05().go();
} catch (Exception ex) {
Logger.getLogger(HC05.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void go() throws Exception {
//scan for all devices:
scanFinished = false;
LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
String name = btDevice.getFriendlyName(false);
System.out.format("%s (%s)\n", name, btDevice.getBluetoothAddress());
if (name.matches("HC.*")) {
hc05device = btDevice;
System.out.println("got it!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void inquiryCompleted(int discType) {
scanFinished = true;
}
@Override
public void serviceSearchCompleted(int transID, int respCode) {
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
});
while (!scanFinished) {
//this is easier to understand (for me) as the thread stuff examples from bluecove
Thread.sleep(500);
}
//search for services:
UUID uuid = new UUID(0x1101); //scan for btspp://... services (as HC-05 offers it)
UUID[] searchUuidSet = new UUID[]{uuid};
int[] attrIDs = new int[]{
0x0100 // service name
};
scanFinished = false;
LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet,
hc05device, new DiscoveryListener() {
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
}
@Override
public void inquiryCompleted(int discType) {
}
@Override
public void serviceSearchCompleted(int transID, int respCode) {
scanFinished = true;
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
hc05Url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (hc05Url != null) {
break; //take the first one
}
}
}
});
while (!scanFinished) {
Thread.sleep(500);
}
System.out.println(hc05device.getBluetoothAddress());
System.out.println(hc05Url);
//if you know your hc05Url this is all you need:
StreamConnection streamConnection = (StreamConnection) Connector.open(hc05Url);
OutputStream os = streamConnection.openOutputStream();
InputStream is = streamConnection.openInputStream();
os.write("1".getBytes()); //just send '1' to the device
os.close();
is.close();
streamConnection.close();
}
}
一旦知道HC-05的url(btspp://...)您可以连接,而无需扫描所有设备,也无需搜索服务。这要快得多,代码减少到几行
下面是我的arduino代码,用于处理数据:在发送“1”时打开电路板LED(针脚13),在发送“0”时关闭:
/*
* Bluetooth-Modul anschliessen: +5, GND,
* Tx an Arduino Rx(Pin 0)
* Rx an Arduino Tx(Pin 1)
*
* Per Handy mit App 'ArduDroid by Techbitar':
* -Menü 'Connect me to a Bluetooth device
* -Send Data "A" oder "B" schaltet die LED auf Pin 13 aus bzw. an
* und gibt Rückmeldung 'LED:on' oder 'LEF:off'.
*
* https://www.youtube.com/watch?v=sXs7S048eIo
*
* Alternativ mit JavaCode: projekt 'MrBlue'
*/
int ledPin = 13;
int cmd = -1;
int flag = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
cmd = Serial.read();
flag = 1;
}
if (flag == 1) {
if (cmd == '0') {
digitalWrite(ledPin, LOW);
Serial.println("LED: off");
} else if (cmd == '1') {
digitalWrite(ledPin, HIGH);
Serial.println("LED: on");
} else {
Serial.print("unknown command: ");
Serial.write(cmd);
Serial.print(" (");
Serial.print(cmd, DEC);
Serial.print(")");
Serial.println();
}
flag = 0;
cmd = 65;
}
Serial.flush();
delay(100);
}
这是我和HC-05之间的协议:
我的Arduino Uno和HC-05之间的连接:
对于win7上的“输入代码”窗口,请使用:1234或0000在本期中,您能继续吗?我也对这个感兴趣。请告诉我们您是否取得了任何进展。我想我曾经用图书馆连接过蓝牙设备,因为这些蓝牙设备也会被列为串行设备。这是直截了当和容易做到的。唯一的问题是,你可能需要一些Windows驱动程序。我在Win7上看到“输入代码”窗口。。。我不知道该插入什么,因为HC-05实际上没有生成任何代码(我可以在哪里查看它?),我在某个地方读到“尝试使用代码1234或0000”。1234是我设备上的默认设置。我觉得你可能在引用某个地方的内容。请说出来源。此外,解释为什么这可能有助于改善您的帖子的印象,以及在语言细节上花费一些精力,如标点符号、大写字母。请看一下和
/*
* Bluetooth-Modul anschliessen: +5, GND,
* Tx an Arduino Rx(Pin 0)
* Rx an Arduino Tx(Pin 1)
*
* Per Handy mit App 'ArduDroid by Techbitar':
* -Menü 'Connect me to a Bluetooth device
* -Send Data "A" oder "B" schaltet die LED auf Pin 13 aus bzw. an
* und gibt Rückmeldung 'LED:on' oder 'LEF:off'.
*
* https://www.youtube.com/watch?v=sXs7S048eIo
*
* Alternativ mit JavaCode: projekt 'MrBlue'
*/
int ledPin = 13;
int cmd = -1;
int flag = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
cmd = Serial.read();
flag = 1;
}
if (flag == 1) {
if (cmd == '0') {
digitalWrite(ledPin, LOW);
Serial.println("LED: off");
} else if (cmd == '1') {
digitalWrite(ledPin, HIGH);
Serial.println("LED: on");
} else {
Serial.print("unknown command: ");
Serial.write(cmd);
Serial.print(" (");
Serial.print(cmd, DEC);
Serial.print(")");
Serial.println();
}
flag = 0;
cmd = 65;
}
Serial.flush();
delay(100);
}