蓝牙入门代码

1.MyAdapter代码

/**
 * Created by wangcaiwen on 2017/8/15.
 */

public class MyAdapter extends BaseAdapter {

    private ArrayList mList;
    private Context context;

    public MyAdapter(ArrayList mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
if(view==null) {
    view = View.inflate(context, R.layout.item, null);
    holder=new ViewHolder();
    holder.tv_address=view.findViewById(R.id.tv_address);
    holder.tv_name=view.findViewById(R.id.tv_name);
    holder.tv_state=view.findViewById(R.id.tv_state);

    view.setTag(holder);
}else{
   holder = (ViewHolder) view.getTag();
}

//设置值
        BluetoothDevice device= (BluetoothDevice) getItem(i);

        holder.tv_address.setText(device.getAddress());
        holder.tv_name.setText(device.getName());
        int state = device.getBondState();
        switch (state){
            case BluetoothDevice.BOND_BONDED:
                holder.tv_state.setText("已配对");
                break;
            case BluetoothDevice.BOND_NONE:
                holder.tv_state.setText("未配对");
                break;
            case BluetoothDevice.BOND_BONDING:
                holder.tv_state.setText("正在配对");
                break;

        }

        return view;
    }

    class ViewHolder{
        public TextView tv_address;
        public TextView tv_name;
        public TextView tv_state;
    }
}

2.activity代码

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private ArrayList mList;
    private BluetoothAdapter mBlueAdapter;
    private MyAdapter ma;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();

        //注册广播接收器,用于接受蓝牙相关的广播
        registerBlueToothReceiver();
    }

    /**
     * 注册蓝牙广播
     */
    private void registerBlueToothReceiver() {


    }

    /**
     * 初始化数据
     */
    private void initData() {
        mList = new ArrayList<>();


        mBlueAdapter = BluetoothAdapter.getDefaultAdapter();

        mBlueAdapter.enable();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
    }

    /**
     * 扫描设备
     * @param v
     */
    public void scan(View v){
        searchBondedDevice();
        searchUnBondedDevice();
        setAdapter();

    }

    /**
     * 设置适配器,刷新数据
     */
    private void setAdapter() {
        if(ma==null){
            ma = new MyAdapter(mList,MainActivity.this);
            lv.setAdapter(ma);
        }else{
            ma.notifyDataSetChanged();
        }
    }

    /**
     * 搜索未配对的设备
     */
    private void searchUnBondedDevice() {
        if(mBlueAdapter.isDiscovering()){
            mBlueAdapter.cancelDiscovery();
        }
        mBlueAdapter.startDiscovery();
    }

    /**
     * 搜索已配对的设备
     */
    private void searchBondedDevice() {
        Set bondedDevices = mBlueAdapter.getBondedDevices();
        for (BluetoothDevice bd : bondedDevices) {
            if(!mList.contains(bd)){
                mList.add(bd);
            }
        }

    }

    /**
     * 广播接收器
     */
    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //蓝牙设备
           BluetoothDevice device= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            //发现蓝牙设备的逻辑
            if(action.equals(BluetoothDevice.ACTION_FOUND)){
                    //是否包含此设备
                if(!mList.contains(device)){
                    mList.add(device);
                }
                //刷新数据
                setAdapter();
                //蓝牙状态监听
            }else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
                //得到蓝牙状态
                int bondState = device.getBondState();
                switch(bondState){
                    case BluetoothDevice.BOND_BONDED:
                       mList.remove(device);
                        mList.add(0,device);
                        setAdapter();
                        break;

                    case BluetoothDevice.BOND_BONDING:
                        Toast.makeText(MainActivity.this, "正在配对...", Toast.LENGTH_SHORT).show();
                        break;

                    case BluetoothDevice.BOND_NONE:
                        Toast.makeText(MainActivity.this, "未配对", Toast.LENGTH_SHORT).show();
                        break;
                }

            }

        }
    }
}




你可能感兴趣的:(移动互联基础)