如何搜索 Beacon
How can I search Beacon
我在这些 android 版本中看不到信标(Android 7.0 API 24,Android 6.0.1 API 23)我看到了消息 "could not find callback wrapper" 但是 Android 4.4.4,API 19 完美运行
这是我的代码:public class Tab3Helper extends Fragment implements BeaconConsumer {
//相对布局
相对布局rl;
//回收站视图
私人 RecyclerView 房车;
私有 RecyclerView.LayoutManager 布局管理器;
私有 RecyclerView.Adapter 适配器;
//信标管理器
私人 BeaconManager beaconManager;
// 进度条
私有 ProgressBar pb;
//新的
public 静态最终标识符 MY_MATCHING_IDENTIFIER = Identifier.fromInt(0x8b9c);
//结尾
@覆盖
public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
//getting beaconManager instance (object) for Main Activity class
beaconManager = BeaconManager.getInstanceForApplication (getActivity ( ));
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
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"));
//Binding MainActivity to the BeaconService.
beaconManager.bind (this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate (R.layout.tab3helper, container, false);
// Intializing the Layout
//Relative Layout
rl = v.findViewById (R.id.Relative_One);
// Recycler View
rv = v.findViewById (R.id.search_recycler);
//Progress Bar
// pb = v.findViewById(R.id.pb);
return v;
}
public void onBeaconServiceConnect() {
final Region region = new Region("myBeaons",null, null, null);
beaconManager.addMonitorNotifier (new MonitorNotifier ( ) {
public void didEnterRegion(Region region) {
try {
// Log.d(TAG, "didEnterRegion");
beaconManager.startRangingBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
public void didExitRegion(Region region) {
try {
//Log.d(TAG, "didExitRegion");
beaconManager.stopRangingBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
public void didDetermineStateForRegion(int state, Region region) {
System.out.println( "I have just switched from seeing/not seeing beacons: "+state);
}
});
beaconManager.addRangeNotifier (new RangeNotifier ( ) {
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
// Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
/*
for (Beacon oneBeacon : beacons) {
System.out.println ("Major value =" +oneBeacon.getId2 ()+ "size =" +beacons.size () + "*");
}
*/
if(beacons.size()>0){
//System.out.print("**"+beacons.size()+"**");
try{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Make ProgressBar Invisible
//pb.setVisibility(View.INVISIBLE);
// Make Relative Layout to be Gone
rl.setVisibility(View.GONE);
//Make RecyclerView to be visible
rv.setVisibility(View.VISIBLE);
// Setting up the layout manager to be linear
layoutManager = new LinearLayoutManager(getActivity());
rv.setLayoutManager(layoutManager);
}
});
}
catch(Exception e){
}
final ArrayList<ArrayList<String>> arrayList = new ArrayList<ArrayList<String>>();
// Iterating through all Beacons from Collection of Beacons
for (Beacon b:beacons) {
//new
String receivedString = null;
// byte[] bytes = b.getId2().toByteArray();
//byte[] bytes = b.getId2().toByteArray();
byte[] bytes = b.getId1().toByteArray();
receivedString = null;
try {
receivedString = new String(bytes, 0, bytes.length, "ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String uuid = receivedString;
//end new
//UUID
// String uuid = String.valueOf(b.getId1());
//Major
String major = String.valueOf(b.getId2());
//Minor
String minor = String.valueOf(b.getId3());
// test
//Distance
double distance1 = b.getDistance();
String distance = String.valueOf(Math.round(distance1 * 100.0) / 100.0);
//Name
String nameUser = b.getBluetoothName();
ArrayList<String> arr = new ArrayList<String>();
arr.add(uuid);
arr.add(major);
arr.add(minor);
arr.add(distance + " meters");
arr.add(nameUser);
arrayList.add(arr);
//System.out.print("**"+b.getId1()+"**");
//System.out.print("**"+arrayList.size()+"**");
}
try {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Setting Up the Adapter for Recycler View
adapter = new RecyclerAdapter(arrayList);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}catch(Exception e){
}
//fin
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
Android 6.0+增加了几个扫描蓝牙信标的新限制:
- 必须在设置中打开位置。如果关闭,则不会检测到信标。
- 应用程序必须在运行时从用户那里获得位置权限。如果他们不这样做,那么扫描将静默失败。
对于上面的第 2 项,请遵循 this
这样的指南
我在这些 android 版本中看不到信标(Android 7.0 API 24,Android 6.0.1 API 23)我看到了消息 "could not find callback wrapper" 但是 Android 4.4.4,API 19 完美运行
这是我的代码:public class Tab3Helper extends Fragment implements BeaconConsumer { //相对布局 相对布局rl; //回收站视图 私人 RecyclerView 房车; 私有 RecyclerView.LayoutManager 布局管理器; 私有 RecyclerView.Adapter 适配器; //信标管理器 私人 BeaconManager beaconManager; // 进度条 私有 ProgressBar pb; //新的 public 静态最终标识符 MY_MATCHING_IDENTIFIER = Identifier.fromInt(0x8b9c); //结尾 @覆盖 public void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState);
//getting beaconManager instance (object) for Main Activity class
beaconManager = BeaconManager.getInstanceForApplication (getActivity ( ));
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
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"));
//Binding MainActivity to the BeaconService.
beaconManager.bind (this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate (R.layout.tab3helper, container, false);
// Intializing the Layout
//Relative Layout
rl = v.findViewById (R.id.Relative_One);
// Recycler View
rv = v.findViewById (R.id.search_recycler);
//Progress Bar
// pb = v.findViewById(R.id.pb);
return v;
}
public void onBeaconServiceConnect() {
final Region region = new Region("myBeaons",null, null, null);
beaconManager.addMonitorNotifier (new MonitorNotifier ( ) {
public void didEnterRegion(Region region) {
try {
// Log.d(TAG, "didEnterRegion");
beaconManager.startRangingBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
public void didExitRegion(Region region) {
try {
//Log.d(TAG, "didExitRegion");
beaconManager.stopRangingBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
public void didDetermineStateForRegion(int state, Region region) {
System.out.println( "I have just switched from seeing/not seeing beacons: "+state);
}
});
beaconManager.addRangeNotifier (new RangeNotifier ( ) {
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
// Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
/*
for (Beacon oneBeacon : beacons) {
System.out.println ("Major value =" +oneBeacon.getId2 ()+ "size =" +beacons.size () + "*");
}
*/
if(beacons.size()>0){
//System.out.print("**"+beacons.size()+"**");
try{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Make ProgressBar Invisible
//pb.setVisibility(View.INVISIBLE);
// Make Relative Layout to be Gone
rl.setVisibility(View.GONE);
//Make RecyclerView to be visible
rv.setVisibility(View.VISIBLE);
// Setting up the layout manager to be linear
layoutManager = new LinearLayoutManager(getActivity());
rv.setLayoutManager(layoutManager);
}
});
}
catch(Exception e){
}
final ArrayList<ArrayList<String>> arrayList = new ArrayList<ArrayList<String>>();
// Iterating through all Beacons from Collection of Beacons
for (Beacon b:beacons) {
//new
String receivedString = null;
// byte[] bytes = b.getId2().toByteArray();
//byte[] bytes = b.getId2().toByteArray();
byte[] bytes = b.getId1().toByteArray();
receivedString = null;
try {
receivedString = new String(bytes, 0, bytes.length, "ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String uuid = receivedString;
//end new
//UUID
// String uuid = String.valueOf(b.getId1());
//Major
String major = String.valueOf(b.getId2());
//Minor
String minor = String.valueOf(b.getId3());
// test
//Distance
double distance1 = b.getDistance();
String distance = String.valueOf(Math.round(distance1 * 100.0) / 100.0);
//Name
String nameUser = b.getBluetoothName();
ArrayList<String> arr = new ArrayList<String>();
arr.add(uuid);
arr.add(major);
arr.add(minor);
arr.add(distance + " meters");
arr.add(nameUser);
arrayList.add(arr);
//System.out.print("**"+b.getId1()+"**");
//System.out.print("**"+arrayList.size()+"**");
}
try {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Setting Up the Adapter for Recycler View
adapter = new RecyclerAdapter(arrayList);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}catch(Exception e){
}
//fin
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion (region);
} catch (RemoteException e) {
e.printStackTrace ( );
}
}
Android 6.0+增加了几个扫描蓝牙信标的新限制:
- 必须在设置中打开位置。如果关闭,则不会检测到信标。
- 应用程序必须在运行时从用户那里获得位置权限。如果他们不这样做,那么扫描将静默失败。
对于上面的第 2 项,请遵循 this
这样的指南