Reputation: 199
I need to check whether any device is connected and communicating to my device via bluetooth in doInBackground method in Async Task. 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.
Upvotes: 1
Views: 211
Reputation: 336
I do not know your problem in detail, but my Solution would be the 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);
}
}
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,
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.
Upvotes: 1
Reputation: 204
are you talking abou ble 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;
}
}
Upvotes: 0