socket关闭了?如果发送端关闭,接受端继续接受的话,应该会报这样的错误

解决方案 »

  1.   

    没有关闭 我试过 把“textView4.setText(message);  ”删除调,只保留Log.d("asfasfd",message);   在logcat里 都能显示 接收到的数据。。 所以 我觉得 不是 端口的问题 :(
      

  2.   

    目前是 textview的值只能更改一次 再次发送更改值时  就程序关闭了  :( 
      

  3.   

    package com.example.bluetoothtest;import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Random;
    import java.util.Set;import android.R.integer;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothServerSocket;
    import android.bluetooth.BluetoothSocket;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.os.Build;
    import java.util.UUID; public class MainActivity extends ActionBarActivity {
    private Button button;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice bluetoothDevice;
    private BluetoothSocket bluetoothSocket;
    private BluetoothServerSocket bluetoothServerSocket;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private OutputStream outputStream;
    private InputStream inputStream;
    private Thread thread;
    private Handler handler;
    private final Random mGenerator = new Random();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
    .add(R.id.container, new PlaceholderFragment()).commit();
    }
    } @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    String message = (String)msg.obj;
                 Log.d("asfasfd",String.valueOf(msg.what));  
                        textView4.setText(message); 
                }  

    };
    button = (Button)findViewById(R.id.button1);
    textView1 = (TextView)findViewById(R.id.textView1);
    textView2 = (TextView)findViewById(R.id.textView2);
    textView3 = (TextView)findViewById(R.id.textView3);
    textView4 = (TextView)findViewById(R.id.textView4);
    textView4.setText("asdfasdfasfd");
    thread = new Thread(new inputlisner());
            button.setOnClickListener(new buttonlisner());
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if(!bluetoothAdapter.enable())
            {
             Intent intent = new Intent(bluetoothAdapter.ACTION_REQUEST_ENABLE);
             startActivityForResult(intent, 0x1);
            }else
            {
             Set<BluetoothDevice> deviceaddress = bluetoothAdapter.getBondedDevices();
             if(deviceaddress.size() < 0 )
             {
             textView1.setText("没有匹配的设备,请进行匹配");
             }else
             {   
             textView1.setText("匹配设备成功1");
             try{
             textView2.setText("远端舍必获取地址成功");
                bluetoothDevice = bluetoothAdapter.getRemoteDevice(deviceaddress.iterator().next().getAddress());
            
             }catch (Exception e) {
    // TODO: handle exception
             textView2.setText("获取地址失败");
    }
             try{
            
             bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
             textView3.setText("创建串口端口成功");
             }catch (Exception e) {
             textView3.setText("创建串口端口失败");
    // TODO: handle exception
    }
             try{
             bluetoothSocket.connect();
             textView3.setText("设备链接成功");
             }catch (Exception e) {
             textView3.setText("设备链接失败");
    // TODO: handle exception
    }
             }
            
            }
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    } @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    } /**
     * A placeholder fragment containing a simple view.
     */
    class buttonlisner implements OnClickListener, android.view.View.OnClickListener{
    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

    }
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if(!thread.isAlive()){
    thread.start();
    }//判断现成是否存活或者开启
    try{
    outputStream = bluetoothSocket.getOutputStream();
    byte[] buffer;
    String message = "1";
    buffer = message.getBytes();
        outputStream.write(buffer);
    }catch (Exception e) {
    // TODO: handle exception
    }

    }
    }
    class inputlisner extends Thread{
        @Override
    public void run() {
    // TODO Auto-generated method stub
         Message message = Message.obtain();
    int bytes = 0;
    while(true)
    {
    try{

    inputStream = bluetoothSocket.getInputStream();
    bytes = inputStream.read();
    message.obj = String.valueOf(bytes-48);

    }catch (Exception e) {
    // TODO: handle exception
    }
    handler.sendMessage(message);
    }
    }
    }
    public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
    } @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
    false);
    return rootView;
    }
    }}
      

  4.   

    我看是消息队列的问题。
    你把  Message message = Message.obtain(); 这个代码 放到while循环里面试试。
      

  5.   

    解决了 谢谢 birdsaction