學了BLE以後,開始作android掃描程序。掃描附近的BLE設備並顯示出來。java
參考android高級編程第4章P113android
準備工做,新建項目,將寫好的掃描ble的類添加進去。git
(1)首先,在res/layout 文件夾中建立一個新的佈局文件,new_device_fragment.xml,此文件中包含來自main.xml的Button結點:github
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scan_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="scan" > </Button>
(2)爲每一個UI組件建立一個新的Fragment。首先建立一個繼承於Fragemnt的NewDeviceFragment。重寫OnCreateView處理程序來填充第一步建立的佈局。編程
public class ScanDeviceFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.scan_device_fragment, container, false); //return super.onCreateView(inflater, container, savedInstanceState); } }
(3)每一個fragment應該封裝它所提供的功能,對於NewDeviceFragment來講,就是接收用戶的點擊,開始掃描。當掃描到新的設備時,添加到列表中。數組
首先定義一個接口,MainActivity經過實現該接口來監聽新設備的添加。ide
public interface OnNewDeviceAddedListener{ public void onNewDeviceAdded(BleDevice newDevice); }
(4)建立一個變量來保存實現了這個接口的Activity類的引用。一旦Fragment綁定到它的父activity,就能夠在OnAttach中得到該activity的引用。佈局
private OnNewDeviceAddedListener onNewDeviceAddedListener; @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try{ onNewDeviceAddedListener = (OnNewDeviceAddedListener)activity; } catch (ClassCastException e){ throw new ClassCastException(activity.toString() + "must implement onNewDeviceAddedListener"); } }
(5)將button.onClickListener移入Fragment中。當用戶點擊button時,不是直接向list中添加文本,而是把它傳遞給父activity的OnNewDeviceAddedListener.onNewDeviceAdded實現中。this
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.scan_device_fragment, container, false); final Button scan_btn = (Button)view.findViewById(R.id.scan_btn); scan_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub BleScan blescanservice = new BleScan(); blescanservice.onSetUp(); blescanservice.scanLeDevice(true); } }); return view;
(6)接下來,建立包含bledevice列表的Fragment。android提供ListFragment類,它能夠很容易地建立基於Fragment的簡單的ListView。建立一個新的繼承於ListFragment的類。spa
public class DeviceListFragment extends ListFragment { }
(7)完成了Fragment,該返回Activity了。首先更新main.xml佈局。添加scandevicefragment和devicelistfragment。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ble.blescanner.MainActivity" > <fragment android:name="com.ble.blescanner.ScanDeviceFragment" android:id="@+id/scanfragmnet" android:layout_width="match_parent" android:layout_height="wrap_content"/> <fragment android:name="com.ble.blescanner.DeviceListFragment" android:id="@+id/devicefragment" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
(8)回到MainActivity中。在OnCreat方法中,在給devicelist Fragment建立和分配適配器以前,先經過FragmentManager獲取devicelist Fragment的引用。由於List View和Button View此時封裝在Fragment中,因此不須要在Activity中獲取它們的引用。須要把Array Adapter和Array List的做用域擴展爲類變量。
private ArrayAdapter<BleDevice> aa; private ArrayList<BleDevice> devices; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取該Fragment的引用 FragmentManager fm = getFragmentManager(); DeviceListFragment devicelistFragment = (DeviceListFragment)fm.findFragmentById(R.id.devicelistfragment); //建立device的arraylsit devices = new ArrayList<BleDevice>(); //建立ArrayAdapter用來將數組綁定到listview上 aa = new ArrayAdapter<BleDevice>(this, android.R.layout.simple_list_item_1, devices); //將ArrayAdapter綁定到listview上 devicelistFragment.setListAdapter(aa); }
(9)已經經過適配器將listView和Arraylist鏈接到一塊兒,因此剩下的就是把scandevice Fragment中建立的任何一個新的設備添加進來。首先聲明mainActivity將實現第3步中在scandevice Fragmentzhong 定義的OnNewDeviceAddedListener接口
public class MainActivity extends Activity implements ScanDeviceFragment.OnNewDeviceAddedListener{
(10)最後,經過實現onNewDeviceAdded處理程序來實現監聽。在通知ArrayAdapter數據集已改變以前,把接收到的字符串變量添加到ArrayList中。
@Override public void onNewDeviceAdded(BleDevice newDevice) { // TODO Auto-generated method stub devices.add(newDevice); aa.notifyDataSetChanged(); }
代碼下載https://github.com/pearl2015/workspace.git /BleScanner
下一篇:將添加字符串改爲添加ble device,修改Adapter