如何使用异步任务以编程方式检查蓝牙是否已与其他设备连接?

How to check whether bluetooth has connected with other device programatically using Async Task?

我需要在 Async Task 的 doInBackground 方法中检查是否有任何设备已连接并通过蓝牙与我的设备通信。如果 connected/communicating - 将 toast 消息显示为 "Connected"。如果不显示为 "No device connected".

我到处找,都没有满足我要求的答案。

我不知道你的问题的细节,但我的解决方案是 BroadhastReceiver for Bluetooth Adapter。

public class AnyActivity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static boolean isConnected = false;
private static boolean isConnecting = false;
private static boolean isDisdconected = false;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_any);

        IntentFilter BT_ConState_filter  = new 
        IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);

        this.registerReceiver(BR_BT_ConState, BT_ConState_filter);
}

BroadcastReceiver BR_BT_ConState =new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action_BR_BT_ConState =intent.getAction();
            int iBR_BT_ConState_state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, mBluetoothAdapter.ERROR);

            if (action_BR_BT_ConState.equals(mBluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED))
            {
                switch (iBR_BT_ConState_state) {
                    case BluetoothAdapter.STATE_CONNECTING:
                        Toast.makeText(this, "Connecting" ,Toast.LENGTH_SHORT).show();
                        isConnected = false;
                        isConnecting = true;
                        isDisdconected = false;
                        break;

                    case BluetoothAdapter.STATE_CONNECTED:
                        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
                        isConnected = true;
                        isConnecting = false;
                        isDisdconected = false;
                        break;

                    case BluetoothAdapter.STATE_DISCONNECTED:
                        Toast.makeText(this, "Disconected", Toast.LENGTH_SHORT).show();
                        isConnected = false;
                        isConnecting = false;
                        isDisdconected = true;
                        break;
                }
            }
        }
    };

public static boolean isConnected(){
return isConnected;
}

public static boolean isDisConnected(){
return isDisConnected;
}

public static boolean isConnecting(){
return isDisConnected;
}

@Override
    protected void onDestroy(){
        super.onDestroy();
        context.unregisterReceiver(BR_BT_ConState);
    }
}

如果你确切地告诉我你想做什么,例如: 运行 一些代码取决于状态, 执行一些 TextView 更改, 或者类似回调方法的东西,

或者其他什么,我可以尝试为您提供更多帮助。

我现在所做的是创建一些您应该能够在 AsynkTask 中调用的静态函数,只需调用 AnyActivity.isConnected() 即可返回 BluetoothAdapter 的当前状态。

你在谈论关贸总协定吗?是的然后创建一个回调

    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        switch (newState) {
            case BluetoothProfile.STATE_DISCONNECTED:

                break;

            case BluetoothProfile.STATE_CONNECTING:

                break;

            case BluetoothProfile.STATE_CONNECTED:

                break;
        }
    }