首页 > 移动平台 > 详细

Android之BroadcastReceiver

时间:2014-12-19 12:01:57      阅读:334      评论:0      收藏:0      [点我收藏+]

[代码来源:GOOGLE原生示例BluetoothChat]

[代码功能:实现对安卓蓝牙设备检测到的事件广播的处理]

[代码如下]

  // The BroadcastReceiver that listens for discovered devices and
  // changes the title when discovery is finished
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();

      // When discovery finds a device
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        // Get the BluetoothDevice object from the Intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // If it‘s already paired, skip it, because it‘s been listed already
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
          mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
        // When discovery is finished, change the Activity title
      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        setProgressBarIndeterminateVisibility(false);
        setTitle(R.string.select_device);
        if (mNewDevicesArrayAdapter.getCount() == 0) {
          String noDevices = getResources().getText(R.string.none_found).toString();
          mNewDevicesArrayAdapter.add(noDevices);
        }
      }
    }
  };

 

Android之BroadcastReceiver

原文:http://www.cnblogs.com/jayhust/p/4173508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!