2015-01-17 4 views
0

Android 스마트 폰을 사용하여 ibeacon을 감지하려고합니다. 나에게 안드로이드 라이브러리를 제공 한 회사에서 ibeacon 장치를 구입했다. (이 라이브러리는 내가 사용한 코드와 같은 AltBeacon의 안드로이드 비컨 라이브러리와 매우 흡사하다.) 나는이 장치를 감지 볼 수있는 로그 캣에서 전화 응용 프로그램을 실행하면android의 ibeacon 스캔 결과가 스마트 폰에 표시되지 않음

public class IBeaconListAdapter extends BaseAdapter { 

private Activity activity; 
private List<IBeacon> beacons; 
private static LayoutInflater inflater = null; 

public IBeaconListAdapter(Activity _activity, List<IBeacon> _beacons) { 
    this.activity = _activity; 
    this.beacons = _beacons; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return beacons.size(); 
} 

@Override 
public Object getItem(int position) { 
    return beacons.get(position); 
} 

@Override 
public long getItemId(int position) { 
    /*IBeacon beacon = beacons.get(position); 
    if (beacon != null) { 
     return beacon.hashCode(); 
    }*/ 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 

    if (convertView == null) { 
     view = inflater.inflate(R.layout.beacon_list_row, null); 
    } 

    TextView majorTextView = (TextView)view.findViewById(R.id.majorValue); 
    TextView minorTextView = (TextView)view.findViewById(R.id.minorValue); 
    TextView rssiTextView = (TextView)view.findViewById(R.id.rssiValue); 
    TextView accuracyTextView = (TextView)view.findViewById(R.id.accuracyValue); 

    IBeacon beacon = beacons.get(position); 

    if (beacon != null) { 
     DecimalFormat df = new DecimalFormat("#.0"); 
     majorTextView.setText(beacon.getMajor()); 
     minorTextView.setText(beacon.getMinor()); 
     rssiTextView.setText(beacon.getRssi() + " dBm"); 
     accuracyTextView.setText(df.format(beacon.getAccuracy()) + " m"); 
    } 

    return view; 
} 

} 

동안, 다음은 목록을 만들 수있는 두 번째 활동이 년대 MainActivity

public class MainActivity extends Activity implements IBeaconConsumer { 

private static final String TAG = "BB-EXAPP"; 

// iBeacon bluetooth scanning parameters 
private static final int FOREGROUND_SCAN_PERIOD = 1000; 
private static final int FOREGROUND_BETWEEN_SCAN_PERIOD = 1000; 
private static final int BACKGROUND_SCAN_PERIOD = 250; 
private static final int BACKGROUND_BETWEEN_SCAN_PERIOD = 2000; 

// iBeacon Library Stuff 
private static final Region blueupRegion = new Region("BlueUp", "acfd065e-c3c0-11e3-9bbe-1a514932ac01", null, null); 
private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this); 
private Intent iBeaconService; 
private boolean isMonitoring = false; 
private boolean isRanging = false; 

// Android BLE Stuff 
private BluetoothAdapter mBluetoothAdapter; 
private static final int REQUEST_ENABLE_BT = 1; 


// UI Stuff 
private List<IBeacon> beacons; 
private ListView listView; 
private IBeaconListAdapter listAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Initializes Bluetooth adapter 
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
    mBluetoothAdapter = bluetoothManager.getAdapter(); 

    // Ensures Bluetooth is available on the device and it is enabled. If not, 
    // displays a dialog requesting user permission to enable Bluetooth. 
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
    } 

    // Initializes iBeacon Service 
    iBeaconService = new Intent(getApplicationContext(), IBeaconService.class); 

    // Start the iBeacon Service 
    Log.d(TAG, "Starting service: iBeaconService"); 
    startService(iBeaconService); 

    // Set desired scan periods 
    iBeaconManager.setForegroundScanPeriod(FOREGROUND_SCAN_PERIOD); 
    iBeaconManager.setForegroundBetweenScanPeriod(FOREGROUND_BETWEEN_SCAN_PERIOD); 
    iBeaconManager.setBackgroundScanPeriod(BACKGROUND_SCAN_PERIOD); 
    iBeaconManager.setBackgroundBetweenScanPeriod(BACKGROUND_BETWEEN_SCAN_PERIOD); 

    // Bind the iBeacon Service 
    iBeaconManager.bind(this); 

    // 
    // UI Initialization 
    // 

    // Create Empty IBeacons List 
    beacons = new ArrayList<IBeacon>(); 

    // Create List Adapter 
    listAdapter = new IBeaconListAdapter(this, beacons); 

    // Get ListView 
    listView = (ListView)findViewById(R.id.listView); 

    // Set ListAdapter 
    listView.setAdapter(listAdapter); 
} 

@Override 
public void onIBeaconServiceConnect() { 
    Log.d(TAG, "onIBeaconServiceConnect"); 

    // Set Monitor Notifier 
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() { 

     @Override 
     public void didExitRegion(Region region) { 
      Log.d(TAG, "didExitRegion: region = " + region.toString()); 
     } 

     @Override 
     public void didEnterRegion(Region region) { 

      Log.d(TAG, "didEnterRegion: region = " + region.toString()); 

      // Set Range Notifier 
      iBeaconManager.setRangeNotifier(new RangeNotifier() { 

       @Override 
       public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) { 
        // Update UI iBeacons List 
        beacons = new ArrayList<IBeacon>(iBeacons); 
        listAdapter.notifyDataSetChanged(); 

        // Log found iBeacons 
        Log.d(TAG, "didRangeBeaconsInRegion: region = " + region.toString()); 
        if (!iBeacons.isEmpty()) { 
         int j = 0; 
         for (IBeacon beacon : iBeacons) { 
          Log.d(TAG, " [" + j + "] (Major = " + beacon.getMajor() + ", Minor = " + beacon.getMinor() + ", RSSI = " + beacon.getRssi() + ", Accuracy = " + beacon.getAccuracy() + ")"); 
          j++; 
         } 
        } 
       } 

      }); 

      // Start Ranging 
      try { 
       iBeaconManager.startRangingBeaconsInRegion(blueupRegion); 
       isRanging = true; 
       Log.d(TAG, "startRangingBeaconsInRegion: region = " + blueupRegion.toString()); 
      } catch (RemoteException e) { 
       Log.d(TAG, "startRangingBeaconsInRegion [RemoteException]"); 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void didDetermineStateForRegion(int state, Region region) { 
      Log.d(TAG, "didDetermineStateForRegion: state = " + state + ", region = " + region.toString()); 
     } 

    }); 


    // Start Monitoring 
    try { 
     iBeaconManager.startMonitoringBeaconsInRegion(blueupRegion); 
     isMonitoring = true; 
     Log.d(TAG, "startMonitoringBeaconsInRegion: region = " + blueupRegion.toString()); 
    } catch (RemoteException e) { 
     Log.d(TAG, "startMonitoringBeaconsInRegion [RemoteException]"); 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onResume() { 
    Log.d(TAG, "onResume"); 
    super.onResume(); 
    if (iBeaconManager.isBound(this)) { 
     iBeaconManager.setBackgroundMode(this, false); 
     Log.d(TAG, "iBeaconManager.setBackgroundMode = false"); 
    } 
} 

@Override 
public void onStop() { 
    Log.d(TAG, "onStop"); 
    if (iBeaconManager.isBound(this)) { 
     iBeaconManager.setBackgroundMode(this, true); 
     Log.d(TAG, "iBeaconManager.setBackgroundMode = true"); 
    } 
    super.onStop(); 
} 

@Override 
public void onDestroy() { 
    Log.d(TAG, "onDestroy"); 

    if (isRanging) { 
     try { 
      iBeaconManager.stopRangingBeaconsInRegion(blueupRegion); 
      Log.d(TAG, "stopRangingBeaconsInRegion: region = " + blueupRegion.toString()); 
     } catch (RemoteException e) { 
      Log.d(TAG, "stopRangingBeaconsInRegion [RemoteException]"); 
      e.printStackTrace(); 
     } 
    } 

    if (isMonitoring) { 
     try { 
      iBeaconManager.stopMonitoringBeaconsInRegion(blueupRegion); 
      Log.d(TAG, "stopMonitoringBeaconsInRegion: region = " + blueupRegion.toString()); 
     } catch (RemoteException e) { 
      Log.d(TAG, "stopMonitoringBeaconsInRegion [RemoteException]"); 
      e.printStackTrace(); 
     } 
    } 

    if (iBeaconManager.isBound(this)) { 
     Log.d(TAG, "Unbinding iBeaconManager"); 
     iBeaconManager.unBind(this); 
    } 

    Log.d(TAG, "Stopping service: iBeaconService"); 
    stopService(iBeaconService); 

    super.onDestroy(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

입니다 전화에서 나는 아무것도 볼 수없는 빈 페이지! LogCat에서 일단 onIBeaconServiceConnect가 호출되면 app이 startMonitoringBeaconsInRegion으로 점프하고 didRangeBeaconsInRegion 및 코드를 포함하는 didEnterRegion 메서드를 호출하지 않는다는 것을 알았습니다. 내 질문과 유사한 질문에 대한 다른 답변을 찾을 수 없으며 실제로 내 실수가 어디인지 알 수 없습니다.

+0

"LogCat에서 장치를 감지 한 것을 볼 수 있습니까?"라는 것은 무엇을 의미합니까? LogCat에서보고있는 것을 게시하여 생각하게 할 수 있습니까? – davidgyoung

답변

0

몇 가지 생각 :

  1. 이 비콘 정말 전송하고 당신이 그들이 생각하는 식별자를 가지고 있는지 확인하십시오. Locate과 같은 기성품 앱을 다운로드하고 확인하십시오.

  2. 귀하의 코드는 UHID가 acfd065e-c3c0-11e3-9bbe-1a514932ac01 인 비컨을 찾고있는 지역을 설정합니다. 이게 정확히 맞는 거니? 영역 정의를 Region("BlueUp", null, null, null);으로 바꾸어 식별자에 관계없이 영역을 감지하도록하십시오.

  3. 중요한 개발을 계획중인 경우 Android Beacon Library 2.0으로 업그레이드하는 것이 좋습니다. 라이브러리가 기반으로하는 오래된 0.x 라이브러리 버전은 더 이상 지원되거나 유지 관리되지 않으므로 이와 같은 문제가 발생할 때 도움을 얻는 것이 더 어려워집니다.

+0

이미 Locate 앱을 다운로드했는데 찾아 냈습니다. 내가 말했듯이 지역 정의를 바꿨지 만 아무것도 바뀌지 않았습니다. 항상 빈 페이지가 있습니다. Android Beacon Library와 유사한 라이브러리를 사용하고 있지만, iBeacon 사양을 충족하는 비컨을 감지합니다. AltBeacon 라이브러리를 사용해 보았지만 BeaconParser를 사용하여 라이브러리를 구성 할 수 없었습니다. 어쨌든 LogCat에 결과를 보여줄 답변을 추가했습니다. – arianna

+0

iBeaconManager.debug = true로 설정해보십시오. 서비스가 첫 번째 비컨 감지에 연결될 때부터 새로운 logcat 발췌 록을 캡처하십시오. – davidgyoung

+0

다른 답변의 로그를 참조하십시오. @arianna에서 –

0

이들은 LogCat의 중요한 라인입니다.

D/BB-EXAPP(7756): onIBeaconServiceConnect 
D/BB-EXAPP(7756): startMonitoringBeaconsInRegion: region = proximityUuid: acfd065e-c3c0-11e3-9bbe-1a514932ac01 major: null minor:null 
D/AbsListView(7756): unregisterIRListener() is called 
I/IBeaconService(7756): start monitoring received 
D/BluetoothAdapter(7756): startLeScan(): null 
D/BluetoothAdapter(7756): onClientRegistered() - status=0 clientIf=4 
I/IBeaconService(7756): Adjusted scanStopTime to be Sat Jan 17 12:09:58 CET 2015 
D/AbsListView(7756): unregisterIRListener() is called 
D/BluetoothAdapter(7756): onScanResult() - Device=DA:0D:22:4B:40:17 RSSI=-59 
D/Callback(7756): attempting callback via intent: ComponentInfo{com.android.appbeacon/com.blueup.libbeacon.IBeaconIntentProcessor} 
D/BluetoothAdapter(7756): onScanResult() - Device=DA:0D:22:4B:40:17 RSSI=-64 
I/IBeaconService(7756): iBeacon detected multiple times in scan cycle :acfd065e-c3c0-11e3-9bbe-1a514932ac01 0 0 accuracy: 0.7351236323265571 proximity: 2 
D/BluetoothAdapter(7756): stopLeScan()