我一直在尝试扫描信标,但我无法做到。我尝试了所有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) { }
}
清单
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
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 :(得分:0)
从您的调试行中可以看出,Android正在检测蓝牙设备,这表明蓝牙已启用且您的应用有权扫描。
要检查的一些事项:
确保您的BeaconLayout正确无误。 &#34; M:2-3 =中非,I:4-19,I:20-21,I:22-23,P:24-24,d:25-25&#34;适用于AltBeacon。如果您的信标正在传输不同的格式(例如iBeacon或Eddystone),您将需要不同的布局。这里有一个很好的参考:https://beaconlayout.wordpress.com/
确保您的设备实际发送有效的信标信号。我建议我Locate app for Android,因为它会自动检测所有宣传iBeacon,AltBeacon和Eddystone格式的信标。