我怎样才能搜索Beacon

时间:2018-05-08 15:07:10

标签: beacon

我在这些Android版本(Android 7.0 API 24,Android 6.0.1 API 23)中看不到信标,我看到消息"找不到回调包装"但Android 4.4.4,API 19完美运行

这是我的代码:public class Tab3Helper extends Fragment实现BeaconConsumer {     //相对布局     RelativeLayout rl;     // Recycler View     私人RecyclerView rv;     private RecyclerView.LayoutManager layoutManager;     private RecyclerView.Adapter适配器;     //灯塔经理     私人BeaconManager beaconManager;     // 进度条     私人ProgressBar pb;     //新     public static final Identifier 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 ( );
    }

}

1 个答案:

答案 0 :(得分:1)

Android 6.0+为扫描蓝牙信标添加了几项新限制:

  1. 必须在设置中打开位置。如果关闭,则不会检测到信标。
  2. 应用必须在运行时获取用户的位置权限。如果他们无法执行此操作,则扫描将无提示失败。
  3. 对于上面的第2项,请遵循this

    等指南