蓝牙的设置



1.oncreate中

listView = (ListView) findViewById(R.id.list_view);

            //获取蓝牙适配器对象
           bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

           //首先,如果要操作蓝牙,先判断当前的手机手否存在蓝牙
           if (bluetoothAdapter != null){
            //判断蓝牙是否可用
                if (! bluetoothAdapter.isEnabled()){

                //让蓝牙处于可用状态
                bluetoothAdapter.enable();

                //bluetoothAdapter.disable();//不可用,关闭
                  }

             }

            myReceiver = new MyReceiver();
              //动态注册一个广播接收者
           IntentFilter filter = new IntentFilter();

         //指定广播接收的那个动作...BluetoothDevice.ACTION_FOUND发现了一个蓝牙设备
          filter.addAction(BluetoothDevice.ACTION_FOUND);

        //再添加一个动作,,,配对状态改变的时候的动作
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

        registerReceiver(myReceiver,filter);

        //点击条目的时候跟当前未配对的设配进行配对
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                BluetoothDevice bluetoothDevice = list.get(i);
                //没有配对的时候,去配对....使用反射去实现
                if (BluetoothDevice.BOND_NONE == bluetoothDevice.getBondState()){
                    //首先通过MAC地址去获取要配对的设备
                    BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(bluetoothDevice.getAddress());

                    //使用反射去配对
                    try {
                        Method method = BluetoothDevice.class.getMethod("createBond", null);

                        method.invoke(remoteDevice,null);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }

            }
        });


     2.  点击事件中

     //搜索设备的点击事件
    public void searchDevice(View view){

        //1.搜索配对过的设备
        searchBoundDevice();
        //2.扫描周边的蓝牙设备
        searchUnboundDevice();

        //3.设置适配器
        setAdapter();
    }

   3.方法

 /**
     * 扫描没有配对的设备
     */
    private void searchUnboundDevice() {
        //如果现在蓝牙正在扫描
        if (bluetoothAdapter.isDiscovering()){//directory目录,文件夹

            bluetoothAdapter.cancelDiscovery();//结束当前正在执行的扫描
        }

        //开始本次的扫描
        bluetoothAdapter.startDiscovery();//返回值是boolean类型,代表本次扫描是否已经开始


        //开始扫描之后,,,,一旦扫描到设备之后,,,手机会发送广播,,,所以我们要获取发送过来有关蓝牙设备的信息,需要写一个广播接收者
    }

    /**
     * 该方法是:搜索已经配对的设备
     */
    private void searchBoundDevice() {

        //获取已经配对的设备,,,返回值是set集合,,泛型就是蓝牙设备
        Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();

        for (BluetoothDevice device: devices) {
            //添加到list集合
            if (! list.contains(device)){
                list.add(device);
            }
        }
    }

 private void setAdapter() {
        if (adapter == null){

            adapter = new ListViewAdapter(MainActivity.this, list);
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

4.广播接收者

 private class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //获取传递过来的数据....实际上就是扫描到的蓝牙设备
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            //先获取一下当前广播接收的动作
            String action = intent.getAction();

            if (action.equals(bluetoothDevice.ACTION_FOUND)){//当前接收者接收的是扫描的广播发出来的信息
                if (! list.contains(bluetoothDevice)){
                    list.add(bluetoothDevice);
                }

                //设置适配器
                setAdapter();
            }else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
                //首先获取到状态....对比状态
                int bondState = bluetoothDevice.getBondState();
                switch (bondState){
                    case BluetoothDevice.BOND_NONE:
                        Toast.makeText(MainActivity.this,"配对失败",Toast.LENGTH_SHORT).show();
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        Toast.makeText(MainActivity.this,"正在配对",Toast.LENGTH_SHORT).show();

                        break;
                    case BluetoothDevice.BOND_BONDED:
                        Toast.makeText(MainActivity.this,"配对成功",Toast.LENGTH_SHORT).show();

                        list.remove(bluetoothDevice);
                        list.add(0,bluetoothDevice);

                        setAdapter();

                        break;
                }
            }
        }
    }

5.适配器中

 Context context;
    List<BluetoothDevice> list;

    public ListViewAdapter(Context context, List<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = View.inflate(context,R.layout.item_layout,null);
            holder = new ViewHolder();

            holder.text_name = view.findViewById(R.id.text_name);
            holder.text_address = view.findViewById(R.id.text_address);
            holder.text_state = view.findViewById(R.id.text_state);

            view.setTag(holder);

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

        BluetoothDevice bluetoothDevice = list.get(i);

        holder.text_name.setText(bluetoothDevice.getName());
        holder.text_address.setText(bluetoothDevice.getAddress());

        //设置显示是否配对的状态
        int bondState = bluetoothDevice.getBondState();
        switch (bondState){
            case BluetoothDevice.BOND_NONE:
                holder.text_state.setText("未配对");
                break;
            case BluetoothDevice.BOND_BONDING:
                holder.text_state.setText("正在配对");
                break;
            case BluetoothDevice.BOND_BONDED:
                holder.text_state.setText("已经配对");
                break;
        }


        return view;
    }

    //一个蓝牙设备主要的信息:设备的名称,,,设备的地址,,,是否配对一个状态
    private class ViewHolder{
        TextView text_name;
        TextView text_address;
        TextView text_state;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值