android java.io.IOException:传输端点未连接
Posted
技术标签:
【中文标题】android java.io.IOException:传输端点未连接【英文标题】:android java.io.IOException: Transport endpoint is not connected 【发布时间】:2013-03-08 08:24:17 【问题描述】:我需要向蓝牙设备发送一个字符串。但是在发送时,我在java.io.OutputStream.write(byte[])
方法上得到了一个例外java.io.IOException: Transport endpoint is not connected
。
代码如下所示。该代码只是从配对设备列表中搜索特定设备并发送字符串。
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());
【问题讨论】:
【参考方案1】:编辑:
你需要连接到socket,在此之前你需要取消发现
btAdapter.cancelDiscovery();
socket.connect();
为 Kiran K J 工作的 UUID 是
00001105-0000-1000-8000-00805F9B34FB
你还没有得到蓝牙适配器,你刚刚声明了它。
使用静态工厂方法获取这样的蓝牙适配器
在 onCreate 中添加这个
btAdapter=BluetoothAdapter.getDefaultAdapter();
【讨论】:
这是 Set以上是关于android java.io.IOException:传输端点未连接的主要内容,如果未能解决你的问题,请参考以下文章
Gradle Plugin v0.13.1 后重复的 Zip 条目
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )