CompanionDeviceService 在创建后立即被销毁

问题描述 投票:0回答:2

我有一个简单的活动,其功能是在调用时显示 ble 配对请求。用户确认后,调用 startObservingDevicePresence("Some Mac address")。这成功触发了 CompanionDeviceService,我看到设备出现在范围内的日志,但紧接着 onDestroy 被调用。应用程序继续运行,日志中没有错误。有人成功使用过这些新的 android 12 api (https://developer.android.com/guide/topics/connectivity/companion-device-pairing#keep-awake)吗?

主要活动:

    public class MainActivity extends ReactActivity {
        private static final int SELECT_DEVICE_REQUEST_CODE = 42;
        private static CompanionDeviceManager deviceManager;
        private static AssociationRequest pairingRequest;
        private static BluetoothDeviceFilter deviceFilter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(null);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                deviceManager = getSystemService(CompanionDeviceManager.class);
            }
        }

        public void start() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                deviceFilter = new BluetoothDeviceFilter.Builder()
                        .build();

                pairingRequest = new AssociationRequest.Builder()
                        .addDeviceFilter(deviceFilter)
                        .build();

                deviceManager.associate(pairingRequest,
                        new CompanionDeviceManager.Callback() {
                            @Override
                            public void onDeviceFound(IntentSender chooserLauncher) {
                                try {
                                    startIntentSenderForResult(chooserLauncher,
                                            SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(CharSequence charSequence) {

                            }
                        },
                        null);

            }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    try {
                        deviceManager.startObservingDevicePresence("Some MAC address");
                    } catch(DeviceNotAssociatedException e) {}
                }
            }
        }
    }

陪伴服务:

    @RequiresApi(VERSION_CODES.S)
    public class BleCompanionDeviceService extends CompanionDeviceService {
        private static final String TAG = "BleReceiver";
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        public void onDeviceAppeared(@NonNull String s) {
            Log.d(TAG, "DEVICE APPEARED INTO RANGE");
        }
    
        @Override
        public void onDeviceDisappeared(@NonNull String s) {
            Log.d(TAG, "DEVICE DISAPPEARED");
        }
    
        @Override
        public void onDestroy() {
            Log.d(TAG, "SERVICE DESTROYED");
        }
    }
android bluetooth-lowenergy android-ble android-companion-device
2个回答
0
投票

这似乎是 CompanionDeviceManager 中发生的竞争条件,Android 13 应该已修复它。


0
投票

这是预期的行为。 CompanionDeviceService 只能调用 onDeviceAppeared 或 onDeviceDisappeared。由您决定如何处理。

例如,您可能想要为长时间运行的任务启动前台服务或打开您的 Activity。

© www.soinside.com 2019 - 2024. All rights reserved.