提问人:Javed Quantum 提问时间:11/16/2023 最后编辑:Javed Quantum 更新时间:11/16/2023 访问量:24
当从 Android 应用程序单击按钮时,我的特征写入不向 esp32 模块发送数据
when click on button from Android Application my characteristics write not send data to esp32 module
问:
public class MainScreen extends AppCompatActivity implements Serializable, Observer {
BluetoothAdapter mBluetoothAdapter;
public static final String Tag="DetailBle";
boolean MAINSSTATUS;
boolean SOLARSTATUS;
// boolean UPSSTATUS;
private static boolean UPSSTATUS;
boolean BATTERYFULL;
String CHARGING_STATUS,CHARGING_LABEL;
View view;
LinearLayout statusLayout;
int psckecked;
Alarms alarms;
List<BluetoothGattCharacteristic> characteristicsList = new ArrayList<>();
List<BluetoothGattService> servicesList;
BleDevice bleDevice;
BluetoothGattService service;
List<StringBuilder> builderList;
// here we define our variable connected to xml file
LinearLayout solarArrowLoad, solarArrowBattery, gridArrowLoad, gridArrowBattery,batteryArrowLoad;
TextView solarToloadP,solarToBatteryCur;
//gridToLoadP,gridToBatteryCu; ye comment kiye hai
// todayPS totalps,solarPower ye bhi comment kiya hai
TextView solarCurrent,batteryVolt,batteryCurrent,SRnumber, gridVolt,loadPersent;
TextView connetionStatus;
Button upsStatus,upsMode,highCharging,powerSaving; // btn to txt here error
ImageView solarPanel,battery,homeLoad,supply;
//
//
int writeFlag;
ClickDialog clickDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_ble);
ObserverManager.Companion.getInstance().addObserver(this);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
BleDevice bleDevice1 = getIntent().getParcelableExtra("bleDevice"); //BLE class object
// bleDevice1.getName().toString();
if (bleDevice1 != null){
Log.d("","javed"+bleDevice1.getName());
start(bleDevice1);
}
// D4:D4:DA:30:8D:62
else {
Toast.makeText(this, "yaha dikaat hai ky bhai", Toast.LENGTH_SHORT).show();
}
}
void start(BleDevice bleDevice1){
bleDevice=bleDevice1;
Log.d("","oldhai"+bleDevice.getName());
if (bleDevice!=null){
if (BleManager.getInstance().isConnected(bleDevice)){
initView();
setData();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//notifyClick();
write("hello sir"); //1
Log.d("Tag","msg"+write("i am coder"));
Log.d("Tag","msg"+write("i am coder"));
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
indicate();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
setMTU();
}
else
Toast.makeText(this, "Device is not connected", Toast.LENGTH_SHORT).show();
Log.d(Tag+"onCreate","Device id not connected");
}
setConnectivityStatus();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
//start handler as activity become visible
// startCkeckStatus();
super.onResume();
}
private void setConnectivityStatus() {
if (getConnectivityStatus()) {
connetionStatus.setText(getText(R.string.connected));
Log.d(Tag,"Connected");
}
else {
connetionStatus.setText(R.string.active_disconnected);
Log.d(Tag,"Disconnected");
}
}
private boolean getConnectivityStatus() {
return BleManager.getInstance().isConnected(bleDevice);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (bleDevice!=null){
BleManager.getInstance().disconnect(bleDevice);
BleManager.getInstance().destroy();}
stopNotification();
}
private void initView() {
servicesList = new ArrayList<>();
builderList= new ArrayList<>();
connetionStatus=findViewById(R.id.connected_status);
solarToloadP=findViewById(R.id.solar_to_load_power);
solarToBatteryCur=findViewById(R.id.solar_to_battery_current);
// 208 and 209 and 214 ,2013.1113,1117,1118,1198,1120 these line will be connected
// gridToLoadP=findViewById(R.id.grid_to_load_power);
// gridToBatteryCu=findViewById(R.id.grid_to_battery_current);
// todayPS=findViewById(R.id.today_power_saved);
// totalPS=findViewById(R.id.total_power_saved);
// solarPower=findViewById(R.id.solar_power);
// solarCurrent=findViewById(R.id.solar_current);
SRnumber = findViewById(R.id.serialNO);
gridVolt =findViewById(R.id.ip_volt);
loadPersent=findViewById(R.id.load_presentge);
batteryVolt=findViewById(R.id.battery_volt);
batteryCurrent=findViewById(R.id.battrery_to_load_power);
upsStatus=findViewById(R.id.ups_status);
upsMode=findViewById(R.id.ups_mode);
highCharging=findViewById(R.id.high_charging);
powerSaving=findViewById(R.id.power_saving);
solarPanel=findViewById(R.id.solar_panel);
supply=findViewById(R.id.supply);
battery=findViewById(R.id.battery);
homeLoad=findViewById(R.id.load);
gridArrowLoad =findViewById(R.id.grid_arroweup);
gridArrowBattery =findViewById(R.id.grid_arrowdown);
solarArrowLoad =findViewById(R.id.solar_arrow_up);
solarArrowBattery =findViewById(R.id.solar_arrow_mid);
batteryArrowLoad =findViewById(R.id.battery_to_load);
}
private void setData() {
BluetoothGatt gatt = BleManager.getInstance().getBluetoothGatt(bleDevice);
alarms=new Alarms(MainScreen.this);
clickDialog = new ClickDialog(MainScreen.this);
view =getWindow().getDecorView().findViewById(android.R.id.content);
for (BluetoothGattService service : gatt.getServices()) {
servicesList.add(service);
}
service = servicesList.get(2); // 3-1 services chl rhe hai
// Log.d("","i am ser"+service);
//// Log.d("","service1"+service.getCharacteristics().get(1));
// Log.d("","service1"+servicesList.get(1));
// Log.d("","service0"+servicesList.get(0));
// Log.d("","service2"+servicesList.get(2));
characteristicsList.addAll(service.getCharacteristics());
/* ************************************* Button ******************************************* */
// upsStatus.setOnClickListener(view -> {
// Toast.makeText(MainScreen.this, "data is coming",
// Toast.LENGTH_SHORT).show();
// if (!builderList.isEmpty()) {
// if (upsStatus.getText() == getString(R.string.ups_on)) {
// Toast.makeText(MainScreen.this, "data is coming",
// Toast.LENGTH_SHORT).show();
// // String input = builderList.get(1).toString() + "%" + getString(R.string.ups_off) + "%" + "END";
// String input = builderList.get(7).toString() + "%" + "OFF"+ "%" + "END";
// Log.d("","buton"+ input);
// int status = write(input);
// if (status == 4)
// upsStatus.setText(getString(R.string.ups_off));
//
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
//
// } else {
// // String input = builderList.get(1).toString() + "%" + getString(R.string.ups_on) + "%" + "END";
// String input = builderList.get(7).toString() + "%" + " ON"+ "%" + "END";
// int status = write(input);
// Log.d("YourTag", "write method executed. Status: " + status);
// if (status == 1)
// upsStatus.setText(getString(R.string.ups_on));
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
// }
// }
//
// });
// button hai yha p
// upsStatus.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View view) {
//
//
// if (!builderList.isEmpty()) {
// if (upsStatus.getText() == getString(R.string.ups_on)) {
//
// // String input = mArrayList.get(1).toString() + "%" + getString(R.string.ups_off) + "%" + "END";
// String input = builderList.get(1).toString() ;
// Log.d("","input"+input.toString());
// Log.d("","upsss"+builderList.get(10).toString());
// int status = write(input);
// Log.d("","input"+status);
// if (status == 1)
// upsStatus.setText(getString(R.string.ups_off));
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
// } else {
// // String input = mArrayList.get(1).toString() + "%" + getString(R.string.ups_on) + "%" + "END";
// String input = builderList.get(1).toString() + "%" + " ON" + "%" + "END";
// int status = write(input);
// if (status == 1)
// upsStatus.setText(getString(R.string.ups_on));
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
// }
// }
//
// }
// });
// upsStatus.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View view) {
//
// try {
// if (!builderList.isEmpty()) {
// Log.d("", "Builder list size: " + builderList.size());
// Toast.makeText(MainScreen.this, "data is coming",
// Toast.LENGTH_SHORT).show();
// Log.d("","mybuil"+builderList.get(1));
// if (upsStatus.getText().equals(getString(R.string.ups_on))) {
// String input = builderList.get(1).toString();
// int status = write(input);
// if (status == 1)
// upsStatus.setText(getString(R.string.ups_off));
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
// } else {
// String input = builderList.get(1).toString() + "%" + " ON" + "%" + "END";
// int status = write(input);
// if (status == 1)
// upsStatus.setText(getString(R.string.ups_on));
// else
// Toast.makeText(MainScreen.this, "Operation Fail try again",
// Toast.LENGTH_SHORT).show();
// }
// }
// } catch (Exception e) {
// // Handle exceptions here
// e.printStackTrace(); // This line prints the exception's stack trace for debugging
// // You can display an error message or handle the exception in a way suitable for your application
// Toast.makeText(MainScreen.this, "An error occurred: " + e.getMessage(),
// Toast.LENGTH_SHORT).show();
// }
// }
// });
upsStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!builderList.isEmpty()) {
if (upsStatus.getText() == getString(R.string.ups_on)) {
// String input = mArrayList.get(1).toString() + "%" + getString(R.string.ups_off) + "%" + "END";
String input = builderList.get(1).toString() + "%" + "OFF" + "%" + "END";
Log.d("","upsss"+builderList.subList(1,10));
int status = write(input);
if (status == 1)
upsStatus.setText(getString(R.string.ups_off));
else
Toast.makeText(MainScreen.this, "Operation Fail try again",
Toast.LENGTH_SHORT).show();
} else {
// String input = mArrayList.get(1).toString() + "%" + getString(R.string.ups_on) + "%" + "END";
String input = builderList.get(1).toString() + "%" + " ON" + "%" + "END";
int status = write(input);
if (status == 1)
upsStatus.setText(getString(R.string.ups_on));
else
Toast.makeText(MainScreen.this, "Operation Fail try again",
Toast.LENGTH_SHORT).show();
}
}
}
});
我有一个 Android 应用程序,其用户界面包含一个按钮。单击按钮后,我会对与 ESP32 服务器关联的特定 BLE 特性启动写入操作以传输数据。然而,尽管实现了将数据写入特性的代码,但数据并未成功传输到 ESP32。 我从服务器正确接收的数据如下。 UPS%12345678%223%012.8%000%223%00.0%04%1%001%13.5%00.0%00.3%00.0%00.4%000.0%000.0%END 现在我必须将这些数据从 Android 应用程序发送到 esp32。 字符串输入 = builderList.get(1).toString() + “%” + “ ON” + “%” + “END”;
答: 暂无答案
评论