android 10 is crashed when starting bluetooth service



in a couple of days before, I wrote some lines of code to connect application to a HC-05(a Bluetooth module) via a service. I know that a simple service can not be alive in a android 8+. so I modify my service using some free tutorials available on YouTube channels like the following:

https://www.youtube.com/watch?v=BXwDM5VVuKA

android 7- don’t have any problem but android 10 crashes when I clicked on “start service” button.

I bring for you some sections of my code.

onStartCommand in service:

public int onStartCommand(Intent intent, int flags, int startId) {
    createNotificationChannel();
    Intent intent1=new Intent(this,MainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent1,0);
    Notification notification=new NotificationCompat.Builder(this,"ChannelId1").setContentTitle("mY TITLE")
            .setContentText("our app").setSmallIcon(R.drawable.and).setContentIntent(pendingIntent).build();


    Log.d("PrinterService", "Onstart Command");
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
        
        deviceName=intent.getStringExtra("deviceName");

        Set<BluetoothDevice> bt=mBluetoothAdapter.getBondedDevices();
        Log.i("3","thread id:n"+"service CONNECTED"+" "+ bt.size());
        if (bt.size()>0){
            for (BluetoothDevice device:bt){
                if(device.getName().equals(deviceName)){
                    String macAddress=device.getAddress();
                    if (macAddress != null && macAddress.length() > 0) {
                        connectToDevice(macAddress);
                        Log.i("3","thread id:n"+"service CONNECTED");
                    } else {
                        stopSelf();

                        startForeground(1,notification);
                        return START_STICKY;
                    }
                }
            }

        }

    }
    String stopservice = intent.getStringExtra("stopservice");
    if (stopservice != null && stopservice.length() > 0) {
        stop();
    }
    startForeground(1,notification);
    return START_STICKY;
}

and ‘createNotificationChannel()’ function in defined here:

private void createNotificationChannel() {
    if(Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
        NotificationChannel notificationChannel=new NotificationChannel("ChannelId1","Foreground notification", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager manager=getSystemService(NotificationManager.class);
        manager.createNotificationChannel(notificationChannel);
    }
}

onClick method for buttonIn (in order to start service) is here:

public void onClick(View v) {
    //first
    if (v.getId()==R.id.buttonIn){
        buttinEnter.setEnabled(false);
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
            startForegroundService(intentService);
        }else {
            startService(intentService);
        }
        mStopLoop=true;
        //second
        bind_service();
        //third
        

        Handler handler2 = new Handler();
        handler2.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (PrinterService.started==1) {
                    goto_next();//going to the next activity
                }else {
                    buttinEnter.setEnabled(true);
                    Toast.makeText(getApplicationContext(),"you are not connected. turn on your bluetooth on your phone and POWER on device.",Toast.LENGTH_LONG).show();
                    isServiceBound=false;
                }
            }
        }, 5000);
        
    }
    
    }

so can anyone solve this problem with android 10.

Answer

Android 10 has some limitation for implementing Bluetooth. For example, you need to allow some permission related to location. Below text is adopted from the following website:

https://www.journaldev.com/28028/android-10-location-permissions

Android 10 Location Permissions

With the introduction of Android 10, besides the dialog UI, the way of handling location permissions has also changed. Now the user is allowed to choose whether they want location updates when the app is in the background. For that a new permission needs to be declared in the Manifest file:

Calling this along with COARSE_LOCATION would pop up a dialog with three options:

1-Always Allow 2-Allow only while using the app 3-Deny

So the problem was that. Now by adding those permission, my problem solved.



Source: stackoverflow