2017-12-27 16 views
-4

BLE 장치를 검색하는 Android 프로그램을 작성 중입니다. 검사 된 장치의 이름이 "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 

항상 null 이름을 사용할 수 있다고 가정해야합니다.