2017-06-19 56 views
1

我一直在尝试扫描信标,但我无法进行扫描。我尝试了所有的StackOverflow问题,但我还没有找到答案。无法扫描信标

下面是我的代码:

MAINACTIVITY:

public class MainActivity extends AppCompatActivity implements BeaconConsumer { 

protected static final String TAG = "MonitoringActivity"; 
private BeaconManager beaconManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
beaconManager = BeaconManager.getInstanceForApplication(this); 
    beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); 
    beaconManager.bind(this);} 

@Override 
public void onBeaconServiceConnect() { 
    beaconManager.addMonitorNotifier(new MonitorNotifier() { 
     @Override 
     public void didEnterRegion(Region region) { 
      Log.i(TAG, "I just saw an beacon for the first time!"); 
     } 

     @Override 
     public void didExitRegion(Region region) { 
      Log.i(TAG, "I no longer see an beacon"); 
     } 

     @Override 
     public void didDetermineStateForRegion(int state, Region region) { 
      Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state); 
     } 
    }); 

    try { 
     beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", Identifier.parse(null, null, null)); 
    } catch (RemoteException e) { } 
} 

清单

​​

gradle.properties

manifestmerger.enabled=true 

logcat的

06-19 20:03:37.403 7459-7459/com.example.beaconscanner D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 

06-19 20:03:37.796 7459-7459/com.example.beaconscanner I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0 

06-19 20:03:37.933 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON 
06-19 20:03:37.938 7459-7471/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12 

06-19 20:03:39.038 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON 
06-19 20:03:39.040 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON 
06-19 20:03:39.044 7459-7470/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12 

我已根据Android SDK中的蓝牙添加权限位置和大于23

回答

0

似乎从Android是检测蓝牙设备的调试行,这表明蓝牙已启动,您的应用程序有权扫描。

有几件事情要检查:

  1. 确保您BeaconLayout是正确的。 “m:2-3 = beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25”用于AltBeacon。如果您的信标正在传输不同的格式(例如iBeacon或Eddystone),您将需要不同的布局。这里有一个很好的参考:https://beaconlayout.wordpress.com/

  2. 确保您的设备实际上发出了有效的信标信号。我建议我的Locate app for Android,因为它会自动检测广告iBeacon,AltBeacon和Eddystone格式的所有信标。

+0

它的工作原理是将Beaconlayout更改为m:2-3 = 0215,i:4-19,i:20-21,i:22-23,p:24-24' – Burhan