How to calculate distance between two android device using Bluetooth signal strength? -
i'm working on android application. in project want show bluetooth scanning device, mac address, bluetooth signal strength , distance between 2 android device.
i have done 3 requirements don't know how distance using signal strength.
package com.example.bluetoothdemo; import java.util.arraylist; import java.util.list; import java.util.set; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.net.wifi.wifiinfo; import android.net.wifi.wifimanager; import android.os.bundle; import android.app.activity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.content.intent; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; import android.content.broadcastreceiver; public class mainactivity extends activity { private bluetoothadapter btadapter = bluetoothadapter.getdefaultadapter(); private static final int request_enable_bt = 1; listview listdevicesfound; button btnscandevice; textview statebluetooth; bluetoothadapter bluetoothadapter; arrayadapter<string> btarrayadapter; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnscandevice = (button)findviewbyid(r.id.scan_device); statebluetooth = (textview)findviewbyid(r.id.textview1); bluetoothadapter = bluetoothadapter.getdefaultadapter(); listdevicesfound = (listview)findviewbyid(r.id.listview1); btarrayadapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1); listdevicesfound.setadapter(btarrayadapter); checkbluetoothstate(); btnscandevice.setonclicklistener(btnscandeviceonclicklistener); registerreceiver(actionfoundreceiver, new intentfilter(bluetoothdevice.action_found)); registerreceiver(receiver, new intentfilter(bluetoothdevice.action_found)); } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); unregisterreceiver(actionfoundreceiver); } private void checkbluetoothstate(){ if (bluetoothadapter == null){ statebluetooth.settext("bluetooth not support"); }else{ if (bluetoothadapter.isenabled()){ if(bluetoothadapter.isdiscovering()){ statebluetooth.settext("bluetooth in device discovery process."); }else{ statebluetooth.settext("bluetooth enabled."); btnscandevice.setenabled(true); } }else{ statebluetooth.settext("bluetooth not enabled!"); intent enablebtintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enablebtintent, request_enable_bt); } } } private button.onclicklistener btnscandeviceonclicklistener = new button.onclicklistener(){ @override public void onclick(view arg0) { // todo auto-generated method stub btarrayadapter.clear(); bluetoothadapter.startdiscovery(); }}; @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub if(requestcode == request_enable_bt){ checkbluetoothstate(); } } private final broadcastreceiver actionfoundreceiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { // todo auto-generated method stub string action = intent.getaction(); if(bluetoothdevice.action_found.equals(action)) { bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); btarrayadapter.add(device.getname() + "\n" + device.getaddress()+ " \n "+intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value)); btarrayadapter.notifydatasetchanged(); } }}; @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } private final broadcastreceiver receiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if(bluetoothdevice.action_found.equals(action)) { int rssi = intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value); string name = intent.getstringextra(bluetoothdevice.extra_name); textview rssi_msg = (textview) findviewbyid(r.id.listview1); toast.maketext(getapplicationcontext()," rssi: " + rssi + "dbm", toast.length_short).show(); } } }; }
try using "b , l bluetooth le scanner" app google play store, visualize signals several bluetooth le devices. discover rssi signal strengths "noisy" when multiple devices present. walls, furniture metal components, , wi-fi sources cause signal variations. best solution create "zones" distance readings... like: far, close, next-to, etc.
Comments
Post a Comment