Я работаю в мобильном приложении android, и мой телефон успешно подключается к микроконтроллеру ESP32. Диапазон Bluetooth в ESP32 высок, и я хочу отключить Bluetooth моего телефона, если расстояние / расстояние между телефоном и оборудованием превышает 10 метров. Я не могу изменить код ESP32 для уменьшения диапазона мощности, поэтому есть ли способ уменьшить диапазон bluetooth или автоматически отключить телефон, если он превышает указанный диапазон c? Пожалуйста, найдите мой android код студии ниже, который у меня есть:
public class ConnectedDevice extends AppCompatActivity {
private BluetoothAdapter myBluetooth = null;
private BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
private BluetoothDisconnect bluetoothDisconnect;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connected_device);
new ConnectBT().execute();
btnDis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DisconnectOnBtnClick();
}
});
bluetoothDisconnect = new BluetoothDisconnect();
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(bluetoothDisconnect, intentFilter);
mButtonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStop.setVisibility(View.INVISIBLE);
Intent intent = new Intent(ConnectedDevice.this, PairedDevices.class);
startActivity(intent);
}
});
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(bluetoothDisconnect);
}
private void DisconnectOnBtnClick() {
if (mReadThread != null) {
mReadThread.stop();
do {
} while (mReadThread.isRunning());
mReadThread = null;
}
try {
btSocket.close();
}
catch(IOException e)
{
Toast.makeText(getApplicationContext(), "Could not disconnect", Toast.LENGTH_SHORT).show();
}
finish();
}
private void sendSMS() {
try {
message = brand + " " + model + " " + licensePlate;
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("5089715596",null,message,null,null);
Toast.makeText(this,"Message Sent",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(this, "Could not send message",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private class BluetoothDisconnect extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
sendSMS();
startTimer();
try {
btSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
mButtonStop.setVisibility(View.VISIBLE);
}
@Override
public void onFinish() {
mTimerRunning = false;
mButtonStop.setVisibility(View.INVISIBLE);
Toast.makeText(ConnectedDevice.this, "Calling", Toast.LENGTH_SHORT).show();
callNumber();
}
}.start();
mTimerRunning = true;
mButtonStop.setText("Stop");
}
@SuppressLint("MissingPermission")
private void callNumber()
{
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:5088631994"));
startActivity(phoneIntent);
}
private void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
private class ConnectBT extends AsyncTask<Void, Void, Void> {
private boolean ConnectSuccess = true;
@Override
protected void onPreExecute () {
progress = ProgressDialog.show(ConnectedDevice.this, "Connecting...", "Please Wait!!!");
}
@Override
protected Void doInBackground (Void... devices) {
try {
if ( btSocket==null || !isBtConnected ) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
remoteDevice = myBluetooth.getRemoteDevice(address);
btSocket = remoteDevice.createInsecureRfcommSocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
}
catch (IOException e) {
ConnectSuccess = false;
}
return null;
}
@Override
protected void onPostExecute (Void result) {
super.onPostExecute(result);
if (!ConnectSuccess) {
Toast.makeText(getApplicationContext(), "Connection Failed. Make sure your device is in range", Toast.LENGTH_SHORT).show();
finish();
}
else {
isBtConnected = true;
mReadThread = new ReadInput();
getCurrentData();
}
progress.dismiss();
}
}
}