博客 页面 12

ionic报错Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

报错内容:
typescript Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

奇怪的是,在个别手机(魅族3S)上会报错,但其他手机运行正常

使用chrome连接手机调试网页

web app开发经常会遇到不同手机渲染出的结果不一样的问题,这时可以通过本方法调试:

手机打开开发者模式后,chrome访问以下地址,即可发现设备
chrome://inspect/#devices

ionic阻止页面退出,并弹出警告框

尝试使用registerBackButtonAction,但不支持ios
尝试使用narbar,但不支持返回键

可能是最优的解决方案:
https://forum.ionicframework.com/t/how-to-use-alertcontroller-result-in-ionviewcanleave-method/121186/3

产生的附加结果,是push也会受到影响。解决办法是使用ModalController

rtctest

http://rtc.blinker-iot.com:8080/demos/demo_video_only.html

webrtc学习

浏览器获取视频并显示:
var video = document.querySelector(‘video’);
navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError);
var constraints = window.constraints = {
audio: false,
video: true
};
function handleSuccess(stream) {
var videoTracks = stream.getVideoTracks();
console.log(‘Got stream with constraints:’, constraints);
console.log(‘Using video device: ‘ + videoTracks[0].label);
stream.oninactive = function() {
console.log(‘Stream inactive’);
};
window.stream = stream; // make variable available to browser console
video.srcObject = stream;
}

linux下使用:
pip3 install aiortc websockets

哪些学校正在使用《Arduino程序设计基础》做教材

重庆大学、郑州大学、厦门大学,吉林大学,电子科技大学,长春理工大学,郑州轻工业学院,西安文理学院。

预计国内已经有20多所学校在使用《Arduino程序设计基础》做教材了,这也是对我的肯定,谢谢选择该书的学校和老师。

ionic/cordova如何写一个后台服务

一个cordova解决方案:

https://github.com/Red-Folder/bgs-core

看起来好复杂

看来不管用什么方案,都要有android基础才行。

关于android服务的学习:

https://www.cnblogs.com/huolongluo/p/6340743.html

https://blog.csdn.net/a704225995/article/details/56481934

官方文档:

https://developer.android.com/training/run-background-service/create-service?hl=zh-cn

学习cordova-plugin-autostart插件经验

cordova-plugin-autostart项目地址:

https://github.com/ToniKorin/cordova-plugin-autostart

一开始没细看readme,直接用enable(),自启动是生效了,但打开的是app。

如果是开发一些基于android专用设备,用这种自启动方式很有效。如,银行排号机、自动售货机、早教机等等,可以开机直接启动,再加了无法退出,就可以满足多种使用场景了。

我只想启动后就在后台运行,不让用户察觉,尝试了多种方案均无法达到理想效果。

 

遂决定先读懂插件,尝试自己写。

下面这篇文章写的很好,给出了多种自启动并保持方案:

https://blog.csdn.net/crissjs/article/details/38293441

阅读源码过程中,发现了autostart插件中居然还有个enableService()函数。由于我使用的ionic,其

@ionic-native/autostart 中并没有提供enableService()接口,这个很坑。
原来插件本身是可以自启动服务的,但怎么用还是个问题,readme没有写明,还是要自己阅读源代码才行。

分析程序从main入手,分析插件也是如此,先找到其入口AutoStart.java。其中主要是使能了启动的信号接收器,没毛病。

再看config.xml,其中有其第二入口,这里第二入口自启动的入口:

        <config-file target="AndroidManifest.xml" parent="/manifest/application">
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.BootCompletedReceiver" android:enabled="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.UserPresentReceiver" android:enabled="false">
                <intent-filter>
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.PackageReplacedReceiver" android:enabled="true">
                <intent-filter>
                    <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                </intent-filter>
            </receiver>
        </config-file>

其plugin中注册了几个receiver,分别是:

android.intent.action.BOOT_COMPLETED//启动完成
android.intent.action.USER_PRESENT//解锁屏幕
android.intent.action.MY_PACKAGE_REPLACED//包更新

这些动作可以触发对应函数执行

具体怎么调用的可以看cordova文档,也不深究了。

启动完成后执行的文件实际上就是BootCompletedReceiver.java中的:

AppStarter appStarter = new AppStarter();
appStarter.run(context, intent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, true);

再看appStarter.java,看注释就可以找到最后启动服务的代码。

其中使用到IntentService,关于IntentService可见:

https://blog.csdn.net/p106786860/article/details/17885115

这篇文章开头就很清晰的说了IntentService和普通服务的区别。

所以关键代码:

            Intent serviceIntent = new Intent();
            serviceIntent.setClassName(context, serviceClassName);
            if ( onAutostart ) {
                serviceIntent.putExtra(CORDOVA_AUTOSTART, true);
            }
            context.startService(serviceIntent);

参数context为当前包名,serviceClassName为要启动的服务类名。

真相大白了,cordova-plugin-autostart可以启动服务,只要指定服务类名即可,当然你要另外写服务,这个插件里没法写。
现在的问题核心落到了,如何写一个android原生服务上了。
也许可用的资源:
https://github.com/Red-Folder/bgs-core

ionic/cordova自启动后台服务

https://github.com/katzer/cordova-plugin-background-mode

实际试验不行,看issues中的贡献者回复:

This plugin was never meant to run while the app is closed. It is not a service, it’s just trying to keep the app alive.

该插件不是一个服务,因此无法实现

 

参考:

https://blog.csdn.net/robert_cysy/article/details/54097206

https://stackoverflow.com/questions/10343828/creating-an-android-service-with-phonegap-have-phonegap-app-run-even-when-clos

https://blog.csdn.net/crissjs/article/details/38293441

 

参考项目:

https://github.com/ToniKorin/cordova-plugin-location-provider/blob/master/src/android/LocationService.java

webRTC服务器部署

webrtc需要两个配合服务器程序:信令服务器和穿透服务器

穿透服务器

参考:https://www.cnblogs.com/idignew/p/7440048.html

穿透服务器使用coturn
https://github.com/coturn/coturn

git clone https://github.com/coturn/coturn
cd coturn
./configure
make
sudo make install

配置turnserver.conf

cp /usr/local/etc/turnserver.conf.default   /etc/turnserver.conf
vi /etc/turnserver.conf
 
#监听端口可以不设置会默认的使用3478
listening-port=3478
#listening-ip,注意必须是你的内网IP地址如(如果你是阿里云的,就是私网地址):
listening-ip=172.xx.xx.xx
#relay-ip可以不设置,默认会使用你的外网ip地址作为转发包的中继地址,建议不设置,使用默认就可以:
#external-ip,注意必须使用你的外网IP地址如:
external-ip=xxx.xxx.xxx.xxx
#设置用户名及密码,这个是作为TURN服务器使用必须设置的,可以设置多个,我这里配置2个
user=user1:password1
user=user1:password2

启动:

turnserver -v -r  外网地址:3478 -a -o -c /etc/turnserver.conf

停止:

ps -ef|grep turnserver
kill -9 xxxx

信令服务器

https://github.com/priologic/easyrtc