2017-12-27 1688 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 

您必須始終假定您可以得到一個空名稱。

相關問題