我修改了Minddroid github Android程序以讀取LEGO NXT(精彩設備!)上的傳感器。現在我想將藍牙信息讀取和寫入NXT運行的Mindstorms程序。 這樣我就可以運行一個NXT程序,並在Android要求它們時將結果/讀數發送給Android。如何使用藍牙在Android和正在運行的NXT程序之間進行交互
0
A
回答
2
我已經創建了一個NXT將數據發送回我的Android設備的項目。下面是一些代碼,應該工作:
這是所有的Android端代碼:
這是一個類,我寫的,將採取的連接,並通過藍牙通信護理。
public class Connector {
public static final String TAG = "Connector";
public static final boolean BT_ON = true;
public static final boolean BT_OFF = false;
public BluetoothAdapter bluetoothAdapter;
public BluetoothSocket bluetoothSocket;
public String address;
public Connector(String address) {
this.address = address;
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void setBluetooth(boolean state) {
if(state == Connector.BT_ON) {
// Check if bluetooth is off
if(this.bluetoothAdapter.isEnabled() == false)
{
this.bluetoothAdapter.enable();
while(this.bluetoothAdapter.isEnabled() == false) {
}
Log.d(Connector.TAG, "Bluetooth turned on");
}
}
// Check if bluetooth is enabled
else if(state == Connector.BT_OFF) {
// Check if bluetooth is enabled
if(this.bluetoothAdapter.isEnabled() == true)
{
this.bluetoothAdapter.disable();
while(this.bluetoothAdapter.isEnabled() == true) {
}
Log.d(Connector.TAG, "Bluetooth turned off");
}
}
}
public boolean connect() {
boolean connected = false;
BluetoothDevice nxt = this.bluetoothAdapter.getRemoteDevice(this.address);
try {
this.bluetoothSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
this.bluetoothSocket.connect();
connected = true;
}
catch (IOException e) {
connected = false;
}
return connected;
}
public Integer readMessage() {
Integer message;
if(this.bluetoothSocket!= null) {
try {
InputStreamReader input = new InputStreamReader(this.bluetoothSocket.getInputStream());
message = input.read();
Log.d(Connector.TAG, "Successfully read message");
}
catch (IOException e) {
message = null;
Log.d(Connector.TAG, "Couldn't read message");
}
}
else {
message = null;
Log.d(Connector.TAG, "Couldn't read message");
}
return message;
}
}
在您的活動課程中,您可以創建Connector
對象。在onCreate()方法,你必須連接到建立像這樣的NXT的連接:
// Establish a bluetooth connection to the NXT
this.connector = new Connector("00:16:53:12:B6:78");
this.connector.setBluetooth(Connector.BT_ON);
this.connector.connect();
現在讀從NXT(Integer對象),你可以做這樣的一條消息:
this.connector.readMessage();
要關閉連接:
this.connector.setBluetooth(Connector.BT_OFF);
這是所有NXT端代碼:
注意:下載leJOS爲所有代碼工作(leJOS將允許您在Java中編碼您的NXT)。
在主類中定義這兩個對象:
public static DataOutputStream dataOutputStream;
public static NXTConnection bluetoothConnection;
連接到您的手機:
bluetoothConnection = Bluetooth.waitForConnection();
bluetoothConnection.setIOMode(NXTConnection.RAW);
dataOutputStream = bluetoothConnection.openDataOutputStream();
將數據發送到手機中的Integer對象的形式:
dataOutputStream.write(100);
dataOutputStream.flush();
要斷開連接運行:
dataOutputStream.close();
bluetoothConnection.close();
我希望這會有所幫助。
1
我對藍牙命令有些困惑,但現在我發現你需要下載leJOS!
我通常會盡量避免與NXT上的固件混淆,但是java更容易處理!
對於任何有興趣的人,你可以發送命令到你的機器人的NXT本身的格式,雖然它不如上面列出的漂亮。有一個偉大的教程在這裏: http://www.robotappstore.com/Knowledge-Base/Programming-LEGO-NXT-Mindstorms/92.html
但是,如果你想下載的應用程序免費,這裏是一個: http://www.robotappstore.com/Apps/Lego-NXT-Mindstorms-Driver---Android-app.html?x=693A00AA-7F15-46E7-9616-8101068DB58D
還有一堆多,如果你只是搜索周圍有太多
希望這會有所幫助!
相關問題
- 1. 如何在類之間進行交互
- 2. 如何使用java與正在運行的進程進行交互
- 3. MergExt使用MergBLE進行iOS和Arduino之間的藍牙通信
- 4. 如何在python中與正在運行的進程進行交互?
- 5. 如何通過藍牙與iOS之外的設備進行交互
- 6. 在應用程序運行時期間保持藍牙/ WiFi
- 7. akka actor如何在線程之間進行交互
- 8. 藍牙和計算機之間的交互
- 9. 在不同的android設備上通過藍牙進行交互的應用程序?
- 10. 獲取數據並與正在運行的應用程序進行交互
- 11. 你如何使用PHP與正在運行的C++進程交互?
- 12. 用於與arduino進行藍牙通信的Android應用程序
- 13. 在ruby腳本和正在運行的C++程序之間進行通信
- 14. 如何在Android應用程序的命令行中與sqlite3進行交互?
- 15. 同時藍牙遙控(android)和運行程序
- 16. 如何在燒瓶應用程序運行時與它們進行交互?
- 17. 我如何根據GPS位置在web服務器和android應用程序之間進行交互?
- 18. 如何在Android上的應用程序之間進行通信
- 19. iOS和Android之間的藍牙傳輸?
- 20. Android中運行任務和運行進程之間的區別
- 21. Android應用程序和C#窗體應用程序之間的藍牙通信
- 22. 與正在運行的Flash進程交互?
- 23. 通過配對的Android和Raspberry PI之間的藍牙進行數據傳輸
- 24. 如何將android應用程序與樹莓派進行交互...?
- 25. 如何在Firefox插件和android進程之間進行通信?
- 26. 如何在Android中使用Sybase-iAnywhere-Blue-SDK-for-Android進行藍牙打印?
- 27. Android:如何處理Android和PC之間的藍牙連接
- 28. 在android中使用藍牙進行數據傳輸
- 29. 是否可以在ASP.Net頁面和Silverlight之間進行交互?
- 30. 使用應用程序進行藍牙掃描