В конструкторе ConnectThread:
Код: Выделить всё
if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
try {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
} else {
Log.e(TAG, "Missing BLUETOOTH_CONNECT permission");
}
targetSocket = tmp;
Код: Выделить всё
bluetoothAdapter.cancelDiscovery();
try {
targetSocket.connect();
Log.i(TAG, "Connection successful!");
} catch (IOException e) {
Log.d(TAG, Log.getStackTraceString(e));
Log.e(TAG, "Could not connect; closing socket", e);
try {
targetSocket.close();
} catch (IOException e2) {
Log.e(TAG, "Could not close the client socket", e2);
}
}
Код: Выделить всё
ConnectThread connectThread = new ConnectThread(device, mBluetoothManager, MainActivity.this);
connectThread.start();
Что у меня в LOGCAT**
Код: Выделить всё
ConnectThread com.example.carcontroller D java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1170)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:1188)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:566)
at com.example.carcontroller.ConnectThread.run(ConnectThread.java:50)
ConnectThread com.example.carcontroller E Could not connect; closing socket
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1170)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:1188)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:566)
at com.example.carcontroller.ConnectThread.run(ConnectThread.java:50)
Подробнее здесь: https://stackoverflow.com/questions/798 ... n-java-app
Мобильная версия