我在定时器里面间隔5秒 开始一次发现蓝牙设备,然后关闭发现蓝牙设备,在ios平台,每次都可以正常的有wx.onBluetoothDeviceFound()回调。但是在android(三星S7)平台上wx.onBluetoothDeviceFound,没有被回调。我不知道什么情况。代码如下:rssi_timer_cb 是间隔5秒的定时器函数,求解,谢谢。
423 wx.onBluetoothDeviceFound(function (res) {
424 var rssi_flag = false;
425 var rssi_val = 0;
426 //console.log(res);
427 //console.log("rssi_name: " + that.le_devicename);
428 for (var x=0; x < res.devices.length; x++) {
429 if (res.devices[x].name == that.le_devicename && rssi_flag == false) {
430 rssi_val = res.devices[x].RSSI;
431 rssi_flag = true;
432 }
433 }
434
435 if (rssi_flag == true) {
436 wx.stopBluetoothDevicesDiscovery({
437 success: function (res) {
438 console.log(res);
439 },
440
441 fail: function (res) {
442 console.log(res);
443 },
444
445 complete: function(res) {
446 rssi_find = false;
447 callback(le.prototype.SUCC, rssi_val);
448 },
449 });
450 }
451 else {
452 //callback(le.prototype.ERR_RSSI_OUT, rssi_val);
453 //callback(56, rssi_val);
454 }
455
456 });
460 function rssi_timer_cb() {
461 if (that.connect_state == true) {
462 console.log("RSSI已连接");
463 var rssi_gf = false;
464 var rssi_gv = 0;
465 wx.getBluetoothDevices({
466 success: function (res) {
467 console.log(res);
468 for (var i=0; i > res.devices.length; i++) {
469 if (res.devices[i].name == that.le_devicename && rssi_gf == false) {
470 rssi_gv = res.devices[i].RSSI;
471 rssi_gf = true;
472 }
473 }
474 if (rssi_gf == true) {
475 callback(le.prototype.SUCC, rssi_gv);
476 }
477 else {
478 callback(le.prototype.ERR_RSSI_OUT, rssi_val);
479 }
480 },
481
482 fail: function(res) {
483 console.log(res);
484 },
485 });
486 }
487 else
488 {
489 //console.log("RSSI未连接delay: " + delay);
490 le.prototype.open_adapter(callback_open_adapter, that);
491 function callback_open_adapter(ret_msg) {
492 if (ret_msg == le.prototype.SUCC) {
493 //that.rssi_timer = setInterval(rssi_timer_cb, delay);
494 //console.log("start rssi timer " + delay + " " + that.rssi_timer);
495 //function rssi_timer_cb() {
496 if (rssi_find == true) {
497 //callback(le.prototype.ERR_RSSI_OUT, rssi_val);
498 console.log("find open!");
499 wx.stopBluetoothDevicesDiscovery({
500 success: function (res) {
501 console.log(res);
502
503 var open_timer = setTimeout(open_timeout_bc, 200);
504 function open_timeout_bc() {
505 wx.startBluetoothDevicesDiscovery({
506 allowDuplicatesKey: false,
507 //interval: 4000,
508 success: function(res) {
509 console.log(res);
510 rssi_find = true;
511 },
512
513 fail: function(res) {
514 },
515
516 complete: function(res) {
517 },
518 });
519 clearTimeout(open_timer);
520 }
521 },
523 fail: function (res) {
524 console.log(res);
525 },
526 });
527 }
528 else {
529 console.log("find close!");
530 wx.startBluetoothDevicesDiscovery({
531 allowDuplicatesKey: false,
532 //interval: 4000,
533 success: function(res) {
534 console.log(res);
535 rssi_find = true;
536 },
537
538 fail: function(res) {
539 },
540 });
541 }
542
543 wx.onBluetoothAdapterStateChange(function(res) {
544 console.log("adapterState changed, now is ", res);
545 if (res.available == false) {
546 wx.showModal({
547 title: '提示',
548 showCancel: false,
549 content: '请检查手机蓝牙是否打开',
550 });
551 }
552 else {
553 }
554 });
555 }
556 else {
557 callback(ret_msg, 0);
558 }
559 }
我也遇到这个问题。在iOS上没有问题,在s7上一个设备也没有搜到。。。
====================================================
论坛里面找到答案了:要给wechat定位的权限。