Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BLE扫描和结果显示_Android_Bluetooth Lowenergy - Fatal编程技术网

Android BLE扫描和结果显示

Android BLE扫描和结果显示,android,bluetooth-lowenergy,Android,Bluetooth Lowenergy,我正在开发一款android应用程序,需要连接到蓝牙低能耗设备。为了实现该目标,并遵循以下步骤,我在清单文件中包含了正确的权限。在main活动中,我尝试扫描可编程设备并在屏幕上打印结果。代码如下所示: final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); bluetoothLeScanner.startScan(callback); // Bef

我正在开发一款android应用程序,需要连接到蓝牙低能耗设备。为了实现该目标,并遵循以下步骤,我在清单文件中包含了正确的权限。在main活动中,我尝试扫描可编程设备并在屏幕上打印结果。代码如下所示:

     final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
     bluetoothLeScanner.startScan(callback);
     // Before 5 seconds, stop the scan and show results.
     new Handler().postDelayed(new Runnable() {
         @Override
         public void run() {
             bluetoothLeScanner.stopScan(callback);
             callback.onBatchScanResults(results);
             callback.onScanFailed(2);
             callback.onScanResult(3,result);
             listOfResults.setText(results.toString());
         }
     },5000);
  • 其中:
    • Bluetooth适配器
      是执行操作所需的Blueooth适配器,如
    • bluetoothLeScanner
      是执行LE扫描操作所需的对象
    • callback
      是一个扫描回调对象
    • results
      是一个列表
    • result
      是扫描结果
    • listOfResults
      是文本视图
  • 问题可能出在所使用的方法中,因为根据,我们找到了使用回调执行的三个空洞(onBatchScanResult、onScanResult和onScanFailed),但我只处理BatchScanResult
  • 为什么没有显示设备?打印的唯一内容是活动名称、软件包名称和应用程序名称

我这样做的方式是实现scanCallback()对象,并根据需要重写onScanResult()或onBatchScanResults()

    private ScanCallback callback= new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);

            // handles scan result
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);

            // handles batch scan results
            for (ScanResult result : results) {

                // you can iterate through each result like so
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            // handles error
        }
    };

也可以考虑使用<代码>结果.GETDeVice()/Cuffe >,这样您就可以检索您需要的设备的特定数据而不是大量的信息。例如,

result.getDevice().getAddress()
如果您只想要地址。

当我运行应用程序打印存储在
result
中的信息时,您可能需要检查我的回答,应用程序会突然关闭。如果我尝试打印
结果
而不是
结果
,则应用程序将同样关闭。两次我都看到与空对象引用相关的错误(即使BLE设备处于活动状态),看起来您从扫描中没有得到任何东西,并且您的应用程序由于空对象引用而中断。尝试在那里调试,执行类似于
if(result==null){}
的操作或类似于
String text=result!=无效的result.toString():“无设备”
这有点奇怪,因为BLE设备处于活动状态。我用一个应用程序来显示可移动设备,它找到了我的。有什么想法吗?试试看@dglozano链接。在我的例子中,每次扫描发现设备时,我都会将设备添加到设备的ArrayList中。你能用你的新代码更新你的帖子吗?如果没有更多的上下文,帮助你有点困难。经过一些测试,结果是一样的。在Diego(@dglozano)的问题中,他建议使用RxAndroidBle库,但我更愿意不使用外部库。解决方案,重写方法
onScanResult
,不起作用,因为当至少有三个BLE设备正常工作时,问题仍然是
result
返回为null。
bluetoothLeScanner.startScan(callback);
bluetoothLeScanner.stopScan(callback);