Mankii
Mankii
  • 发布:2019-09-12 12:02
  • 更新:2020-09-21 13:56
  • 阅读:6589

uni-app BroadcastReceiver 广播监听无效,求解

分类:uni-app

我从官方手里复制了广播监听的例子的代码,放到“onLoad”页面里面,但是每个都没有生效。
例如,这是一个监听飞行模式开关的例子:
var receiver;
main = plus.android.runtimeMainActivity(); //获取activity
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent);
console.log(intent.getAction());
result.textContent += '\nAction :' + intent.getAction();
main.unregisterReceiver(receiver);
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); //监听飞行模式
main.registerReceiver(receiver, filter); //注册监听
============================================
原原本本地从一个大神的帖子里复制到我的页面里去,并且放在“onLoad”中,然后真机调试。
控制台没有报错。
但是当我切换飞行模式开关的时候,也没有相应的打印出来任何东西。
============================================
不晓得是我缺少了什么配置或者代码,还是操作方式不对呢?求指教!

2019-09-12 12:02 负责人:无 分享
已邀请:
2***@qq.com

2***@qq.com

没有开始设备搜索
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
//获取默认适配器
var bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery(); //开启搜索设备

c***@163.com

c***@163.com

碰到同样的问题,不知道兄弟怎么解决的

风浅

风浅 - 哇噢

兄弟,我也想咨询下
设备已经开始搜索了
调用蓝牙广播(以下方法)的时候,就卡在那里没反应了
plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {onReceive: function(context, intent) { }});

  • n***@qq.com

    大哥,解决了吗,我也遇到这个问题了

    2020-09-04 17:21

风浅

风浅 - 哇噢

最后还是弃用了,因为监听接收消息的时候,发现好像页面卡死了,也不知道有啥好办法,
搜索不到 我这边的原因是因为没有申请到动态权限,代码如下

searchDevices() {  
                let BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
                let BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");  
                let BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");  
                let BluetoothReceiver = plus.android.importClass("android.bluetooth.BluetoothReceiver");  
                let Intent = plus.android.importClass("android.content.Intent");  
                let IntentFilter = plus.android.importClass("android.content.IntentFilter");  
                this.BAdapter = BluetoothAdapter.getDefaultAdapter();  
                //检查设备是否支持蓝牙    
                if (this.BAdapter == null) {  
                    uni.showToast({  
                        title: "该设备不支持蓝牙",  
                        duration: 2000,  
                        icon: "none"  
                    });  
                    return false;  
                } else {  
                    // 如果没有打开蓝牙    
                    if (!this.BAdapter.isEnabled()) {  
                        // 打开蓝牙操作  
                        let startBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
                        console.log(startBt);  
                        this.mainActivity.startActivityForResult(startBt, 1000);  
                        // this.BAdapter.enable(); //打开蓝牙  
                        // console.log(this.BAdapter.getAddress());  
                        // console.log(this.BAdapter.getName());  
                    } else {  
                        // console.log(BAdapter.isDiscovering());  
                        // 检测当前是否在搜索蓝牙  
                        if (this.BAdapter.isDiscovering()) {  
                            this.BAdapter.cancelDiscovery();  
                        }  
                        let IntentFilter = plus.android.importClass('android.content.IntentFilter');  
                        let filter = new IntentFilter();  
                        if (this.btFindReceiver != null) {  
                            try {  
                                this.mainActivity.unregisterReceiver(this.btFindReceiver);  
                            } catch (e) {  
                                console.error(e);  
                            }  
                            this.btFindReceiver = null;  
                            // this.BAdapter.cancelDiscovery();  
                        }  
                        // SDK  
                        this.Build = plus.android.importClass("android.os.Build");  
                        let Manifest = plus.android.importClass("android.Manifest");  
                        // console.log(Build.VERSION.SDK_INT);  
                        // if (Build.VERSION.SDK_INT >= 6.0) {}  

                        let ArrPermissions = [  
                            Manifest.permission.BLUETOOTH_ADMIN,  
                            Manifest.permission.BLUETOOTH,  
                            Manifest.permission.ACCESS_FINE_LOCATION,  
                            Manifest.permission.ACCESS_COARSE_LOCATION  
                        ]  
                        if (!this.PermissionChecks(ArrPermissions)) {  
                            // 如果没有权限,请求权限  
                            this.PermissionRequest(ArrPermissions);  
                        } else {  
                            // 开始showtoast  
                            uni.showLoading({  
                                title: '蓝牙设备搜索中',  
                                mask: false  
                            });  
                            filter.addAction(BluetoothDevice.ACTION_FOUND); //发现蓝牙设备  
                            filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //蓝牙设备绑定状态改变  
                            filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //蓝牙设备的状态改变  
                            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //搜索完成  
                            //filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //开始扫描  

                            this.btFindReceiver = null;  
                            let that1 = this;  
                            this.devsList = [];  
                            this.btFindReceiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {  
                                onReceive: function(context, intent) { //实现onReceiver回调函数  
                                    console.log('onReceive函数回调');  
                                    plus.android.importClass(context);  
                                    plus.android.importClass(intent);  
                                    let action = intent.getAction();  
                                    if (BluetoothDevice.ACTION_FOUND == action) { // 找到设备  
                                        let device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
                                        let newDevice = {  
                                            "object": device,  
                                            "name": plus.android.invoke(device, "getName") ? plus.android.invoke(device, "getName") : '',  
                                            "address": plus.android.invoke(device, "getAddress") ? plus.android.invoke(device, "getAddress") : ''  
                                        };  
                                        that1.devsList.push(newDevice)  
                                        // console.log(plus.android.invoke(device, "getName"));  
                                        // that1.DiscoveryNewDevice(newDevice);  
                                    }  
                                    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { // 搜索完成  
                                        console.log('搜索完成');  
                                        that1.BAdapter.cancelDiscovery();  
                                        uni.hideLoading();  
                                        uni.showToast({  
                                            title: "搜索结束",  
                                            duration: 2000,  
                                            icon: "none"  
                                        });  
                                    }  
                                    console.log('onReceive结束回调');  
                                }  
                            });  
                            this.mainActivity.registerReceiver(this.btFindReceiver, filter);  
                            this.BAdapter.startDiscovery(); //开启搜索   
                        }  
                    }  

                }

该问题目前已经被锁定, 无法添加新回复