如何在 Android 中将 BluetoothGatt 和 Characteristic 传递给 AsyncTask?
How to pass BluetoothGatt and Characteristic to AsyncTask in Android?
我想用AsyncTask
实现mGatt.writeCharacteristic
。
起初,我尝试将 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) {
}
}
但是我不知道如何验证 AsyncTask<String, Void, String>
的参数。我试过 AsyncTask<BluetoothGatt, Void, String>
,但它有错误。
如何在Android
中将BluetoothGatt
和Characteristic
传递给AsyncTask
?
在您的 AsyncTask 中编写您自己的构造函数。像这样:
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) {
}
}
你可以这样称呼它:
new WriteCharacteristic("a",1).execute();
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) {
}
}
我想用AsyncTask
实现mGatt.writeCharacteristic
。
起初,我尝试将 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) {
}
}
但是我不知道如何验证 AsyncTask<String, Void, String>
的参数。我试过 AsyncTask<BluetoothGatt, Void, String>
,但它有错误。
如何在Android
中将BluetoothGatt
和Characteristic
传递给AsyncTask
?
在您的 AsyncTask 中编写您自己的构造函数。像这样:
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) {
}
}
你可以这样称呼它:
new WriteCharacteristic("a",1).execute();
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) {
}
}