Java 通过蓝牙传输字符串

Java 通过蓝牙传输字符串,java,android,eclipse,bluetooth,Java,Android,Eclipse,Bluetooth,我们可以在文本视图中列出配对的蓝牙设备。该应用程序有两个EditText,一个接受字符串消息,另一个接受目标设备的MAC地址,还有一个发送按钮将消息发送到目标地址,但当单击发送按钮时,该应用程序将关闭。请帮助我们。 提前谢谢。 包com.example.javapoint import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view

我们可以在文本视图中列出配对的蓝牙设备。该应用程序有两个EditText,一个接受字符串消息,另一个接受目标设备的MAC地址,还有一个发送按钮将消息发送到目标地址,但当单击发送按钮时,该应用程序将关闭。请帮助我们。 提前谢谢。 包com.example.javapoint

     import android.os.Bundle;

 import android.app.Activity;  
  import android.util.Log;
   import android.view.Menu;  
   import android.view.View;

    import java.io.IOException;
 import java.io.OutputStream;
  import java.util.Set; 
 import java.util.UUID;

   import android.bluetooth.BluetoothAdapter;  
     import android.bluetooth.BluetoothDevice;  
     import android.bluetooth.BluetoothSocket;
   import android.content.Intent;  
    import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;  
    import android.view.View.OnClickListener;


   public class MainActivity extends Activity {
 TextView textview1;  
 UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

 private static final int REQUEST_ENABLE_BT = 1;  
 BluetoothAdapter btAdapter;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textview1 = (TextView)findViewById(R.id.textView1);


    // Getting the Bluetooth adapter  
    btAdapter = BluetoothAdapter.getDefaultAdapter();  
    textview1.append("\nAdapter: " + btAdapter);  


        CheckBluetoothState();  

  }  

  /* It is called when an activity completes.*/  
  @Override  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);  
    if (requestCode == REQUEST_ENABLE_BT) {  
      CheckBluetoothState();  
    }  
  }  

  @Override  
  protected void onDestroy() {  
    super.onDestroy();  
  }  

  private void CheckBluetoothState() {  
    // Checks for the Bluetooth support and then makes sure it is turned on  
    // If it isn't turned on, request to turn it on  
    // List paired devices  
    if(btAdapter==null) {   
      textview1.append("\nBluetooth NOT supported. Aborting.");  
      return;  
    } else {  
      if (btAdapter.isEnabled()) {  
        textview1.append("\nBluetooth is enabled...");  

        // Listing paired devices  
        textview1.append("\nPaired Devices are:");  
        Set<BluetoothDevice> devices = btAdapter.getBondedDevices();  
        for (BluetoothDevice device : devices) {  
          textview1.append("\n  Device: " + device.getName() + ", " + device);  
        }  
      } else {  
        //Prompt user to turn on Bluetooth  
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
      }  
    }  


    Button sbtn=(Button)findViewById(R.id.btn2);
    final EditText et=(EditText)findViewById(R.id.message);
final String msg=et.getText().toString();
     final EditText et1=(EditText)findViewById(R.id.macaddress);
    final String mac=et1.getText().toString();
     sbtn.setOnClickListener(new OnClickListener()
        {




            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 byte[] toSend = msg.getBytes();


                 try {

                     BluetoothDevice mBluetoothDevice =    btAdapter.getRemoteDevice(mac);
BluetoothSocket socket       =mBluetoothDevice.createInsecureRfcommSocketToServiceRecord(applicationUUID);
                        socket.connect();
    OutputStream mmOutStream = socket.getOutputStream();
                        mmOutStream.write(toSend);
                        socket.close();
    // Your Data is sent to  BT connected paired device ENJOY.
                    } catch (IOException e) {

                    }
            }
        });


    }






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  }
导入android.os.Bundle;
导入android.app.Activity;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.view.view.OnClickListener;
公共类MainActivity扩展了活动{
文本视图文本视图1;
UUID applicationuid=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
私有静态最终整数请求_ENABLE_BT=1;
蓝牙适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1=(TextView)findViewById(R.id.textview1);
//获取蓝牙适配器
btAdapter=BluetoothAdapter.getDefaultAdapter();
textview1.append(“\nAdapter:+btAdapter”);
CheckBluetoothState();
}  
/*当活动完成时调用它。*/
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
如果(requestCode==REQUEST\u ENABLE\u BT){
CheckBluetoothState();
}  
}  
@凌驾
受保护的空onDestroy(){
super.ondestory();
}  
私有void CheckBluetoothState(){
//检查蓝牙支持,然后确保它已打开
//如果未打开,请请求打开它
//列出配对设备
如果(btAdapter==null){
textview1.append(“\n不支持Bluetooth。正在中止”);
回来
}否则{
如果(btAdapter.isEnabled()){
textview1.append(“\n蓝牙已启用…”);
//列出配对设备
textview1.append(“\n需要的设备是:”);
Set devices=btAdapter.getBondedDevices();
对于(蓝牙设备:设备){
textview1.append(“\n设备:“+Device.getName()+”,“+Device”);
}  
}否则{
//提示用户打开蓝牙
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}  
}  
按钮sbtn=(按钮)findViewById(R.id.btn2);
最终EditText et=(EditText)findViewById(R.id.message);
最后一个字符串msg=et.getText().toString();
最终EditText et1=(EditText)findViewById(R.id.macaddress);
最后一个字符串mac=et1.getText().toString();
sbtn.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
字节[]toSend=msg.getBytes();
试一试{
BluetoothDevice mBluetoothDevice=btAdapter.getRemoteDevice(mac);
BluetoothSocket=mBluetoothDevice.CreateSecurerCommsocketToServiceRecord(ApplicationUID);
socket.connect();
OutputStream mmOutStream=socket.getOutputStream();
mmOutStream.write(toSend);
socket.close();
//您的数据将发送到BT连接的配对设备。
}捕获(IOE异常){
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}

logcat异常stacktrace,please.03-28 02:19:58.160:E/BluetoothAdapter(794):蓝牙绑定器为空03-28 02:19:59.550:D/gralloc_goldfish(794):未检测到GPU仿真的仿真器。03-28 02:22:48.401:I/编舞(794):跳过了54帧!应用程序可能在其主线程上做了太多工作。03-28 02:22:57.651:I/编舞(794):跳过52帧!应用程序可能在其主线程上做了太多工作。这些错误已记录在日志中。请编辑问题,而不是作为评论发布。查找
致命异常
。未找到致命异常