简体   繁体   English

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

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

I need to check whether any device is connected and communicating to my device via bluetooth in doInBackground method in Async Task.我需要在异步任务的 doInBackground 方法中检查是否有任何设备已连接并通过蓝牙与我的设备通信。 If connected/communicating - display toast message as "Connected".如果已连接/正在通信 - 将吐司消息显示为“已连接”。 if not display as "No device connected".如果未显示为“未连接设备”。

I've searched everywhere, but no answers satisfied my requirements.我到处搜索,但没有答案满足我的要求。

I do not know your problem in detail, but my Solution would be the BroadhastReceiver for Bluetooth Adapter.我不详细了解您的问题,但我的解决方案是蓝牙适配器的 BroadhastReceiver。

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);
    }
}

If you tell me what you want to do exactly, for example: Run Some Code depending on state, Execute some TextView Changes, Or something like a callback Method,如果你告诉我你想要做什么,例如:根据状态运行一些代码,执行一些 TextView 更改,或者类似回调方法,

or what else, I can try to help you even more.或者其他什么,我可以试着帮助你更多。

What I did now is creating some static functions you should be able to call in your AsynkTask, just call AnyActivity.isConnected() and you get the current state of the BluetoothAdapter returned.我现在所做的是创建一些您应该能够在 AsynkTask 中调用的静态函数,只需调用AnyActivity.isConnected()获得返回的 BluetoothAdapter 的当前状态。

are you talking abou ble gatt?你在谈论关于 gatt 吗? it yes then create a callback它是然后创建一个回调

    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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM