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设备中的固件dump出来
  2. 判断android设备是否支持多点触控
  3. Android O 设备不允许安装第三方APP
  4. android获取设备屏幕分辨率的方法
  5. Jquery 判断是否 移动设备 浏览
  6. 访问android所有可以使用的外接设备
  7. android 串口通信,蓝牙通信等需要输出十六进制数据的方法

随机推荐

  1. Android 倒计时(支持小时,分钟)
  2. Android学习日记(1)
  3. Android中build target,minSdkVersion,targ
  4. 获取Android正在运行的任务和服务
  5. Android SystemUI状态栏添加图标
  6. Android小技巧&Android Studio快捷键(不定
  7. 说说在 Android 如何在全局获取 Context
  8. Android猜牌小游戏(改进版)
  9. Android设置背景图像重复【整理自网络】
  10. Android - 设定更改主Activity