简体   繁体   中英

Bluetooth serial communication between android and arduino uno

I am very much new to 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. 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.

first you need to buy a Bluetooth shield for your Ardunio uno if you do not have one. The Bluetooth shield should have instructions on how to connect it to the Ardunio uno to use the serial ports. Android has an excellent sample application that is distributed with the SDK, it is called BluetoothChat, you should be able to find it easy. 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.

I added this at the start of the class, the second UUID is used to connect to the Arduino board.

// 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.

Hope that works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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