启动蓝牙服务时android 10崩溃

时间:2021-07-06 18:30:51

标签: java android service bluetooth android-10.0

几天前,我写了几行代码,通过服务将应用程序连接到 HC-05(蓝牙模块)。我知道一个简单的服务不能在 android 8+ 中存活。所以我使用 YouTube 频道上提供的一些免费教程来修改我的服务,如下所示:

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

android 7- 没有任何问题,但当我点击“开始服务”按钮时,android 10 崩溃了。

我为您带来了我的一些代码部分。

服务中的onStartCommand:

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;
}

和 'createNotificationChannel()' 函数在此处定义:

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);
    }
}

buttonIn 的 onClick 方法(为了启动服务)在这里:

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);
        
    }
    
    }

任何人都可以用 android 10 解决这个问题。

1 个答案:

答案 0 :(得分:0)

Android 10 在实现蓝牙方面有一些限制。 例如,您需要允许一些与位置相关的权限。 以下文字摘自以下网站:

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

Android 10 位置权限

随着 Android 10 的推出,除了对话框 UI 之外,处理位置权限的方式也发生了变化。 现在,当应用程序在后台时,用户可以选择是否需要位置更新。 为此,需要在清单文件中声明一个新的权限:

与 COARSE_LOCATION 一起调用会弹出一个包含三个选项的对话框:

1-始终允许 2-仅在使用应用程序时允许 3-拒绝

所以问题是。现在通过添加这些权限,我的问题解决了。

相关问题