我正在开发移动android应用程序,并且我的手机已成功连接到ESP32微控制器。 ESP32的蓝牙范围很大,如果手机与硬件之间的距离/范围超过10米,我想断开手机的蓝牙。我无法修改ESP32的代码来缩小功率范围,所以有什么方法可以缩小蓝牙范围或让手机在超出特定范围时自动断开连接?请找到我的Android Studio
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();
}
}
}
到目前为止我下面的代码:
使用rssi强度和功率值计算
public double getDistance(int measuredPower, double rssi) {
if (rssi >= 0) {
return -1.0;
}
if (measuredPower == 0) {
return -1.0;
}
double ratio = rssi * 1.0 / measuredPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
} else {
double distance= (0.42093) * Math.pow(ratio, 6.9476) + 0.54992;
return distance;
}
}
如何获得功效值?
在ScanResult调用getTxPower(如果您的api> 26中)
注:rssi可靠性不是100%,可能会受到环境因素的影响