Android code to alert when a specific bluetooth device is on - android

I want to know how make a Bluetooth program with android, witch the main functionality is alert me when a specific Bluetooth device is on (by introducing on code for example the name of that device).
E.g. If my cell phone Bluetooth is on i want to be alert in my android program in another cell phone for example.
I try some programs but they gave me all the same, only discover new devices or connect to them.
Thanks

mSpecificDevice is the specific device you want to check.
MainActivity.java:
public class MainActivity extends Activity {
// Return Intent extra
public static String EXTRA_DEVICE_ADDRESS = "device_address";
private String mSpecificDevice = "my-device";
// Member fields
private BluetoothAdapter mBtAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set result CANCELED in case the user backs out
setResult(Activity.RESULT_CANCELED);
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void doDiscovery(View view) {
doDiscovery();
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onDestroy() {
super.onDestroy();
// Make sure we're not doing discovery anymore
if (mBtAdapter != null) {
mBtAdapter.cancelDiscovery();
}
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}
/**
* Start device discover with the BluetoothAdapter
*/
private void doDiscovery() {
// If we're already discovering, stop it
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (mSpecificDevice.equals(device.getName())) {
Toast.makeText(getApplicationContext(), device.getName() + " is on", Toast.LENGTH_LONG).show();
}
}
}
};
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:onClick="doDiscovery"/>
Don't forget to add the following to AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

instead of button calling doDiscovery you can use threading which call this methaod after every some seconds

Related

Detect Enable/Disable Bluetooth

I made an application to turn on/off bluetooth. To turn on/off i used a switch button. Here is the code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = null;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch (state){
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_ON:
break;
}
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null){
//Device does not support bluetooth
}
else{
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
}
});
}
}
}
The problem is that if i turn on/off bluetooth from settings and not from my app the switch doesn't change.
I have implemented a broadcast receiver but i can't access the switch from it.
I tried:
bluetoothSwich.setChecked(true)
inside the broadcast receiver but it doesn't work.
Ty
EDIT :
I partially solved the problem with the global switch but to catch the on/off action from settings first i have to click the on/off button from my app at least one time. any suggestion?
To detect the state change of bluetooth you need to add following permission to your AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH" />
Use a Local broadcast preferably. You do not need to register it in Manifest . register it at runtime where you need it.(If need throughout the app then register it in Manifest)
private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
// Bluetooth is disconnected, do handling here
}
}
}
};
Runtime register:
LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
Runtime unregister : Don't forget to unregister the broadcast.
LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);
Static Register:
<receiver
android:name=".FullyQualifiedBroadcastReceiverClassName"
android:enabled="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>

How to create a button that starts WiFi direct

I have no prior experience with app development and am now supposed to create an app in android studio that connects to a sensor by WiFi-direct. I have read up on WiFi-direct and tried to follow the guides https://developer.android.com/guide/topics/connectivity/wifip2p.html and https://stuff.mit.edu/afs/sipb/project/android/docs/training/connect-devices-wirelessly/wifi-direct.html but when using their code I have still no clue how to move forward. I have also looked at several demo apps for WiFi direct.
My code looks like this at the moment.
Broadcast reciever:
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
MainActivity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
mActivity.setIsWifiP2pEnabled(true);
} else {
mActivity.setIsWifiP2pEnabled(false);
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
private boolean isWifiP2pEnabled = false;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
}
/* register the broadcast receiver with the intent values to be matched */
#Override
protected void onResume() {
super.onResume();
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
The next part in the guide is that I should try to discover peers. Where should I implement this code?
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}
#Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
}
});
My final product should be a button that when I press it I should search for peers and be able to connect to one. The sensor will boot in WiFi direct. Does anyone know where I can find more info on how to proceed or have tips on what classes I need and such?
Thank you!
The way it works is by:
Calling discoverPeers when tapping on the button
Next, in your WiFiDirectBroadcastReceiver -> WIFI_P2P_PEERS_CHANGED_ACTION condition, request the list of available peers like this: mManager.requestPeers(mChannel, peerListListener);
Then, you have to implement WifiP2pManager.PeerListListener in your activity or fragment because the discovered peers will return into this method:
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {}
Once you have the list of peers, you can connect to any peer using
mManager.connect(mChannel, config, new ActionListener() {});
I believe following https://developer.android.com/guide/topics/connectivity/wifip2p.html is good enough to get the api working.
Goodluck.

Not able to scan bluetooth devices

I am developing an app which scans the bluetooth devices and list them all. My requirement is that I have to scan for devices as soon as application starts..Now my problem is when i run the app for the first time it only turns on the bluetooth but does not scan for devices..
I can see the bluetooth icon on screen but in log cat getState() method of the BluetoothAdapter shows state as STATE_OFF.
Please anyone help me about this issue ??
Here is my code snippet
public class MainActivity extends Activity {
private BluetoothAdapter bluetoothAdapter;
Set<String> BTList;
ArrayAdapter<String> BTAdapter;
private ListView listView;
private BroadcastReceiver mReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
listView = (ListView) findViewById(R.id.listView1);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null)
Toast.makeText(this, "Devices does not support Bluetooth",
Toast.LENGTH_SHORT).show();
if (!bluetoothAdapter.isEnabled())
bluetoothAdapter.enable();
if(bluetoothAdapter.isEnabled()) {
if(bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
}
bluetoothAdapter.startDiscovery();
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BTAdapter.add(device.getName() + "\n" + device.getAddress());
BTAdapter.notifyDataSetChanged();
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
BTAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1);
listView.setAdapter(BTAdapter);
}
#Override
protected void onDestroy() {
super.onDestroy();
//if(bluetoothAdapter != null)
// bluetoothAdapter.cancelDiscovery();
unregisterReceiver(mReceiver);
}
}
Check in your AndroidManifest.xml file, if you have these below entries
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

Android BLE Scan in Background Service

I'm trying to make an Android app that will scan for a certain Bluetooth device as a background service. Once the phone is within a certain range of the Bluetooth device, measured by reading the RSSI, the background service will start an Activity displayed to the user. If the phone is moved out of range of the Bluetooth device, (after the RSSI value is beyond a certain threshold,) the Activity should be killed.
Here is the code for the background service:
public class BeaconScanService extends Service implements BluetoothAdapter.LeScanCallback {
private Service self = this;
public static final String UNLOCK = "unlock";
public static final String STATUS = "status";
public static final String SIGNAL = "signal";
public static final String GET_SIGNAL = "get_signal";
//desired device to find
private static final String targetMAC = "E1:BE:A8:1A:8B:A0";
//class to handle saving RSSI values and detecting Bluetooth proximity
private proxDetector proxy;
//boolean to determine if phone is in BT range
static boolean inProx = false;
private BluetoothAdapter mBluetoothAdapter;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Proximity Detector
proxy = new proxDetector();
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
/*
* We need to enforce that Bluetooth is first enabled, and take the
* user to settings to enable it if they have not done so.
*/
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//Bluetooth is disabled
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(enableBtIntent);
}
/*
* Check for Bluetooth LE Support. In production, our manifest entry will keep this
* from installing on these devices, but this will allow test devices or other
* sideloads to report whether or not the feature exists.
*/
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "No LE Support.", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
//Begin scanning for LE devices
startScan();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
//Cancel any scans in progress
mHandler.removeCallbacks(mStopRunnable);
mHandler.removeCallbacks(mStartRunnable);
mBluetoothAdapter.stopLeScan(this);
}
private Runnable mStopRunnable = new Runnable() {
#Override
public void run() {
stopScan();
}
};
private Runnable mStartRunnable = new Runnable() {
#Override
public void run() {
startScan();
}
};
private void startScan() {
Toast.makeText(this, "Scanning", Toast.LENGTH_SHORT).show();
//Scan for Bluetooth device with specified MAC
mBluetoothAdapter.startLeScan(this);
mHandler.postDelayed(mStopRunnable, 5000);
}
private void stopScan() {
Toast.makeText(this, "Not Scanning", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.stopLeScan(this);
mHandler.postDelayed(mStartRunnable, 2500);
}
/* BluetoothAdapter.LeScanCallback */
#Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
/*
* Create a new beacon and pass it up to the main thread
*/
Toast.makeText(self, "OnLeScan", Toast.LENGTH_SHORT).show();
BT_Beacon beacon = new BT_Beacon(device.getAddress(), rssi);
mHandler.sendMessage(Message.obtain(null, 0, beacon));
}
/*
* We have a Handler to process scan results on the main thread
*/
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
Toast.makeText(self, "Handlering", Toast.LENGTH_SHORT).show();
BT_Beacon beacon = (BT_Beacon) msg.obj;
if(beacon.getAddress().equals(targetMAC)){//Only look for target device
Toast.makeText(self, String.format("%ddBm", beacon.getSignal()), Toast.LENGTH_SHORT).show();
//HANDLE PROXIMITY DETECTION
Intent i1 = new Intent(UNLOCK);
proxy.processProx(beacon.getSignal());
Intent i2 = new Intent(SIGNAL);
i2.putExtra(GET_SIGNAL, proxy.prox);
sendBroadcast(i2);
if(proxy.crossedLine()){
i1.putExtra(STATUS, 1);
sendBroadcast(i1);
inProx = true;
Intent i = new Intent(self,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}else{
i1.putExtra(STATUS, 0);
sendBroadcast(i1);
inProx = false;
}
}
}
};
}
Ultimately I want this background service to be started on boot, but for testing purposes, I am able to create or destroy this service from an Activity that should also display the RSSI value:
public class MainActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnScan = (Button) findViewById(R.id.btnScan);
Button btnUnscan = (Button) findViewById(R.id.btnUnscan);
btnScan.setOnClickListener(this);
btnUnscan.setOnClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, new IntentFilter(BeaconScanService.SIGNAL));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnScan:
Intent i = new Intent(this,BeaconScanService.class);
startService(i);
break;
case R.id.btnUnscan:
Intent i = new Intent(this,BeaconScanService.class);
stopService(i);
break;
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
int sig = bundle.getInt(BeaconScanService.GET_SIGNAL);
TextView rssiView = (TextView) findViewById(R.id.text_rssi);
rssiView.setText(String.format("%ddBm", sig));
}
}
};
}
This is the layout for for MainActivity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"/>
<Button
android:id="#+id/btnUnscan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Scan"
android:layout_below="#+id/btnScan"/>
<TextView
android:id="#+id/text_rssi"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceListItem"
android:text="dBm"
android:layout_below="#+id/btnUnscan"/>
</RelativeLayout>
in the activity that is started and ended by the BeaconScanService, the BroadcastReceiver is registered like this:
#Override
protected void onResume() {
super.onResume();
registerReceiver(beaconScanReceiver, new IntentFilter(BeaconScanService.UNLOCK));
}
and the BroadcastReceiver looks like this:
private final BroadcastReceiver beaconScanReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
int status = bundle.getInt(BeaconScanService.STATUS);
if(status == 0) {
finish();
}
}
}
};
When I run this code, I can start and stop the BeaconScanService fine, and I know the service is scanning for the Bluetooth device due to debug Toasts being displayed. As far as I can tell, the code breaks down somewhere in the Handler of the service. No RSSI value is displayed in the TextView, and MyActivity is not started when the phone is brought close enough to the Bluetooth device.
I'm not too familiar with the interprocess communication aspects of Android programming, so I'm probably doing something wrong there. Any thoughts?

Android Getting continuously Bluetooth signal strength of paired devices

i am listing all paired devices,and it well work but now want to get Bluetooth signal strength of paired devices...i know it will get by use rssi but cannot implement get it continuously in my app..
plz me by giving suitable code as my code...my code is here...
public class Security extends Fragment implements OnClickListener{
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ArrayList<String> mylist = new ArrayList<String>();
//private Object ImageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.security, null);
BA = BluetoothAdapter.getDefaultAdapter();
/* starting the bluetooth*/
on(v);
pairedDevices = BA.getBondedDevices();
//length=4;
// int j=1;
for(BluetoothDevice bt : pairedDevices) {
mylist.add(bt.getName());
length=j;
j++;
bt.getBondState();
}
return v;
}
#Override
public void onResume() {
super.onResume();
// Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show();
}
/*************************Bluetooth function****************************/
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getActivity(),"Turned on"
,Toast.LENGTH_LONG).show();
} else{
// Toast.makeText(getActivity(),"Already on",
// Toast.LENGTH_LONG).show();
}
}
public void Discovery(View view) {
if(BA.isDiscovering()) {
BA.cancelDiscovery();
}
}
public void list(View view){
Toast.makeText(getActivity(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v) {
for(int j=0;j<length;j++) {
if(v.getId()==j)
Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show();
//hand.update(run,1000);
}
}
}
You can get the signal from following code.
Code your activity
#Override
public void onCreate(Bundle savedInstanceState) {
.....
// Registering Broadcast. this will fire when Bluetoothdevice Found
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
}
private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String mIntentAction = intent.getAction();
if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) {
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
}
}
};
This broadcast will execute when your device will be connected to the remote device.
There are several other action on which you can fire this broadcast. have a look at here(BluetoothDevice)
Check following link for constant reading of RSSI
Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)
Output of link :
Then if you want to do it in continuously, you need to run it in a service or in a thread. So that you can even add time slots or sleep (waits) measuring the RSSI.

Categories

Resources