通过蓝牙LE扫描阻止对服务器的异步调用

时间:2015-07-28 13:08:47

标签: android rest android-asynctask bluetooth

我有一个Activity构造一个RESTManager类(用于对服务器进行异步调用并将数据返回到Activity)。

然而,在构建我的RESTManager并使用

调用服务器之后
        //Retrieve data from server
        RESTManager m = new RESTManager(this,getApplicationContext());
        //Set the data list
        m.delegate=this;
        m.retrieveRoomData();

服务器通话执行直到我关闭蓝牙扫描。

但是,我在RESTManager之后立即开始我的蓝牙扫描:

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setBackgroundScanPeriod(1100l);
    beaconManager.setBackgroundBetweenScanPeriod(100l);
    beaconManager.setForegroundScanPeriod(1100l);
    beaconManager.setForegroundBetweenScanPeriod(100l);

    //Set the custom BeaconLayout for iBeacons
    if (myPref.getBoolean("layoutSet", true)) {

        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
        backgroundPowerSaver = new BackgroundPowerSaver(this);
        Log.v("Beacon Layout", "Beacon Layout Set");
        myPref.edit().putBoolean("layoutSet", false).apply();
        myPref.edit().commit();
    }
    beaconManager.bind(this);

我没有收到任何错误。

有没有理由说我的RESTManager的AsyncTask会挂起蓝牙扫描?

相关代码:

RESTManager类

    public class RESTManager {
    private DateTime lastUpdate = DateTime.now();
    public AsyncResponse delegate=null;
    private SharedPreferences mPrefs;
    private SharedPreferences.Editor preferenceEditor;
    private ArrayList<RoomData> roomDataList = new ArrayList<RoomData>();
    private ArrayList<MeetingData> meetingDataList = new ArrayList<MeetingData>();
    private Activity currentActivity;
    private Context context;

    public RESTManager(Activity currentActivity, Context context) {
        this.currentActivity = currentActivity;
        this.context = context;
        mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    //Todo update meeting data logic
    public ArrayList<MeetingData> retrieveMeetingDataFromServer() {

        return null;
    }

    public void retrieveRoomData() {
        new CallServerForRoomData().execute();
    }

    //TODO add timestamping logic.
    private class CallServerForRoomData extends AsyncTask<String, Void, String> {
        String myJsonData = "";

        @Override
        protected String doInBackground(String... params) {

            //If the data isn't cached, call the server.
            if (!mPrefs.contains("RoomData")) {
                try {
                    setupHttpClient();
                    HttpClient myClient = new HttpClient(RequestMethod.GET, "http://10.184.46.217:9012/v1/rooms", new HttpHeaders(), null, null);
                    myClient.connect();
                    HttpResponse mR = myClient.processResponse();
                    myJsonData = mR.getServerResponseAsString();
                    System.out.println(myJsonData);

                    preferenceEditor = mPrefs.edit();
                    preferenceEditor.putString("RoomData", myJsonData);
                    preferenceEditor.commit();
                    setRoomDataList(convertRoomDataJson(myJsonData));
                    return "server";
                } catch (HttpClientException e) {
                    e.printStackTrace();
                }
            }
            //If it is cached, retrieve the data locally.
            else {
                setRoomDataList(convertRoomDataJson(mPrefs.getString("RoomData", "NULL")));
                return "local";
            }

            return "Done";
        }

        protected void onPostExecute(final String result) {
            delegate.dataFinishedRetrieving(getRoomDataList());
            // setRoomDataList(convertRoomDataJson(myJsonData));
            currentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "Data retrieved on " + result + " storage", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * AsyncTask for retrieving a room's meeting data.
     * Pass in the room's unique identifier as a parameter of the execute method.
     */
    //TODO add timestamping logic
    private class CallServerForMeetingData extends AsyncTask<String, Void, String> {
        String myJsonData = "";

        @Override
        protected String doInBackground(String... params) {

            try {
                setupHttpClient();
                HttpClient myClient = new HttpClient(RequestMethod.GET, "http://10.184.146.217:9012/v1/room/" + params[0] + "/schedule", new HttpHeaders(), null, null);
                myClient.connect();
                HttpResponse mR = myClient.processResponse();
                myJsonData = mR.getServerResponseAsString();


            } catch (HttpClientException e) {
                e.printStackTrace();
            }

            //Set the converted MeetingData to the RoomData object
            for (RoomData e : roomDataList) {
                if (e.getObjectId() == params[0]) {
                    e.setMeetingData(convertMeetingDataJson(myJsonData));
                }
            }

            return "Done";
        }
    }

    //Initializes the HTTPClient and attaches it to the application lifecycle.
    public void setupHttpClient() {
        HttpCacheManager.init(Application.getAppContext());
    }

    public ArrayList<MeetingData> convertMeetingDataJson(String JsonData) {
        final Gson gson = initCustomGSON();
        Type MeetingDataListType = null;
        if (JsonData != "") {
            MeetingDataListType = new TypeToken<ArrayList<MeetingData>>() {
            }.getType();
        }
        return (gson.fromJson(JsonData, MeetingDataListType));
    }

    public ArrayList<RoomData> convertRoomDataJson(String JsonData) {
        final Gson gson = initCustomGSON();
        Type RoomDataListType = null;
        if (!JsonData.equals("")) {
            RoomDataListType = new TypeToken<ArrayList<RoomData>>() {
            }.getType();
            roomDataList = (gson.fromJson(JsonData, RoomDataListType));
        }
        roomDataList = (gson.fromJson(JsonData, RoomDataListType));
        for (RoomData e : roomDataList) {
            Log.v("RoomData name", e.getName());
            Log.v("RoomData roomId", e.getObjectId());
            //Log.v("RoomData imageUrl", e.getImageUrl());
            if (e.getBeacons() != null) {
                Log.v("Number of beacons", e.getBeacons().toString());
            }
        }
        return roomDataList;
    }

    /**
     * Initializes the GSON Json Decoder with the custom JodaTime serializers.
     *
     * @return
     */
    public Gson initCustomGSON() {
        final GsonBuilder builder = new GsonBuilder();
        JodaTimeConverters converter = new JodaTimeConverters();
        converter.registerAll(builder);
        return builder.create();
    }

    public ArrayList<RoomData> getRoomDataList() {
        return roomDataList;
    }

    public void setRoomDataList(ArrayList<RoomData> roomDataList) {
        this.roomDataList = roomDataList;
    }

    public void setMeetingDataListForRoom(RoomData whichRoom) {
        new CallServerForMeetingData().execute(whichRoom.getObjectId());
    }
 }

0 个答案:

没有答案