BlueTooth


public class MainActivity extends AppCompatActivity {


    private Button btnScan;
    private ListView listView;
    private BluetoothAdapter bluetoothAdapter;
    //用于显示列表的集合
    private List<BluetoothDevice>  lists=new ArrayList<>();
    private MyReceiver receiver;
    private MyAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnScan = (Button) findViewById(R.id.btn_scan);
        listView = (ListView) findViewById(R.id.lv);


        //动态注册广播接收器
        receiver = new MyReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);//发现蓝牙设备
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//蓝牙状态改变
        registerReceiver(receiver,filter);



        //1.获取蓝牙适配器对象
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //2.判断此时的蓝牙设备是否可用
        if(bluetoothAdapter !=null){//手机是否有蓝牙功能
            if(!bluetoothAdapter.isEnabled()){//蓝牙不可用时
                bluetoothAdapter.enable();//设置蓝牙可用
//                bluetoothAdapter.disable();//设置蓝牙不可用


            }
        }


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //得到选中的设备
                BluetoothDevice device=lists.get(position);
                //判断它的状态
               if(device.getBondState()==BluetoothDevice.BOND_NONE){//未绑定的状态
                   //通过mac地址,得到要配对的设备
                   BluetoothDevice remote=bluetoothAdapter.getRemoteDevice(device.getAddress());
                   try {
                       //进行连接-得用反射技术
                       Method method=BluetoothDevice.class.getMethod("createBond",null);
                       method.invoke(remote,null);


                       //进行连接后,设备的状态会改变,发送一个广播,需要到广播接收器中去处理
                   } catch (NoSuchMethodException e) {
                       e.printStackTrace();
                   } catch (InvocationTargetException e) {
                       e.printStackTrace();
                   } catch (IllegalAccessException e) {
                       e.printStackTrace();
                   }


               }
            }
        });


        btnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.获取已经配对好的设备
               Set<BluetoothDevice> bondedDevices=bluetoothAdapter.getBondedDevices();
                for(BluetoothDevice device:bondedDevices){
                    lists.add(device);
                }
                //2.进行扫描(未配对的设备)
                if(bluetoothAdapter.isDiscovering()){//判断现在是否正在进行扫描
                    bluetoothAdapter.cancelDiscovery();//取消本次扫描
                }
                //返回的值是布尔类型-判断是否正确启动了扫描
                boolean result=bluetoothAdapter.startDiscovery();
                Log.d("zzz","正在扫描:"+result);
                //扫描到一个设备后,会发送一个广播,需要注册广播接收器



            }
        });
    }


    public void setAdapter(){
        if(adapter!=null){
            adapter=new MyAdapter(lists,this);
            listView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
        unregisterReceiver(receiver);
    }


    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //将扫描到的设备添加到lists集合中
            if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction())){
                //得到设备 在intent对象中
                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if(!lists.contains(device)){
                    lists.add(device);


                    //设置适配器
                    setAdapter();


                }
            }
            if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())){
                //判断设备的状态,如果已经配对成功,则刷新适配器
                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int state=device.getBondState();
                switch (state){
                    case BluetoothDevice.BOND_BONDED:
                        Toast.makeText(MainActivity.this,"已经配对",Toast.LENGTH_SHORT).show();
                        lists.remove(device);
                        lists.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;
                }


            }


        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值