bluetooth.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import Vue from 'vue';
  2. let main, Context, BManager, BluetoothAdapter, BAdapter, BluetoothDevice, IntentFilter;
  3. import {
  4. SET_INFODATA,
  5. SET_CONNECTBLEDATA,
  6. SET_REQUEST_DATA
  7. } from '@/store/actionsType.js';
  8. import {
  9. GET_CONNECTBLEDATA
  10. } from '@/store/gettersType.js';
  11. /**
  12. * 蓝牙初始化和注册
  13. */
  14. Vue.prototype.$init_bluetooth = function() {
  15. // console.log('蓝牙初始化');
  16. let _this = this;
  17. //获取android应用Activity活动对象
  18. main = plus.android.runtimeMainActivity();
  19. //引入Context类
  20. Context = plus.android.importClass("android.content.Context");
  21. // Context.BLUETOOTH_SERVICE 获取Context类的静态常量(蓝牙服务,获取BluetoothManager,以使用蓝牙)
  22. BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
  23. //获取蓝牙适配器对象类
  24. BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
  25. //蓝牙本地适配器(对象)
  26. BAdapter = BluetoothAdapter.getDefaultAdapter();
  27. //引入蓝牙设备类(创建与相应设备的连接或查询有关该设备的信息,例如名称,地址,类和绑定状态)
  28. BluetoothDevice = plus.android.importClass('android.bluetooth.BluetoothDevice');
  29. //引入过滤器类 (IntentFilter可以匹配Intent中的动作,类别,数据)
  30. IntentFilter = plus.android.importClass('android.content.IntentFilter');
  31. }
  32. /**
  33. * 检查蓝牙是否开启
  34. * 1.用户没有开启,提示开启
  35. * 2.用户蓝牙已经开启
  36. */
  37. Vue.prototype.$check_bluetooth_open = function() {
  38. let _this = this;
  39. /**
  40. * BAdapter.isEnabled(); 判断蓝牙是否打开
  41. * BAdapter.enable(); //开启蓝牙
  42. * BAdapter.disable(); //关闭蓝牙
  43. */
  44. return new Promise((resolve, reject) => {
  45. if (!BAdapter.isEnabled()) {
  46. //蓝牙未打开
  47. uni.showModal({
  48. title: "提示",
  49. content: "蓝牙未开启,是否开启蓝牙~",
  50. success: function(res) {
  51. if (res.confirm) {
  52. //开启蓝牙
  53. BAdapter.enable();
  54. resolve(true);
  55. } else if (res.cancel) {
  56. resolve(false);
  57. }
  58. }
  59. })
  60. // 后续提示框提示或用户手动打开
  61. } else {
  62. //蓝牙已打开
  63. resolve(true);
  64. }
  65. })
  66. }
  67. /**
  68. * 检测手机是否已经连接蓝牙设备
  69. */
  70. Vue.prototype.$check_bluetooth_connect = function() {
  71. let _this = this;
  72. // 先清空vuex原来已有的数据
  73. _this.$store.dispatch(SET_CONNECTBLEDATA, []);
  74. return new Promise((resolve, reject) => {
  75. // 获取android应用已配对的蓝牙设备类
  76. let lists = BAdapter.getBondedDevices();
  77. // 引入类
  78. plus.android.importClass(lists);
  79. // 获取已配对蓝牙设备的个数
  80. let len = lists.size();
  81. // iterator() 把一个容器的所有对象,做成一个线性表(List),而iterator本身是一个指针
  82. let iterator = lists.iterator();
  83. // console.log(iterator.hasNext());
  84. plus.android.importClass(iterator);
  85. /**
  86. * iterator.hasNext() true如果迭代具有更多元素
  87. * iterator.next() 放回迭代中的下一个元素
  88. * iterator.remove() 从基础集合中移除此迭代器返回的最后一个元素(可选操作)
  89. */
  90. while (iterator.hasNext()) {
  91. let d = iterator.next();
  92. plus.android.importClass(d);
  93. let matchList = {
  94. name: d.getName(),
  95. mac: d.getAddress()
  96. }
  97. // console.log(matchList);
  98. _this.$store.dispatch(SET_CONNECTBLEDATA, matchList);
  99. resolve({
  100. code: true,
  101. msg: matchList
  102. });
  103. }
  104. //获取一个已连接的设备
  105. // plus.android.importClass(BManager); //引入相关的method函数
  106. // //蓝牙适配器
  107. // let BAdapter = BManager.getAdapter();
  108. // // console.log(BAdapter);
  109. // plus.android.importClass(BAdapter); //引入相关的method函数,这样之后才会有isEna;
  110. // let lists = BAdapter.getBondedDevices();
  111. // // console.log(lists);
  112. // plus.android.importClass(lists);
  113. // let iterator = lists.iterator();
  114. // // console.log(iterator);
  115. // plus.android.importClass(iterator);
  116. // // console.log(iterator.hasNext());
  117. // if(iterator.hasNext()){ //判断下一个元素的有无
  118. // let d = iterator.next();
  119. // plus.android.importClass(d);
  120. // //已连接蓝牙的数据
  121. // // console.log(d.getAddress());
  122. // console.log(d.getAddress() + "----" + d.getName());
  123. // // _this.match_list = {
  124. // // name: d.getName(),
  125. // // mac: d.getAddress()
  126. // // };
  127. // let matchList = {
  128. // name: d.getName(),
  129. // mac: d.getAddress()
  130. // }
  131. // _this.$store.dispatch(SET_CONNECTBLEDATA,matchList);
  132. // // console.log(_this.$store.getters.GET_CONNECTBLEDATA)
  133. // /**
  134. // * 连接打印机
  135. // */
  136. // resolve({code:true,msg:matchList});
  137. // }else{
  138. // resolve({code:false})
  139. // }
  140. })
  141. }
  142. /**
  143. * 打开蓝牙
  144. */
  145. Vue.prototype.$open_bluetooth = function() {
  146. let _this = this;
  147. return new Promise((resolve, reject) => {
  148. if (!BAdapter.isEnabled()) {
  149. BAdapter.enable(); //启动蓝牙
  150. uni.showToast({
  151. icon: "none",
  152. title: '蓝牙已打开',
  153. duration: 3000
  154. })
  155. resolve(true);
  156. }
  157. })
  158. }
  159. /**
  160. * 关闭蓝牙
  161. */
  162. Vue.prototype.$close_bluetooth = function() {
  163. let _this = this;
  164. if (BAdapter.isEnabled()) {
  165. BAdapter.disable(); //关闭蓝牙
  166. uni.showToast({
  167. icon: "none",
  168. title: '蓝牙已关闭',
  169. duration: 2000
  170. })
  171. } else {
  172. uni.showToast({
  173. icon: "none",
  174. title: '蓝牙已关闭',
  175. duration: 2000
  176. })
  177. }
  178. }
  179. /**
  180. * 搜索蓝牙设备
  181. */
  182. Vue.prototype.$search_bluetooth = function() {
  183. let _this = this;
  184. let obj = {};
  185. return new Promise((resolve, reject) => {
  186. // console.log(BAdapter.isEnabled());
  187. // console.log(JSON.stringify(_this.$store.getters));
  188. // BAdapter.isconnect("DC:1D:30:7C:74:96");
  189. //判断蓝牙是否开启
  190. if (!BAdapter.isEnabled()) {
  191. uni.showModal({
  192. title: "提示",
  193. content: "蓝牙未开启,是否开启蓝牙~",
  194. success: function(res) {
  195. if (res.confirm) {
  196. console.log('用户点击确定');
  197. obj.code = false; //用户点击确定,开启蓝牙
  198. obj.msg = "蓝牙未开启";
  199. resolve(obj);
  200. // _this.$open_bluetooth();
  201. } else if (res.cancel) {
  202. // resolve()
  203. obj.code = null;
  204. resolve(obj);
  205. }
  206. }
  207. })
  208. } else {
  209. obj.code = true;
  210. obj.msg = "开启搜索蓝牙";
  211. resolve(obj);
  212. }
  213. })
  214. }
  215. /**
  216. * 监听蓝牙设备信息
  217. */
  218. Vue.prototype.$search_pipei = function() {
  219. let timer = null;
  220. let _this = this;
  221. //提示蓝牙开启权限访问
  222. uni.openBluetoothAdapter({
  223. success(res) {
  224. if (res.errMsg === "openBluetoothAdapter:ok") {
  225. //这里是开启蓝牙搜寻
  226. uni.startBluetoothDevicesDiscovery({
  227. allowDuplicatesKey: false,
  228. success: (res) => {
  229. console.log('startBluetoothDevicesDiscovery success', res)
  230. uni.showLoading({
  231. title: "蓝牙搜索中...",
  232. mask: true
  233. })
  234. //每次搜索都把之前的清空
  235. // _this.bArray = [];
  236. // _this.no_match_list = [];
  237. _this.$store.dispatch(SET_INFODATA, []);
  238. let bArray = []; //用于蓝牙去重
  239. let filter = new IntentFilter(); //实例化过滤器类
  240. let BDevice = new BluetoothDevice(); //实例化蓝牙设备类
  241. // let connect = _this.$store.state.Bluetooth.connectBLEData;
  242. // console.log("已连接:" + JSON.stringify(connect));
  243. BAdapter.startDiscovery(); //开启搜索
  244. let receiver = plus.android.implements(
  245. 'io.dcloud.android.content.BroadcastReceiver', {
  246. onReceive: function(context, intent) { //回调
  247. try {
  248. plus.android.importClass(intent);
  249. if (intent.getAction() ==
  250. "android.bluetooth.adapter.action.DISCOVERY_FINISHED"
  251. ) {
  252. main.unregisterReceiver(receiver); //取消监听
  253. } else {
  254. // Intent中获取设备对象
  255. BDevice = intent.getParcelableExtra(
  256. BluetoothDevice.EXTRA_DEVICE);
  257. console.log(BDevice.getName() + "---" +
  258. BDevice.getAddress());
  259. console.log(BDevice.getName())
  260. // 判断如果蓝牙没有名称则不显示
  261. if (BDevice.getName() !== null) {
  262. //蓝牙去重
  263. let address = BDevice.getAddress();
  264. //已经连接的蓝牙
  265. if (bArray.indexOf(address) == -1) {
  266. bArray.push(address);
  267. _this.$store.dispatch(
  268. SET_INFODATA, {
  269. name: BDevice.getName(),
  270. mac: BDevice
  271. .getAddress()
  272. })
  273. }
  274. }
  275. //如果intent为空则取消蓝牙监听
  276. if (BDevice == null) {
  277. main.unregisterReceiver(
  278. receiver); //取消监听
  279. uni.hideLoading()
  280. //获取已匹配的蓝牙
  281. // that.bluetooth_list()
  282. return;
  283. }
  284. if (timer != null) {
  285. clearTimeout(timer);
  286. }
  287. timer = setTimeout(() => {
  288. main.unregisterReceiver(
  289. receiver); //取消监听
  290. uni.hideLoading();
  291. }, 3000);
  292. }
  293. } catch (e) {
  294. uni.showToast({
  295. icon: "none",
  296. title: "蓝牙搜寻错误~",
  297. duration: 3000,
  298. mask: true
  299. })
  300. }
  301. }
  302. });
  303. filter.addAction(BDevice.ACTION_FOUND); //可发现
  304. filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
  305. filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED); //搜索结果
  306. filter.addAction(BAdapter.ACTION_STATE_CHANGED);
  307. main.registerReceiver(receiver, filter); //注册监听
  308. },
  309. fail: (err) => {
  310. uni.showToast({
  311. icon: "none",
  312. title: "蓝牙搜寻失败~",
  313. duration: 3000,
  314. mask: true
  315. })
  316. }
  317. })
  318. }
  319. },
  320. fail(err) {
  321. uni.showToast({
  322. icon: "none",
  323. title: "蓝牙搜索失败"
  324. })
  325. }
  326. })
  327. }