2011-03-22 101 views
12

即时通讯目前正在对一个Android应用程序.. 我有,而应用程序是 当前运行.. 如何通知每当设备的蓝牙 关闭用户要通知远程设备达到BT是否关闭了 ? 在此先感谢通知,如果蓝牙在Android应用程序关闭

+0

题外话评论,因为这里没有PM系统:请停止增加噪声的职位编辑。你在文章中添加了无用的粗体字,这不是一件好事。欢迎您的编辑,只是没有这种无用和刺激的噪音。 – 2011-07-18 12:04:41

回答

20

注册广播接收器意图动作BluetoothAdapter.ACTION_STATE_CHANGED和移动你的notifiyng代码到onReceive方法。不要忘记检查,如果新的状态为OFF

if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { 
    if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
     == BluetoothAdapter.STATE_OFF) 
     // Bluetooth was disconnected 
} 
+0

非常感谢...它的工作很好... – Hussain 2011-03-22 09:16:34

7

如果你想在用户连接断开了蓝牙,后来检测,检测用户何时有自己的蓝牙断开连接,你应该做以下步骤:

1)获取用户BluetoothAdapter:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();  

2)创建和配置接收器,一个代码为这样:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 

     // It means the user has changed his bluetooth state. 
     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 

      if (btAdapter.getState() == BluetoothAdapter.STATE_TURNING_OFF) { 
       // The user bluetooth is turning off yet, but it is not disabled yet. 
       return; 
      } 

      if (btAdapter.getState() == BluetoothAdapter.STATE_OFF) { 
       // The user bluetooth is already disabled. 
       return; 
      } 

     } 
    } 
};  

3)注册您的广播接收器到你的活动:

this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));  
相关问题