我需要检查异步任务中的doInBackground方法中是否已连接任何设备并通过蓝牙与我的设备通信。如果已连接/正在通信-将吐司消息显示为“已连接”。如果未显示为“未连接设备”。
我到处搜索,但没有答案满足我的要求。
答案 0 :(得分:0)
我不详细了解您的问题,但是我的解决方案将是用于蓝牙适配器的BroadhastReceiver。
public Integer getPlayerBits(Player p) {
String queryStr = "SELECT Coins FROM PlayerData WHERE PlayerUID=?";
try {
Connection conn = getConn();
PreparedStatement st = conn.prepareStatement(queryStr);
st.setString(1, p.getUniqueId().toString());
ResultSet rs = st.executeQuery();
rs.next();
Integer amount = (Integer) rs.getInt("Coins");
conn.close();
return amount;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void addBits(Player p, int bitAmount) {
String queryStr = "UPDATE PlayerData SET Coins = Coins + ? WHERE PlayerUID=?";
try {
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement(queryStr);
ps.setInt(1, bitAmount);
ps.setString(2, p.getUniqueId().toString());
ps.executeUpdate();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void removeBits(Player p, int bitAmount) {
String queryStr = "UPDATE PlayerData SET Coins = Coins - ? WHERE PlayerUID=?";
try {
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement(queryStr);
ps.setInt(1, bitAmount);
ps.setString(2, p.getUniqueId().toString());
ps.executeUpdate();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
如果您告诉我您想确切执行的操作,例如: 根据状态运行一些代码, 执行一些TextView更改, 或诸如回调方法之类的东西,
或其他什么,我可以尝试为您提供更多帮助。
我现在要做的是创建一些静态函数,您应该能够在AsynkTask中调用它们,只需调用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);
}
}
,您就会返回BluetoothAdapter的当前状态。
答案 1 :(得分:0)
你在说话吗?是的,然后创建一个回调
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;
}
}