react native 的蓝牙开发基本还没有人介绍,经过摸索终于有了收获
这里介绍android平台下的蓝牙开发
在Android平台下创建BleManagerModule,BleManagerPackage,这个只是react native基础的交互说明,具体参考: http://facebook.github.io/react-native/docs/native-modules-android.html
把接口react native和android接口调用弄通,然后把蓝牙相关代码加入,这个就不介绍了,就是在加蓝牙的时候出现的bug,下面是解决方法
这里需要执行,在node.js下
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --sourcemap-output android/app/src/main/assets/index.android.map --assets-dest android/app/src/main/res/
不然会报错误,具体参考 http://blog.csdn.net/b992379702b/article/details/52234479
然后在代码中加入implements LifecycleEventListener,这个是为了增加生命周期,在结束的时候释放掉蓝牙的资源。
public class BleManagerModule extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener
@Override
public void onHostResume() {
}
@Override
public void onHostPause() {
}
@Override
public void onHostDestroy() {
btconfig.stopBleScan();
}
好吧,最后上传我的代码,给大家留个福利。 https://git.oschina.net/zxforme/react-native-ble 后续会不断更新。
记得要npm install更新,各位点个赞吧。哈哈
好吧。。我最后找到官方的ble,,百度真垃圾,google一下就出来了。。附上地址https://github.com/innoveit/react-native-ble-manager
来源:oschina
链接:https://my.oschina.net/u/163029/blog/750783