2013-02-28 36 views
0

我正在开发一个应用程序,首先我必须搜索可用的蓝牙设备并进行连接。我已经完成了这项任务。之后,打开一个新的活动,其中有一个Edittext和一个Button。我开发了下面的代码。它没有给出任何输出。 我使用连接设备的MAC地址传递数据。在android中使用蓝牙进行数据传输

的java文件...

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文件

<?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> 

请你帮我......

这里是我的代码,它有问题..

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(); 
          } 





       } 
      }); 
+0

嗨,使用这个链接http://manojprasaddevelopers.blogspot.in/2012/02/bluetooth-data-transfer-example.html – itsrajesh4uguys 2013-02-28 10:08:35

+0

有这个环节也一试。 http://english.cxem.net/arduino/arduino5.php – itsrajesh4uguys 2013-02-28 10:10:28

+0

也发现了这一点。 http://www.androidcookbook.info/application-development/bluetooth-data-transfer-example.html – itsrajesh4uguys 2013-02-28 10:11:09

回答

1

这不回答你直接提问,但我注意到的一件事是,您有10000作为您的持续时间参数Toast.makeText(),这是无效的。它只接受Toast.LENGTH_SHORTToast.LENGTH_LONG,这实际上只是0或1.检查文档:http://developer.android.com/reference/android/widget/Toast.html

[编辑] 在你的代码中,我看到的另一个错误是你传递一个空StringgetRemoteDevice。试着做getRemoteDevice("00001101-0000-1000-8000-00805F9B34FB"),看看会发生什么。再次阅读文档:http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String)

+0

@Barney ... It在我的情况下无关紧要...我的实际问题是数据传输......但是,感谢您指向我... – 2013-02-28 10:18:45

+0

另外,在您的代码中,您可以在'MacAddress'上调用'getRemoteDevice',这是一个空的'String '。看我的编辑。 – Barney 2013-02-28 10:24:17

+0

@Barney ...意味着我必须通过UUID而不是Mac地址的连接设备? – 2013-02-28 10:29:04

0

包括连接功能。

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