android 10在启动蓝牙服务时崩溃

6fe3ivhb  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(313)

几天前,我编写了几行代码,通过服务将应用程序连接到hc-05(蓝牙模块)。我知道一个简单的服务在安卓8+中是不可能存在的。因此,我使用youtube频道上提供的一些免费教程修改我的服务,如下所示:
https://www.youtube.com/watch?v=bxwdm5vvuka
安卓7-没有任何问题,但安卓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);
    }
}

按钮的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);

    }

    }

那么谁能用安卓10解决这个问题呢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题