[英]How to pass BluetoothGatt and Characteristic to AsyncTask in Android?
I want to use AsyncTask
to implement mGatt.writeCharacteristic
. 我想使用
AsyncTask
来实现mGatt.writeCharacteristic
。
At first , I try to pass mBluetoothGatt
and BluetoothGattCharacteristic
to AsyncTask
like the following code. 首先,我尝试将
mBluetoothGatt
和BluetoothGattCharacteristic
传递给AsyncTask
如下面的代码所示。
private static BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic HueCharacteristic;
new WriteCharacteristic().execute(mBluetoothGatt , HueCharacteristic);
private class WriteCharacteristic extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
BluetoothGatt gatt = params[0];
BluetoothGattCharacteristic characteristic = params[1];
return null;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
}
}
But I don't know how to verify the param at AsyncTask<String, Void, String>
. 但我不知道如何在
AsyncTask<String, Void, String>
验证参数。 I have try AsyncTask<BluetoothGatt, Void, String>
, but it has error. 我尝试了
AsyncTask<BluetoothGatt, Void, String>
,但是它有错误。
How to pass BluetoothGatt
and Characteristic
to AsyncTask
in Android
? 如何在
Android
中将BluetoothGatt
和Characteristic
传递给AsyncTask
?
Write your own constructor in your AsyncTask. 在AsyncTask中编写自己的构造函数。 Like this:
像这样:
private class WriteCharacteristic extends AsyncTask<String, Void,String> {
public WriteCharacteristic(String a, int b) {
//do something with the variables. Save them, whatever
}
@Override
protected String doInBackground(String... urls) {
return null;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
}
}
And you can call it this way: 您可以这样称呼:
new WriteCharacteristic("a",1).execute();
AsyncTasks not too great to use since on config change they go away butt here is what you can do AsyncTasks不是太好用,因为在配置更改它们消失屁股这里是你可以做的
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic HueCharacteristic;
public MyDataObject {
public MyDataObject(BluetoothGatt gatt, BluetoothGattCharacteristic, ch) {
this.gatt = gatt; this.ch = ch;
public BluetoothGatt gatt;
public BluetoothGattCharacteristic ch;
}
new WriteCharacteristic().execute(new MyDataObject(mBluetoothGatt, HueCharacteristic));
private class WriteCharacteristic extends AsyncTask<MyDataObject , Void, String> {
@Override
protected String doInBackground(MyDataObject... data) {
return null;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.