如何编程的识别蓝牙是否已经连接了

  1. public void onCreate() {
  2.     ...
  3.     IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
  4.     IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
  5.     IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
  6.     this.registerReceiver(mReceiver, filter1);
  7.     this.registerReceiver(mReceiver, filter2);
  8.     this.registerReceiver(mReceiver, filter3);
  9. }

  10. //The BroadcastReceiver that listens for bluetooth broadcasts
  11. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  12.     @Override
  13.     public void onReceive(Context context, Intent intent) {
  14.         String action = intent.getAction();
  15.         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

  16.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  17.            ... //Device found
  18.         }
  19.         else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
  20.            ... //Device is now connected
  21.         }
  22.         else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
  23.            ... //Done searching
  24.         }
  25.         else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
  26.            ... //Device is about to disconnect
  27.         }
  28.         else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
  29.            ... //Device has disconnected
  30.         }           
  31.     }
  32. };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值