Hello,
I try to build a bluetooth connection by android studio. The mac of HC05 (bluetooth PCB) was paired with my samsung android system. When I run the code as below, it works for Android 11 (API 31), and Android 12. But when I used Android 13 (API 33) to run the code below, the socket.connect() always runs fail and throws
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
My targetSdk setting in build.gradle is equal to 33. And I have used the permission of “android.permission.BLUETOOTH_CONNECT” and “android.permission.BLUETOOTH”, etc. in AndroidManifest.xml. My connection type is RFCOMM for bluetooth. How can I do a bluetooth connection in Android 13? Thank you.
public class MainActivity extends AppCompatActivity {
public BluetoothAdapter mBluetoothAdapter;
public BluetoothManager bluetoothManager;
public BluetoothSocket socket;
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothDevice dev = mBluetoothAdapter.getRemoteDevice(MAC);
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
socket = dev.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
}
}