king022 2021-03-15 09:00 采纳率: 33.3%
浏览 1135

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

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

  • 写回答

2条回答 默认 最新

  • Xia_燚 2021-03-15 10:08
    关注

    写在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中

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序