Android的java.io.IOException异常:运输端点没有连接 [英] android java.io.IOException: Transport endpoint is not connected

查看:812
本文介绍了Android的java.io.IOException异常:运输端点没有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字符串发送到蓝牙设备。但在发送,我得到一个Exeption java.io.IOException异常:运输端点没有连接 java.io.OutputStream.write(字节[ ])方法。

在code是如下图所示。在code刚刚从配对的设备列表中搜索特定设备发送的字符串。

 公共类MainActivity延伸活动{    TextView中出;
    私有静态最终诠释REQUEST_ENABLE_BT = 1;
    私人BluetoothAdapter btAdapter;
    私人的ArrayList<&BluetoothDevice类GT; btDeviceList =新的ArrayList<&BluetoothDevice类GT;();
    私人ArrayAdapter<串GT; mPairedDevicesArrayAdapter;
    私人ArrayAdapter<串GT; mNewDevicesArrayAdapter;
    BluetoothDevice类设备1;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        OUT =(的TextView)findViewById(R.id.out);
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        ListpairedDevices();
    }    / *活动结束时该程序被调用。 * /
    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);
        如果(要求code == REQUEST_ENABLE_BT){
            CheckBTState();
        }
    }    公共无效ListpairedDevices(){
        SET<&BluetoothDevice类GT; pairedDevices = btAdapter.getBondedDevices();        //如果有配对的设备,每一个添加一个ArrayAdapter
        如果(pairedDevices.size()大于0){
            out.append(\\ nPaired设备\\ n);
            对于(BluetoothDevice类设备:pairedDevices){
                out.append(\\ n Device123:+ device.getName()+,+设备);
                //mPairedDevicesArrayAdapter.add(\"\
Device123:+ device.getName()+,+设备);
                串的dv = device.toString();
                如果(dv.contains(00:1B:EE:82:31:1E))
                {
                设备1 =设备;
                }
            }
        }其他{
            out.append(\\ n无美嘉设备);
        }
        out.append(\\ nDiscovered设备);
    }    公共无效sendtext(视图v){//按钮点击    SET<&BluetoothDevice类GT; BD = btAdapter.getBondedDevices();
    sendDataToPairedDevice(MESSAGE1);
    }
    私人无效sendDataToPairedDevice(字符串消息){
        字节[] = toSend message.getBytes();
        尝试{
         UUID applicationUUID = UUID.fromString(8ce255c0-200a-11e0-ac64-0800200c9a66);
         的BluetoothSocket插座= device1.createInsecureRfcommSocketToServiceRecord(applicationUUID);
         OutputStream的mmOutStream = socket.getOutputStream();
         mmOutStream.write(toSend);
     }赶上(IOException异常五){
         Log.e(写入期间异常,e.toString());
     }
 }
}


解决方案

编辑:

您需要连接到插座,在这之前,你需要取消的发现

  btAdapter.cancelDiscovery();
 socket.connect();

对于UUID基兰ķĴ工作是

00001105-0000-1000-8000-00805F9B34FB


您还没有拿到蓝牙适配器,你刚才宣布它。

获取蓝牙适配器这样使用静态工厂方法

在的onCreate添加此

  btAdapter = BluetoothAdapter.getDefaultAdapter();

I need to send a string to a bluetooth device. But while sending,I am getting an Exeption java.io.IOException: Transport endpoint is not connected on java.io.OutputStream.write(byte[]) method.

The Code Is as shown below. The code just search a specific device from the paired device list and send string.

public class MainActivity extends Activity {

    TextView out;
    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter;
    private ArrayList<BluetoothDevice> btDeviceList = new ArrayList<BluetoothDevice>();
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
    private ArrayAdapter<String> mNewDevicesArrayAdapter;
    BluetoothDevice device1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        out = (TextView) findViewById(R.id.out);
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        ListpairedDevices();
    }

    /* This routine 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) {
            CheckBTState();
        }
    }

    public void ListpairedDevices() {
        Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

        // If there are paired devices, add each one to the ArrayAdapter
        if (pairedDevices.size() > 0) {
            out.append("\nPaired Devices \n");
            for (BluetoothDevice device : pairedDevices) {
                out.append("\n  Device123: " + device.getName() + "," + device);
                //mPairedDevicesArrayAdapter.add("\n  Device123: " + device.getName() + "," + device);
                String dv=device.toString();
                if(dv.contains("00:1B:EE:82:31:1E"))
                {
                device1=device;
                }
            }
        } else {
            out.append("\nNo Pared Device");
        }
        out.append("\nDiscovered Devices");
    }

    public void sendtext(View v) {//button click

    Set<BluetoothDevice> bd=btAdapter.getBondedDevices();
    sendDataToPairedDevice("message1");
    }
    private void sendDataToPairedDevice(String message ){       
        byte[] toSend = message.getBytes();
        try {
         UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
         BluetoothSocket socket = device1.createInsecureRfcommSocketToServiceRecord(applicationUUID);
         OutputStream mmOutStream = socket.getOutputStream();
         mmOutStream.write(toSend);
     } catch (IOException e) {
         Log.e( "Exception during write", e.toString());
     }
 }
}

解决方案

Edit:

You need to connect to the socket, before that you need to cancel the discovery

 btAdapter.cancelDiscovery();
 socket.connect();

UUID worked for Kiran K J is

00001105-0000-1000-8000-00805F9B34FB


You Haven't get the Bluetooth Adapter, you have just declared it.

Get the bluetooth adapter like this using static factory method

Add this in onCreate

btAdapter=BluetoothAdapter.getDefaultAdapter();

这篇关于Android的java.io.IOException异常:运输端点没有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆