简体   繁体   English

android和arduino uno之间的蓝牙串行通信

[英]Bluetooth serial communication between android and arduino uno

I am very much new to phonegap. 我对Phonegap非常陌生。 I need to send and recieve data via arduino and android mobile.I have minimum or better to say zero knowledge of how to do this. 我需要通过arduino和android mobile发送和接收数据。我至少或最好说零知识如何做到这一点。 Please guys do help me.Can't understand a thing by following this link. 请大家帮我。按此链接无法理解。 https://github.com/don/BluetoothSerial Please give step wise description if you guys have done this. https://github.com/don/BluetoothSerial如果您这样做的话,请给出逐步的说明。

first you need to buy a Bluetooth shield for your Ardunio uno if you do not have one. 首先,如果您没有蓝牙屏蔽,则需要为Ardunio uno购买蓝牙屏蔽。 The Bluetooth shield should have instructions on how to connect it to the Ardunio uno to use the serial ports. 蓝牙屏蔽罩应具有有关如何将其连接到Ardunio uno以便使用串行端口的说明。 Android has an excellent sample application that is distributed with the SDK, it is called BluetoothChat, you should be able to find it easy. Android具有随SDK一起分发的出色示例应用程序,称为BluetoothChat,您应该可以轻松找到它。 I modified the BluetoothChatService.java file to communicate with the Arduino board with few simple modifications to the code where you can use the app to connect to the Arduino board or any other Bluetooth device., here they are. 我修改了BluetoothChatService.java文件以与Arduino板通信,对代码进行了一些简单的修改,您可以在其中使用该应用程序连接到Arduino板或任何其他蓝牙设备。

I added this at the start of the class, the second UUID is used to connect to the Arduino board. 我在课程开始时添加了它,第二个UUID用于连接到Arduino板。

// Name for the SDP record when creating server socket
private static String NAME  = null;
private static final String NAME1 = "BluetoothChat";
private static final String NAME2 = "itead";

// Unique UUID for this application
private static UUID MY_UUID = null;
private static final UUID MY_UUID1 = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID2 = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

Also, I modified this method, 另外,我修改了这种方法

/**
 * Start the ConnectThread to initiate a connection to a remote device.
 * @param device  The BluetoothDevice to connect
 */
public synchronized void connect(BluetoothDevice device) {
    if (D) Log.d(TAG, "connect to: " + device);

    if(device.getName().indexOf("itead")!=-1)
    {
      MY_UUID = MY_UUID2;
      NAME    = NAME2;
    }
    else
    {
      MY_UUID = MY_UUID1;
      NAME    = NAME1;
    }
    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}


    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
}

You should be to connect and communicate with the Arduino board with the current few changes I made to the sample app. 您应该使用我对示例应用程序所做的一些更改来连接并与Arduino板通信。

Hope that works for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM