获取java.io.IOException:读取失败,套接字可能关闭或在BluetoothSocket.connect()上超时

Mr.*_*obo 5 java android bluetooth

我正在尝试编写一个代码,该代码仅连接到运行Android 4.4 KitKat的Nexus 7上的(现在)配对设备.无论我尝试过多少东西,我仍然会遇到这个错误.这是我尝试过的最后一个代码,它似乎正在做我见过人们报告成功的所有内容.

谁能指出我做错了什么?

BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter adapter = bluetoothManager.getAdapter();//BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
BluetoothDevice bt = adapter.getBondedDevices().iterator().next();
BluetoothDevice actual = adapter.getRemoteDevice(bt.getAddress());
String str = "";
for(BluetoothDevice bd : adapter.getBondedDevices()) {
    str += bd.getName() + "\n";
}
str+= actual;

textView.setText(str);
BluetoothSocket socket = actual.createInsecureRfcommSocketToServiceRecord(MY_UUID);
adapter.cancelDiscovery();
socket.connect();
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print(message);
out.flush();
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以通过以下方式获取 Android < 3 的 UUID:

Method method = device.getClass().getMethod("getUuids", null);
ParcelUuid[] phoneUuids = (ParcelUuid[]) method.invoke(device, null);
SERIAL_UUID = phoneUuids[1].getUuid();
Run Code Online (Sandbox Code Playgroud)

或者:

UUID SERIAL_UUID = device.getUuids()[0].getUuid();
Run Code Online (Sandbox Code Playgroud)