BLE 扫描及链接 android程序开发(一)


学了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

相关文章
相关标签/搜索