android应用程序中蓝牙是否关闭的通知

android应用程序中蓝牙是否关闭的通知,android,bluetooth,Android,Bluetooth,我目前正在开发一个android应用程序。。 每当蓝牙启动时,我必须通知用户 在应用程序运行时关闭设备的 当前正在运行。。 如何通知远程设备 关掉? 提前感谢注册BroadcastReceiver并使用intent action蓝牙适配器。action\u STATE\u已更改,并将您的通知代码移动到onReceive方法。别忘了检查新状态是否已关闭 if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {

我目前正在开发一个android应用程序。。 每当蓝牙启动时,我必须通知用户 在应用程序运行时关闭设备的 当前正在运行。。 如何通知远程设备 关掉?
提前感谢

注册BroadcastReceiver并使用intent action
蓝牙适配器。action\u STATE\u已更改
,并将您的通知代码移动到onReceive方法。别忘了检查新状态是否已关闭

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

如果要检测用户何时断开蓝牙连接,然后检测用户何时断开蓝牙连接,则应执行以下步骤:

1)获取用户蓝牙适配器:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();    
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;
            }

        }
    }
};    
this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));    
2)创建并配置接收器,代码如下:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();    
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;
            }

        }
    }
};    
this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));    
3)将您的广播接收器注册到您的活动中:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();    
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;
            }

        }
    }
};    
this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));    

由于这里没有PM系统,请停止在您编辑的帖子中添加噪音。你在帖子中添加了无用的粗体字,这不是一件好事。您的编辑是受欢迎的,只是没有这种无用和恼人的噪音。