蓝牙的基本使用一

         随互联网技术的发展,网络技术已经应用在我们生活的方方面面,尤其是最近两年最热的智能家居。蓝牙是一种近距离的无线通信技术,通过蓝牙可以控制蓝牙等破的开关、亮度、及颜色。废话不多说,今天我们就来谈一谈android中原生蓝牙的基本使用步骤如下:

         1、获取手机的蓝牙适配器,如果适配器的值为空,则说明手机没有蓝牙。

         2、如果手机又蓝,判断蓝牙适配器是否可用。如果不可用,有两种打开蓝牙的方式(1、跳转到打开蓝牙的界面,让用户手动打开蓝牙;2、直接使用系统的api打开,具体的情况根据项目的需求而定)。

         3、使用适配器调用扫描蓝牙设备的方法扫描设备。

         4、定义一个广播接收者,用于接收扫描到的蓝牙设备。

         5、广播接收器接收到蓝牙设备之后,在蓝牙设备对象中就可以获取到蓝牙设备的一系列属性。

到目前为止,今天的需求总算是完成了:获取扫描到的蓝牙设备名称和地址。代码如下:


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ListView mListView;
    private BluetoothAdapter mBluetoothAdapter;
    private int REQUEST_ENABLE_BT=100;
    private ArrayList<BluetoothDevice> devices=new ArrayList<>();
    private ShowBluetoothDeviceAdapter adapter;

    private BroadcastReceiver mBluetoothReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)){
               BluetoothDevice bluetoothDevice= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                devices.add(bluetoothDevice);
                adapter.notifyDataSetChanged();
                /*显示扫描到的蓝牙设备*/

                Log.i(TAG,"扫描到的蓝牙设备:"+bluetoothDevice.getName());
            }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
                Toast.makeText(context,"开始扫描蓝牙设备",Toast.LENGTH_SHORT).show();
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                Toast.makeText(context,"停止扫描蓝牙设备",Toast.LENGTH_SHORT).show();
            }
        }
    };

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

        /*2、打开蓝牙*/
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter==null){
            //2.1、设备没有蓝牙模块

        }else {
            /*2.2、蓝牙是否可用*/
            if (!mBluetoothAdapter.isEnabled()) {
                /*2.2.1、第一种打开蓝牙的方式,启动一个界面,让用户手动的打开*/
                //Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                //startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

                /*2.2.2、直接使用方法打开*/
                mBluetoothAdapter.enable();
            }
        }

        /*3、扫描蓝牙设备*/
        findDevice();

        /*4、显示扫描到的设备*/
        adapter=new ShowBluetoothDeviceAdapter(devices,getApplicationContext());
        mListView.setAdapter(adapter);


    }

    private void findDevice() {
       /*3.1、开始扫描,扫描的过程耗时的,因此是在子线程中执行的*/
        mBluetoothAdapter.startDiscovery();
         /*停止扫描,同样是在子线程中执行的*/
        //mBluetoothAdapter.cancelDiscovery()

        /*3.2、动态注册广播接收扫描到的蓝牙设备*/
        IntentFilter filter=new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        /*添加提示用户开始扫描和停止扫描*/
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver,filter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /*关闭蓝牙*/
        if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter!=null){
            mBluetoothAdapter.disable();

        }
       unregisterReceiver(mBluetoothReceiver);
    }
}

适配器

public class ShowBluetoothDeviceAdapter extends BaseAdapter {
    private  ArrayList<BluetoothDevice> mDevice;
    private  Context mContext;

    public ShowBluetoothDeviceAdapter(ArrayList<BluetoothDevice> devices, Context context) {
        mDevice=devices;
        mContext=context;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null) {
            convertView = View.inflate(mContext, R.layout.item, null);
            viewHolder = new ViewHolder();
            viewHolder.textViewNM = ((TextView) convertView.findViewById(R.id.deviceName));
            viewHolder.textViewADD = ((TextView) convertView.findViewById(R.id.deviceAddress));
            convertView.setTag(viewHolder);
        }  else {
             viewHolder = (ViewHolder)convertView.getTag();
        }

        BluetoothDevice bluetoothDevice = mDevice.get(position);
        viewHolder.textViewNM.setText("蓝牙设备名称:"+bluetoothDevice.getName());
        viewHolder.textViewADD.setText(","+"蓝牙设备地址:"+bluetoothDevice.getAddress());
        return convertView;
    }
    class ViewHolder{
        TextView textViewNM;
        TextView textViewADD;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值