当前位置: 代码迷 >> Android >> socket close! 什么原因罗致的呢
  详细解决方案

socket close! 什么原因罗致的呢

热度:97   发布时间:2016-05-01 13:27:53.0
socket close!! 什么原因导致的呢
08-09 22:01:27.945: E/mango(9898): +++start discovering+++
08-09 22:01:54.445: D/ActivityThread(9898): test selfappwidget getPackageInfo,packageInfo [email protected]
08-09 22:01:54.476: E/mango(9898): +++start discovering+++
08-09 22:01:58.257: I/System.out(9898): 新发现的设备:mango
08-09 22:01:58.273: E/mango(9898): +++与设备配对+++
08-09 22:01:58.273: E/mango(9898): 获取远程设备
08-09 22:01:58.289: I/System.out(9898): 客户端的socket连接开始
08-09 22:02:05.750: I/System.out(9898): [email protected]
08-09 22:02:05.750: I/System.out(9898): [email protected]
08-09 22:02:05.757: I/System.out(9898): 进入管理线程的函数:connected
08-09 22:02:05.812: I/System.out(9898): 进入clientthread,开始write数据流
08-09 22:02:05.812: I/System.out(9898): send:[[email protected]
08-09 22:02:05.820: W/System.err(9898): java.io.IOException: socket closed
08-09 22:02:05.820: W/System.err(9898): at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:316)
08-09 22:02:05.820: W/System.err(9898): at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
08-09 22:02:05.820: W/System.err(9898): at java.io.OutputStream.write(OutputStream.java:80)
08-09 22:02:05.820: W/System.err(9898): at com.card.mango.sendcard$ClientThread.<init>(sendcard.java:223)
08-09 22:02:05.820: W/System.err(9898): at com.card.mango.sendcard.connected(sendcard.java:202)
08-09 22:02:05.820: W/System.err(9898): at com.card.mango.sendcard$ConnectThread.run(sendcard.java:169)


代码如下



//客户端 需要开启蓝牙 搜索设备 建立socket write数据

package com.card.mango;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class sendcard extends Activity {

  // Local Bluetooth adapter
  private BluetoothAdapter mBluetoothAdapter = null;
  private ConnectThread mConnectThread;
  private ClientThread mClientThread;

   
  private static final String Name = "mango"; 
  String defaultname = "abc";
   
User user; //得到user实例和需要交换的数据
String exchange_name="0";
int exchange_image=0;
private TextView et_name;
private ImageView imageView;

   
  @Override 
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.exchange);
   
  // Get local Bluetooth adapter
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
  // If the adapter is null, then Bluetooth is not supported
  if (mBluetoothAdapter == null) {
  Toast.makeText(this, "蓝牙不可用", Toast.LENGTH_LONG).show();
  finish();
  return;
  }  
  if (!mBluetoothAdapter.isEnabled()) {
  //直接打开蓝牙设备
  mBluetoothAdapter.enable();
  }
   
  // Register for broadcasts when a device is discovered
  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  this.registerReceiver(mReceiver, filter);
  相关解决方案