'BluetoothServerSocket does not accept on android 11
I am trying to connect two devices with bluetooth.If server device is android 7 everything works, but if server device is android 11 unfortunately it does not accept the connection. While the server does not accept, the client connects.
Here my client side codes:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("action", action);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
bAdapter.startDiscovery();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("tag","######connecting########");
Log.d("nameTag",device.getName());
if (device != null){
BluetoothSocket socket = null;
try {
socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
socket.close();
//mmBuffer = new byte[1024];
//tmpIn = socket.getInputStream(mmBuffer);
Log.d("tag","######connected########");
//Log.d("message", String.valueOf(tmpIn));
}catch (Exception e){
e.printStackTrace();
}
byte[] bytes = "c".getBytes();
try {
Log.i("write", "write block");
//mmOutStream.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//showToast("Found device " + device.getName());
}
}
};
And my server side codes:
class serverSocketConnectThread extends Thread{
BluetoothAdapter attendanceTool;
BluetoothServerSocket serverSocket;
UUID APP_UUID;
String APP_NAME;
BluetoothSocket client = null;
serverSocketConnectThread(BluetoothAdapter attendanceTool, UUID APP_UUID, String APP_NAME){
this.attendanceTool = attendanceTool;
this.APP_NAME = APP_NAME;
this.APP_UUID = APP_UUID;
}
public void run(){
Log.i("Thread","ServerSocketConnectThread is started");
while (true){
try {
this.serverSocket = this.attendanceTool.listenUsingInsecureRfcommWithServiceRecord(APP_NAME, APP_UUID);
Log.i("Thread","serverSocket is initilazied");
client = this.serverSocket.accept();
Log.i("Thread","client is accepted");
boolean isConnected = connectedDevices.contains(client);
Log.i("Thread","device is found");
if (!isConnected){
connectedDevices.add(client);
Log.i("client", "CLIENT IS ADDED ");
Log.i("client", "CLIENT NAME İS " + client.getRemoteDevice().getName());
Log.i("connectedDevices", "TOTAL NUMBER OF THE CONNECTED DEVICES ARE " + connectedDevices.size());
}else{
Log.i("client","CLIENT IS ALREADY CONNECTED");
}
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Server side output:
W/icle_attendanc: Accessing hidden method Landroid/app/ResourcesManager;->overrideTokenDisplayAdjustments(Landroid/os/IBinder;Ljava/util/function/Consumer;)Z (blacklist, linking, denied)
W/icle_attendanc: Accessing hidden field Landroid/app/ResourcesManager$ActivityResources;->activityResources:Ljava/util/ArrayList; (greylist-max-o, linking, denied)
W/icle_attendanc: Accessing hidden method Landroid/content/res/Resources;->overrideDisplayAdjustments(Ljava/util/function/Consumer;)V (blacklist, linking, denied)
W/icle_attendanc: Accessing hidden method Landroid/app/ResourcesManager;->updateResourcesForActivity(Landroid/os/IBinder;Landroid/content/res/Configuration;IZ)V (greylist-max-o, linking, denied)
W/icle_attendanc: Accessing hidden method Landroid/app/ResourcesManager;->getOrCreateActivityResourcesStructLocked(Landroid/os/IBinder;)Landroid/app/ResourcesManager$ActivityResources; (greylist-max-o, linking, denied)
W/icle_attendanc: Redefining intrinsic method java.lang.Thread java.lang.Thread.currentThread(). This may cause the unexpected use of the original definition of java.lang.Thread java.lang.Thread.currentThread()in methods that have already been compiled.
W/icle_attendanc: Redefining intrinsic method boolean java.lang.Thread.interrupted(). This may cause the unexpected use of the original definition of boolean java.lang.Thread.interrupted()in methods that have already been compiled.
W/ActivityThread: Application com.example.dicle_attendance is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
W/System: A resource failed to call close.
W/System: A resource failed to call close.
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1507)
W/icle_attendanc: ClassLoaderContext classpath size mismatch. expected=0, found=1 (PCL[] | PCL[/data/data/com.example.dicle_attendance/code_cache/.overlay/base.apk/classes3.dex*1674889171])
W/icle_attendanc: Found duplicate classes, falling back to extracting from APK : /data/app/~~f2J17UahDvm6DqMaa9HlIw==/com.example.dicle_attendance-mJdVkWPBh5RI3t3oTuk1-A==/base.apk
W/icle_attendanc: NOTE: This wastes RAM and hurts startup performance.
W/icle_attendanc: Found duplicated class when checking oat files: 'Lcom/example/dicle_attendance/AttendanceTool$1;' in /data/app/~~f2J17UahDvm6DqMaa9HlIw==/com.example.dicle_attendance-mJdVkWPBh5RI3t3oTuk1-A==/base.apk!classes3.dex and /data/data/com.example.dicle_attendance/code_cache/.overlay/base.apk/classes3.dex
I/ForceDarkHelper: packageName:com.example.dicle_attendance getForceDarkConfigInfo null forceDarkAppConfigJsonString:null
I/ForceDarkHelper: ForceDarkConfigInfo: null
I/MiuiForceDarkConfig: MiuiForceDarkConfig setConfig density:2.750000, mainRule:17, secondaryRule:0, tertiaryRule:0
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/Looper: PerfMonitor longMsg : seq=3 plan=18:01:52.142 late=164ms wall=3739ms running=841ms runnable=4ms io=12ms h=android.app.ActivityThread$H w=110 procState=12
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
D/View: com.example.dicle_attendance initForcedUseForceDark: 1
D/DecorView[]: getWindowModeFromSystem windowmode is 1
D/DecorView: createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
W/icle_attendanc: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/icle_attendanc: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/BluetoothAdapter: enable(): BT already enabled!
I/Timeline: Timeline: Activity_launch_request time:716471162
I/Thread: ServerSocketConnectThread is started
W/Activity: Slow Operation: Activity com.example.dicle_attendance/.MainActivity onCreate took 1176ms
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
I/Thread: serverSocket is initilazied
I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@2ef5d55
D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
I/InputTransport: Create ARC handle: 0xb40000767bfb6dc0
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@d9579d1, this = DecorView@c68bc36[MainActivity]
W/Looper: PerfMonitor longMsg : seq=4 plan=18:01:52.146 late=3962ms wall=1823ms running=1666ms runnable=3ms io=14ms h=android.app.ActivityThread$H w=159 procState=12
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=1823ms latency=3962ms running=1666ms procState=12 ClientTransaction{ callbacks=[android.app.servertransaction.LaunchActivityItem] lifecycleRequest=android.app.servertransaction.ResumeActivityItem } historyMsgCount=3 (msgIndex=1 wall=156ms seq=1 running=135ms runnable=1ms io=10ms late=18ms h=android.app.ActivityThread$H w=162) (msgIndex=3 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=2ms latency=5836ms running=0ms procState=12 ClientTransaction{ callbacks=[android.app.servertransaction.TopResumedActivityChangeItem] } historyMsgCount=4 (msgIndex=1 wall=156ms seq=1 running=135ms runnable=1ms io=10ms late=18ms h=android.app.ActivityThread$H w=162) (msgIndex=3 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110) (msgIndex=4 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=3ms latency=5852ms running=3ms procState=12 ClientTransaction{ callbacks=[android.app.servertransaction.TopResumedActivityChangeItem] } historyMsgCount=5 (msgIndex=1 wall=156ms seq=1 running=135ms runnable=1ms io=10ms late=18ms h=android.app.ActivityThread$H w=162) (msgIndex=3 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110) (msgIndex=4 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=33ms latency=5869ms running=14ms procState=12 ClientTransaction{ lifecycleRequest=android.app.servertransaction.PauseActivityItem } historyMsgCount=6 (msgIndex=1 wall=156ms seq=1 running=135ms runnable=1ms io=10ms late=18ms h=android.app.ActivityThread$H w=162) (msgIndex=3 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110) (msgIndex=4 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=1ms latency=5421ms running=0ms procState=15 ClientTransaction{ callbacks=[android.app.servertransaction.TopResumedActivityChangeItem] } historyMsgCount=9 (msgIndex=1 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110) (msgIndex=2 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@d9579d1, this = DecorView@c68bc36[MainActivity]
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=27ms latency=5431ms running=26ms procState=15 ClientTransaction{ lifecycleRequest=android.app.servertransaction.ResumeActivityItem } historyMsgCount=10 (msgIndex=1 wall=3739ms seq=3 running=841ms runnable=4ms io=12ms late=164ms h=android.app.ActivityThread$H w=110) (msgIndex=2 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=5ms latency=470ms running=6ms procState=15 ClientTransaction{ callbacks=[android.app.servertransaction.TopResumedActivityChangeItem] } historyMsgCount=13 (msgIndex=1 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=15ms latency=483ms running=12ms procState=15 ClientTransaction{ lifecycleRequest=android.app.servertransaction.PauseActivityItem } historyMsgCount=14 (msgIndex=1 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
W/Looper: PerfMonitor doFrame : time=357ms vsyncFrame=0 latency=283ms procState=15 historyMsgCount=16 (msgIndex=1 wall=1823ms seq=4 running=1666ms runnable=3ms io=14ms late=3962ms h=android.app.ActivityThread$H w=159)
V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@d9579d1, this = DecorView@c68bc36[MainActivity]
W/Looper: PerfMonitor looperActivity : package=com.example.dicle_attendance/.MainActivity time=49ms latency=495ms running=48ms procState=15 ClientTransaction{ lifecycleRequest=android.app.servertransaction.StopActivityItem } historyMsgCount=13 (msgIndex=13 wall=357ms seq=20 running=334ms runnable=1ms late=283ms h=android.view.Choreographer$FrameHandler c=android.view.Choreographer$FrameDisplayEventReceiver)
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@d9579d1, this = DecorView@c68bc36[MainActivity]
E/ion: ioctl c0044901 failed with code -1: Invalid argument
Client side output
03/02 18:02:47: Launching 'app' on samsung SM-A310F.
App restart successful without requiring a re-install.
$ adb shell am start -n "com.example.client_side/com.example.client_side.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.client_side
Connected to process 10110 on device 'samsung-sm_a310f-310016b9f135132f'.
Connecting to com.example.client_side
Connected to the target VM, address: 'localhost:65393', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
E/Zygote: v2
I/libpersona: KNOX_SDCARD checking this for 10091
I/libpersona: KNOX_SDCARD not a persona
E/Zygote: accessInfo : 0
W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -4 -1 0 1]
I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.client_side
I/art: Late-enabling -Xcheck:jni
D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
W/ActivityThread: Application com.example.client_side is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1305)
W/System: ClassLoader referenced unknown path: /data/app/com.example.client_side-2/lib/arm
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
D/BluetoothAdapter: enable()
D/BluetoothAdapter: enable(): BT is already enabled..!
I/bluetooth: bluetooth enabled
I/filter: filter registered
D/BluetoothAdapter: startDiscovery
I/discovery: discovery started
D/Choreographer: init sf_choreo_doframe debug_Level : 0x4f4cdebug_game_running : false
D/ViewRootImpl@57ddd35[MainActivity]: ThreadedRenderer.create() translucent=false
D/InputTransport: Input channel constructed: fd=58
D/ViewRootImpl@57ddd35[MainActivity]: setView = DecorView@117b33b[MainActivity] touchMode=true
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/ViewRootImpl@57ddd35[MainActivity]: dispatchAttachedToWindow
D/ViewRootImpl@57ddd35[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][720,1280] result=0x27 surface={isValid=true -406667264} surfaceGenerationChanged=true
D/ViewRootImpl@57ddd35[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true -406667264} hwInitialized=true
D/ViewRootImpl@57ddd35[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 720, 1280) ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
D/ViewRootImpl@57ddd35[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
D/ViewRootImpl@57ddd35[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true -406667264}
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e5e2293 nm : com.example.client_side ic=null
I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=59
D/libEGL: loaded /vendor/lib/egl/libGLES_mali.so
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [720x1280]-format:1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@2bd8dce nm : com.example.client_side ic=null
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.device.action.FOUND
D/tag: ######connecting########
D/nameTag: Redmi Note 9
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/tag: ######connected########
I/write: write block
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.device.action.FOUND
D/tag: ######connecting########
D/nameTag: Redmi Note 9
I/art: Do partial code cache collection, code=29KB, data=30KB
I/art: After code cache collection, code=28KB, data=30KB
I/art: Increasing code cache capacity to 128KB
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/tag: ######connected########
I/write: write block
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.device.action.FOUND
D/tag: ######connecting########
D/nameTag: Redmi Note 9
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/tag: ######connected########
I/write: write block
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.device.action.FOUND
D/tag: ######connecting########
D/nameTag: Redmi Note 9
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/tag: ######connected########
I/write: write block
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
D/action: android.bluetooth.device.action.FOUND
D/tag: ######connecting########
D/nameTag: Redmi Note 9
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/tag: ######connected########
I/write: write block
D/action: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BluetoothAdapter: startDiscovery
D/action: android.bluetooth.adapter.action.DISCOVERY_STARTED
The outputs are obtained when server is android 11 and client is android 7
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
