1,添加权限

<uses-permission android:name="android.permission.BLUETOOTH" />    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

2,蓝牙控制类

package com.win16.bluetoothclass3;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.Context;import android.content.Intent;import java.util.ArrayList;import java.util.List;/** * Created by eandroid on 2015/5/27. */public class BlueToothController {    private BluetoothAdapter mAapter;    public BlueToothController() {        mAapter = BluetoothAdapter.getDefaultAdapter();    }    /** * 打开蓝牙 * @param activity * @param requestCode */    public void turnOnBlueTooth(Activity activity, int requestCode) {        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);        activity.startActivityForResult(intent, requestCode);// mAdapter.enable();    }    /** * 打开蓝牙可见性 * @param context */    public void enableVisibly(Context context) {        Intent discoverableIntent = new                Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);        context.startActivity(discoverableIntent);    }    /** * 查找设备 */    public void findDevice() {        assert (mAapter != null);        mAapter.startDiscovery();    }    /** * 获取绑定设备 * @return */    public List<BluetoothDevice> getBondedDeviceList() {        return new ArrayList<>(mAapter.getBondedDevices());    }}

3,蓝牙列表适配

package com.win16.bluetoothclass3;import android.bluetooth.BluetoothDevice;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.ArrayList;import java.util.List;/** * Created by eandroid on 2015/5/27. */public class DeviceAdapter extends BaseAdapter {    private List<BluetoothDevice> mData;    private Context mContext;    public DeviceAdapter(List<BluetoothDevice> data, Context context) {        mData = data;        mContext = context.getApplicationContext();    }    @Override    public int getCount() {        return mData.size();    }    @Override    public Object getItem(int i) {        return mData.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        View itemView = view;        //复用View,优化性能        if( itemView == null) {            itemView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_2,viewGroup,false);        }        TextView line1 = (TextView) itemView.findViewById(android.R.id.text1);        TextView line2 = (TextView) itemView.findViewById(android.R.id.text2);        //获取对应的蓝牙设备        BluetoothDevice device = (BluetoothDevice) getItem(i);        //显示名称        line1.setText(device.getName());        //显示地址        line2.setText(device.getAddress());        return itemView;    }    public void refresh(List<BluetoothDevice> data) {        mData = data;        notifyDataSetChanged();    }}

4,界面展示

package com.win16.bluetoothclass3;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewConfiguration;import android.view.Window;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;/** * Created by eandroid on 2015/5/27. */public class MainActivity extends Activity {    public static final int REQUEST_CODE = 0;    private List<BluetoothDevice> mDeviceList = new ArrayList<>();    private List<BluetoothDevice> mBondedDeviceList = new ArrayList<>();    private BlueToothController mController = new BlueToothController();    private ListView mListView;    private DeviceAdapter mAdapter;    private Toast mToast;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        initActionBar();        setContentView(R.layout.activity_main);        initUI();        IntentFilter filter = new IntentFilter();        //开始查找        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);        //结束查找        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);        //查找设备        filter.addAction(BluetoothDevice.ACTION_FOUND);        //设备扫描模式改变        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);        //绑定状态        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);        registerReceiver(mReceiver, filter);        mController.turnOnBlueTooth(this, REQUEST_CODE);    }    private BroadcastReceiver mReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            if( BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action) ) {                setProgressBarIndeterminateVisibility(true);                //初始化数据列表                mDeviceList.clear();                mAdapter.notifyDataSetChanged();            }            else if( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {                setProgressBarIndeterminateVisibility(false);            }            else if( BluetoothDevice.ACTION_FOUND.equals(action)) {                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                //找到一个,添加一个                mDeviceList.add(device);                mAdapter.notifyDataSetChanged();            }            else if( BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {               int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,0);                if( scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {                    setProgressBarIndeterminateVisibility(true);                }                else {                    setProgressBarIndeterminateVisibility(false);                }            }            else if( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) ) {                BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                if( remoteDevice == null ) {                    showToast("no device");                    return;                }                int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,0);                if( status == BluetoothDevice.BOND_BONDED) {                    showToast("Bonded " + remoteDevice.getName());                }                else if( status == BluetoothDevice.BOND_BONDING){                    showToast("Bonding " + remoteDevice.getName());                }                else if(status == BluetoothDevice.BOND_NONE){                    showToast("Not bond " + remoteDevice.getName());                }            }        }    };    private void initUI() {        mListView = (ListView) findViewById(R.id.device_list);        mAdapter = new DeviceAdapter(mDeviceList, this);        mListView.setAdapter(mAdapter);        mListView.setOnItemClickListener(bindDeviceClick);    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(mReceiver);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    private void showToast(String text) {        if( mToast == null) {            mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);        }        else {            mToast.setText(text);        }        mToast.show();    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.enable_visiblity) {            mController.enableVisibly(this);        }        else if( id == R.id.find_device) {            //查找设备            mAdapter.refresh(mDeviceList);            mController.findDevice();            mListView.setOnItemClickListener(bindDeviceClick);        }        else if (id == R.id.bonded_device) {            //查看已绑定设备            mBondedDeviceList = mController.getBondedDeviceList();            mAdapter.refresh(mBondedDeviceList);            mListView.setOnItemClickListener(null);        }        return super.onOptionsItemSelected(item);    }    private AdapterView.OnItemClickListener bindDeviceClick = new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {            BluetoothDevice device = mDeviceList.get(i);            device.createBond();        }    };    private void initActionBar() {        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        getActionBar().setDisplayUseLogoEnabled(false);        setProgressBarIndeterminate(true);        try {            ViewConfiguration config = ViewConfiguration.get(this);            Field menuKeyField = ViewConfiguration.class                    .getDeclaredField("sHasPermanentMenuKey");            if (menuKeyField != null) {                menuKeyField.setAccessible(true);                menuKeyField.setBoolean(config, false);            }        } catch (Exception e) {            e.printStackTrace();        }    }}

5,界面布局

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <ListView  android:id="@+id/device_list" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></RelativeLayout>

源码下载

http://download.csdn.net/detail/eandroidhu/9433846

更多相关文章

  1. Android(安卓)获得可用摄像头id
  2. Android(安卓)Action
  3. Android蓝牙开发浅谈
  4. 如何把android设备中的固件dump出来
  5. android 内核添加tourch screen
  6. 判断android设备是否支持多点触控
  7. android中简单的listview代码
  8. Jquery 判断是否 移动设备 浏览
  9. android获取设备屏幕分辨率的方法

随机推荐

  1. Android屏幕中隐藏标题栏
  2. android google map apiKey 相关操作
  3. Android单选框(RadioButton)
  4. android 动态向Gallery中添加图片及倒影&
  5. android 中文 api (71) —— BluetoothServ
  6. android移植详解
  7. FloatLabel介绍——Android(安卓)EditTex
  8. Android(安卓)OpenGL学习笔记(二)--三角
  9. Android中贪吃蛇游戏的学习(五)
  10. [安卓基础] 001.学习Android开发的好教程