蓝牙通信的简要设计与开发

蓝牙的两个广播介绍

(1):蓝牙状态的改变是通过广播接收到的。

 // 注册蓝牙状态接收广播
  IntentFilter intentFilter = new  ntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
   registerReceiver(mReceiverBluetoothStatus,intentFilter);

    /**
     * 定义接收蓝牙状态广播receiver
     *
     * @param savedInstanceState
     */
    private BroadcastReceiver mReceiverBluetoothStatus = new BroadcastReceiver() {
   
        @Override
        public void onReceive(Context context,Intent intent) {
   
            int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
            switch (status) {
   
                case BluetoothAdapter.STATE_OFF:
                    Log.d(TAG,"蓝牙已关闭");
                    break;
                case BluetoothAdapter.STATE_ON:
                    Log.d(TAG,"蓝牙已打开");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Log.d(TAG,"蓝牙关闭中...");
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    Log.d(TAG,"蓝牙打开中...");
                    break;
                default:

                    break;
            }
        }
    };

(2):蓝牙搜索到设备、绑定设备(配对)也是通过广播接收的。

   // 注册蓝牙device接收广播
        IntentFilter intentFilterDevice = new IntentFilter();
        // 开始查找
        intentFilterDevice.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        // 结束查找
        intentFilterDevice.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        // 查找设备(查找到设备)
        intentFilterDevice.addAction(BluetoothDevice.ACTION_FOUND);
        // 设备扫描模式改变 (自己状态的改变action,当设置可见或者不见时都会发送此广播)
        intentFilterDevice.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        // 绑定状态
        intentFilterDevice.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(mReceiverDeceiver,intentFilterDevice);
  /**
     * 定义接收蓝牙device广播Receiver
     */
    private BroadcastReceiver mReceiverDeceiver = new BroadcastReceiver() {
   
        @Override
        public void onReceive(Context context,Intent intent) {
   
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
   
                // 开始搜索        ——接收广播
                Log.d(TAG,"开始搜索");
                mList.clear();
                mAdapter.refresh(mList);

            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
   
                // 查找到设备完成   —— 接收广播
                Log.d(TAG,"查找到设备完成");

            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
   
                // 搜索到设备       —— 接收广播
                Log.d(TAG,"搜索到设备");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mList.add(device);
                mAdapter.refresh(mList);


            } else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
   
                // 当自己设备设置蓝牙可见时或者不可见时 —— 接收广播
                int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,0);
                // 可见时
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值