我有一个应用程序来扫描周围的BLE信标.我已经使用Altbeacon库来做到这一点。当我在我的 Nexus 6p 上运行它时,它会找到一些信标,但是当我在 Galaxy s9 或 Kindle fire HD 8 上运行它时,它找到的信标数量始终为零。这是我的代码:
override fun onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers()
beaconManager.addRangeNotifier(object : RangeNotifier {
override fun didRangeBeaconsInRegion(beacons: Collection<Beacon>, region: Region) {
Log.i("ranging" , "beacons size:" + beacons.size)
for (b in beacons) {
Toast.makeText(getApplicationContext(),b.id1.toString() + " beacon I see is about " + b.rssi + " away.",
Toast.LENGTH_SHORT).show();
}
}
})
try {
beaconManager.startRangingBeaconsInRegion(Region("myRangingUniqueId", Identifier.parse(""), null, null))
} catch (e: RemoteException) {
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding : com.example.attendancecheck_v1.databinding.FragmentTeacherScanningBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_teacher_scanning, container, false)
beaconManager.bind(this)
setHasOptionsMenu(true)
return binding.root
}
顺便说一句, 我在我的设备上安装了一个信标模拟器,它可以扫描信标,但在我的代码中, 这些设备上的输出为零.
用这个区域检查一下:
beaconManager.startRangingBeaconsInRegion(Region("myRangingUniqueId", null, null, null))