安卓开发如何让蓝牙设备连接后台一直保持连接

APP只要把程序放到后台或者黑屏,蓝牙就会断开连接,有合适呢么办法能让他一直保持连接

写在service中,再做一个灰色保活。但是不能保证100%的保活,但是可以有效防止后台或者黑屏 断开连接。

/**
 * @author :creat by Xia燚
 * 时间:2021/3/15
 * 邮箱:XiahaotianV@163.com
 **/
public class XXService extends Service {
    private final static int GRAY_SERVICE_ID = 1001;
    
    private XXClientBinder mBinder = new XXClientBinder();

    //灰色保活
    public static class GrayInnerService extends Service {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            //自己的业务逻辑。。。 你要自己写了
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }

    PowerManager.WakeLock wakeLock;//锁屏唤醒

    //获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行
    @SuppressLint("InvalidWakeLockTag")
    private void acquireWakeLock() {
        if (null == wakeLock) {
            PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "PostLocationService");
            if (null != wakeLock) {
                wakeLock.acquire();
            }
        }
    }


    //用于Activity和service通讯
    public class XXClientBinder extends Binder {
        public XXService getService() {
            return XXService.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //设置service为前台服务,提高优先级
        if (Build.VERSION.SDK_INT < 18) {
            //Android4.3以下 ,隐藏Notification上的图标
            startForeground(GRAY_SERVICE_ID, new Notification());
        } else if (Build.VERSION.SDK_INT > 18 && Build.VERSION.SDK_INT < 25) {
            //Android4.3 - Android7.0,隐藏Notification上的图标
            Intent innerIntent = new Intent(this, GrayInnerService.class);
            startService(innerIntent);
            startForeground(GRAY_SERVICE_ID, new Notification());
        } else {
            //Android7.0以上app启动后通知栏会出现一条"正在运行"的通知
            startForeground(GRAY_SERVICE_ID, new Notification());
        }

        acquireWakeLock();
        return START_STICKY;
    }
}
<!--清单配置文件 -->   
 <service
            android:name=".im.XXService$GrayInnerService"
            android:enabled="true"
            android:exported="false"
            android:process=":gray" />

Activity 引用

 

   /**
     * 绑定服务
     */
    private void bindService() {
        Intent bindIntent = new Intent(mContext, XXService.class);
        bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
    }

    /**
     * 启动服务(客户端服务)
     */
    private void startJWebSClientService() {
        Intent intent = new Intent(mContext, XXService.class);
        startService(intent);
    }



    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e("MainActivity", "服务与活动成功绑定");
            binder = (XXService.XXClientBinder) iBinder;
            xxService = binder.getService();
            client = XXService.client;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e("MainActivity", "服务与活动成功断开");
        }
    };

使用的时候 用client 调用你写在蓝牙的方法。  注意: 需要长驻的服务 写在Service中

有完整的代码没?