Write in front
Reference( https://www.jianshu.com/p/0b356e7e38bf ) This article gives a comprehensive explanation of how to detect Bluetooth devices in Android BLE framework. And we can get routines from it.( https://github.com/a1anwang/okble (2) The results of the study are as follows:1) The results of the study are as follows:1) The results of the study are as follows:1) The results of the study are as follows:1. This article simply changes the content of the routine so that it can selectively store the information about the detected ibeacon. For example, we have prepared four RSSI values of ibeacon that have been named in advance (bao,jiao,shi,RDL52832).
Some explanations for this routine
Because our goal is to parse ibeacon, let's first look at his scanibeacon functionality and make changes based on it.
We see that ScanBeaconActivity is an extension of BaseActivity, so we need to understand the content of BaseActivity first, and list the points that need attention here:
There must be several functions: setContentLayout (which is the function to set the layout) beforeInitView (which is the function before initializing the layout, so you should be careful not to add functions such as getScreenWidth to extract layout information, which can cause crashes), initView (which is the function to initialize the layout, from which you can set it). Titles and so on, you can extract layout information from here) afterInitView (), onClickEvent(View v).
Read and store the required ibeacon information
We first define four variables to store RSSI values, and create several functions to store and read the values of variables.
private int bao = 100; public int getbao() { return bao; }; public void setbao(int bao) { this.bao = bao; } private int shi = 100; public int getshi() { return shi; }; public void setshi(int shi) { this.shi = shi; } private int jiao = 100; public int getjiao() { return jiao; }; public void setjiao(int jiao) { this.jiao = jiao; } private int rdl = 100; public int getrdl() { return rdl; }; public void setrdl(int rdl) { this.rdl = rdl; }
Then we read the feedback from the Bluetooth module. We can see that there is a scancallback function which represents the feedback from the search for Bluetooth. We need to modify the inside of it.
OKBLEBeaconManager.OKBLEBeaconScanCallback scanCallBack=new OKBLEBeaconManager.OKBLEBeaconScanCallback() { @Override public void onScanBeacon(OKBLEBeacon beacon) { scanedResults.put(beacon.getIdentifier(),beacon); adapter.notifyDataSetChanged(); } };
Notice that there is a beacon variable where beacon.getIdentifier() is the ID resource to get beacon, so we can also use beacon.getUuid to get the value of his RSSI.
OKBLEBeaconManager.OKBLEBeaconScanCallback scanCallBack=new OKBLEBeaconManager.OKBLEBeaconScanCallback() { @Override public void onScanBeacon(OKBLEBeacon beacon) { scanedResults.put(beacon.getIdentifier(), beacon); switch(beacon.getName()) { case "bao": setbao(beacon.getRssi()); break; case "shi": setshi(beacon.getRssi()); break; case "jiao": setjiao(beacon.getRssi()); break; case "RDL52832": setrdl(beacon.getRssi()); break; default: setbao(0); } } };
In this way, the searched ibeacon is judged and the results that meet our requirements are stored in the corresponding variables.
Write at the end
This is the third blog of our group. Please forgive me for not writing well. We are also learning while doing and telling the truth about the above things I still do not understand, we should give you a reference, hoping to help you. If you have any questions, please point out and let me know. I will change them in time or delete them directly without changing them (laughter).