2016-10-18 6 views
0

https://github.com/davidgyoung/ble-advert-counter/blob/master/app/src/main/java/com/radiusnetworks/blepacketcounter/MainActivity.java 을 통해 코드를 사용하여 BLE 장치의 광고 데이터를 스캔하고 읽는 것을 목표로합니다.android logcat에서 데이터를 광고하는 형식의 BLE을 읽었지만 실행할 수 없습니다.

코드가 잘 작동합니다. LogCat을 통해 형식화 된 광고 데이터를 그림과 같이 가져올 수 있습니다.

enter image description here

그러나 코드에서 나는 관련 로그 문을 찾을 수 없습니다.

BluetoothLeScanner 클래스를 참조하지 않았거나 onScanResult() 메서드가 호출되었습니다.

그리고 포맷 된 데이터 값을 얻기 위해 "ScanResult {mDevice = F3 : E5 : 7F : 73 : 4F : 81, mScanRecord = ScanRecord ..."문자열을 얻고 싶습니다.

어떻게하면됩니까?

감사합니다.

답변

0

로그에 대해서는 확실하지 않지만 데이터를 가져 오는 방법은 다음과 같습니다.

onLeScan() 콜백에는 로그에 인쇄되는 모든 정보가 있습니다. 장치 정보를 얻으려면 콜백 (예 : device.getAddress())에서 장치 개체를 사용할 수 있습니다. 스캔 레코드는 콜백의 scanRecord 바이트 배열에 있습니다. 정보를 얻으려면 배열을 구문 분석해야합니다. 아래 코드를 사용하여 스캔 정보를 구문 분석했습니다.

public WeakHashMap<Integer, String> ParseRecord(byte[] scanRecord) { 
    WeakHashMap<Integer, String> ret = new WeakHashMap<>(); 
    int index = 0; 
    while (index < scanRecord.length) { 
     int length = scanRecord[index++]; 
     //Zero value indicates that we are done with the record now 
     if (length == 0) break; 

     int type = scanRecord[index]; 
     //if the type is zero, then we are pass the significant section of the data, 
     // and we are thud done 
     if (type == 0) break; 

     byte[] data = Arrays.copyOfRange(scanRecord, index + 1, index + length); 
     if (data != null && data.length > 0) { 
      StringBuilder hex = new StringBuilder(data.length * 2); 
      // the data appears to be there backwards 
      for (int bb = data.length - 1; bb >= 0; bb--) { 
       hex.append(String.format("%02X", data[bb])); 
      } 
      ret.put(type, hex.toString()); 
     } 
     index += length; 
    } 

    return ret; 
} 

블렌드 광고에 대해 알아 보려면 아래 링크를 참조하십시오. 이 도움이

BLE obtain uuid encoded in advertising packet

희망.

+0

도움 주셔서 감사합니다. scanRecord 바이트 배열을 처리하는 것이 좋습니다. – stev90098289dev

+0

도움을 주셔서 감사합니다. scanRecord 바이트 배열을 처리하는 것이 좋습니다. 그러나 LogCat에서 BluetoothLeScanner 클래스 또는 onScanResult() 메소드가 이미 scanRecord 바이트 배열로 처리하고 읽을 수있는 MANUFACTURING DATA 및 기타 데이터 정보를 비롯한 잘 레이블 된 문자열을 출력하는 것으로 보입니다. LogCat에서이 문자열을 사용할 수 있다면 편리 할 것입니다. – stev90098289dev