简体   繁体   中英

Data transfer using Bluetooth in android

I am developing an application in which first I have to search available Bluetooth devices and make connection. I have done this task. After this, one new Activity opens in which there is one Edittext and one Button. I have developed the below code. It is not giving any output. I am passing data using MAC address of connected device.

java file...

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class NewMessage extends Activity {

    Button btn;
    EditText et;
    String message1="Hello";//on button click by default

    BluetoothAdapter mBluetoothAdapter1 = null;;

    // BluetoothAdapter mBluetoothAdapter;

    public static String MacAddress;

    @Override
    public void onCreate(Bundle mSavedInstanceState) {
        super.onCreate(mSavedInstanceState);
        setContentView(R.layout.message);

        btn = (Button) findViewById(R.id.btn);
        et = (EditText) findViewById(R.id.et);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mBluetoothAdapter1 = BluetoothAdapter.getDefaultAdapter();

                byte[] toSend=message1.getBytes();

                try
                {
                    final UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
                    BluetoothDevice device=mBluetoothAdapter1.getRemoteDevice(MacAddress);
                    BluetoothSocket socket=device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
                    OutputStream mmout=socket.getOutputStream();
                    mmout.write(toSend);
                    mmout.flush();
                    mmout.close();
                    socket.close();
                    Toast.makeText(getBaseContext(), MacAddress, 10000).show();
                }
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }




            }
        });

    }

    /*private void sendDataToPairedDevice(String message ,BluetoothDevice device){       
           byte[] toSend = message.getBytes();
            try 
            {
                BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
                OutputStream mmOutStream = socket.getOutputStream();
                mmOutStream.write(toSend);
                // Your Data is sent to  BT connected paired device ENJOY.
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

}

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:hint="@string/hint">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/send" />

</LinearLayout>

Please do help me...

Here is my code, which has the problem..

public void onClick(View v) {
                    // TODO Auto-generated method stub




                            mBluetoothAdapter1 = BluetoothAdapter.getDefaultAdapter();

                            byte[] toSend=message1.getBytes();

                            try
                            {

                                final UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
                                BluetoothDevice device=mBluetoothAdapter1.getRemoteDevice(MacAddress);
                                BluetoothSocket socket=device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
                                OutputStream mmout=socket.getOutputStream();
                                mmout.write(toSend);
                                mmout.flush();
                                mmout.close();
                                socket.close();

                            }
                            catch (IOException e) 
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }





                }
            });

This doesn't answer your question directly but one thing I am noticing is that you have 10000 as your duration parameter for Toast.makeText() , which is invalid. It only accepts Toast.LENGTH_SHORT or Toast.LENGTH_LONG , which are actually just 0 or 1. Check the docs: http://developer.android.com/reference/android/widget/Toast.html

[ EDIT ] Another error I see in your code is you pass an empty String to getRemoteDevice . Try doing getRemoteDevice("00001101-0000-1000-8000-00805F9B34FB") and see what happens. Again, read the docs: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String)

Include the connect function.

BluetoothSocket socket=device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
socket.connect();  // here
OutputStream mmout=socket.getOutputStream();

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