2017-12-27 3034 views
-4

我正在编写android程序来扫描BLE设备。如果扫描的设备名称为“Pvz2”,那么我想调用一个函数。将BLE设备名称与字符串进行比较

当我尝试将最近扫描的设备名称与之前定义的字符串进行比较时,一切都会正常工作,但应用程序崩溃。可能是什么问题呢?

final String vardas1="Pvz2"; 

private ScanCallback mScanCallback = new ScanCallback() { 
    @Override 
    public void onScanResult(int callBackType, ScanResult result) { 
     super.onScanResult(callBackType, result); 
     BluetoothDevice btDevice = result.getDevice(); 
     String name = btDevice.getName(); 
     if (result.getScanRecord() != null && Sc_on) { 
      if (name.equals(vardas1)) 
       // Here I would call the function but this point is never reached 
      } 
     }      
     ... 

崩溃的堆栈跟踪:

Stacktrace

+1

请从Android日志中添加stacktrace。 – Christopher

回答

0

你得到一个NPE获取设备名称。

String name = btDevice.getName(); // <-- this returns null, since sometimes is not possible to determine the name 
if (result.getScanRecord() != null && Sc_on) { 
    if (name.equals(vardas1)) { // <-- null.equals crashes 

您必须始终假定您可以得到一个空名称。