Android display bluetooth devices

How to get name of the connected Bluetooth device on android

I can get information about the device that is previously paired and trying to make a connection or a device trying to pair to the device. what I want is the name or the connection state of the currently paired and connected device.

2 Answers 2

String name; String address; String threadName; public void checkConnected() < BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET); >private BluetoothProfile.ServiceListener serviceListener = new BluetoothProfile.ServiceListener() < @Override public void onServiceDisconnected(int profile) < >@Override public void onServiceConnected(int profile, BluetoothProfile proxy) < for (BluetoothDevice device : proxy.getConnectedDevices()) < name = device.getName(); address = device.getAddress(); threadName = Thread.currentThread().getName(); Toast.makeText(MainActivity.this, name+" " + address+ threadName, Toast.LENGTH_SHORT).show(); txtName.setText(name + " " + address); Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = " + BluetoothProfile.STATE_CONNECTED + ")"); >BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy); > >; 
BluetoothServerSocket bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("abc", uuid); BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept(); BluetoothDevice device = bluetoothSocket.getRemoteDevice(); String deviceName = device.getName(); 

looks like the code stock when it get to this line BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();

Have you checked the documentation? developer.android.com/guide/topics/connectivity/… Check out the sections «Querying paired devices» and «Discovering devices». Especially the call to device.getName()

Thanks for your reply. But what I want is to get the connection status of the currently paired and connected Bluetooth device . No the ones that are trying to connect. For that situation I preferred to use broadcast Listers

And about the link you mentioned, BluetoothDevice class doesn’t provide a function to make a query of paired devices connection state

Источник

How to discover Bluetooth devices Programatically in android and display in Listview

Edit: An easy way to accomplish this will be like this snippet to find bluetooth devices hope it helps happy coding Question: Problem Statement I am trying to make an application for Android that can detect nearby smartphone devices. To discover Bluetooth devices programmatically you need to add two permission i.e. ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION.

How to discover Bluetooth devices Programatically in android and display in Listview

I tried and followed some tutorial on web but it didn’t work on new Android versions.

I declared all Bluetooth permissions and used Dexter permission library. I followed few answers but it doesn’t display available Bluetooth device name also

@Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); scan.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < toast("starts scanning. "); mBluetoothAdapter.startDiscovery(); >>); mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() < @Override public void onItemClick(AdapterViewadapterView, View view, int i, long l) < String bluetoothDevice = mAdapter.getItem(i); toast(bluetoothDevice); >>); > public void pairedDevicesListView(View view) < mAdapter.clear(); pairedDevices = mBluetoothAdapter.getBondedDevices(); for (BluetoothDevice device : pairedDevices)< mAdapter.add(device.getName() + "\n" + device.getAddress()); >> > 

To discover a device, first get the bluetooth adapter by calling BluetoothAdapter.getDefaultAdapter()

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 

To start discover, simply call the startDiscovery() from bluetooth adapter. This process is asynchronous so it will return immediately. To catch the discovery process, we can register a BroadcastReceiver with ACTION_FOUND, ACTION_DISCOVERY_STARTED, ACTION_DISCOVERY_STARTED. For each device found, the intent will carry extra field EXTRA_DEVICE containg the BluetoothDevice object.

IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter); adapter.startDiscovery(); 
private final BroadcastReceiver mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) < //discovery starts, we can show progress dialog or perform other tasks >else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) < //discovery finishes, dismis progress dialog >else if (BluetoothDevice.ACTION_FOUND.equals(action)) < //bluetooth device found BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); showToast("Found device " + device.getName()); >> >; 

And, don’t forget to unregister the receiver on Activity’s onDestroy method:

@Override public void onDestroy()

I forgot to declare Location permissions in the manifest file. To discover Bluetooth devices programmatically you need to add two permission i.e. ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION.

Читайте также:  Где блютуз на нокиа

Add manifest permissions as follows

protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnstart=findViewById(R.id.btnstart); mListView=findViewById(R.id.listofdevices); final ArrayAdapter mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1); mListView.setAdapter(mAdapter); txt1=findViewById(R.id.txt1); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); setdevicevisible(); boolean hasBluetooth = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); if(!hasBluetooth) < AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); dialog.setTitle(getString(R.string.bluetooth_not_available_title)); dialog.setMessage(getString(R.string.bluetooth_not_available_message)); dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int which) < // Closes the dialog and terminates the activity. dialog.dismiss(); MainActivity.this.finish(); >>); dialog.setCancelable(false); dialog.show(); > if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) < ActivityCompat.requestPermissions((Activity) this, new String[], 1); > // If another discovery is in progress, cancels it before starting the new one. if (mBluetoothAdapter.isDiscovering()) < mBluetoothAdapter.cancelDiscovery(); >mBluetoothAdapter.startDiscovery(); mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); //Finding devices if (BluetoothDevice.ACTION_FOUND.equals(action)) < // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mAdapter.add(device.getName() + "\n" + device.getAddress()); >> >; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); > 

You could use the MAC address as unique ID.

And you can find in the official doc here a complete example of it

About Signal Strength i think you should use RSSI (Received Signal Strength Indicator)

Edit: An easy way to accomplish this will be like this snippet to find bluetooth devices

mBluetoothAdapter.startDiscovery(); mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); //Finding devices if (BluetoothDevice.ACTION_FOUND.equals(action)) < // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); >> >; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); 

Android how to get the name of connected bluetooth, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

006 : Discover Devices

In this video, we will see the practical on Android Studio that how we can discover nearby available Bluetooth devices . We will call a method startDiscovery(

Discover Nearby Smartphone Devices on Android

Problem Statement

I am trying to make an application for Android that can detect nearby smartphone devices . I need to discover smartphone devices regardless of their connection to a WLAN or their operating system. The only requirement is that their bluetooth or wifi setting is enabled. I am trying to capture the MAC Address and maybe RSSI of these devices. An example of an application I am trying to emulate is Meshlium.

Methods Already Considered

Android Classic Bluetooth:

  • I have followed the guide to discover devices that aren’t currently paired with my device.
  • The smartphones I am trying to discover need to be in «discovery» mode in order to be found. For most modern phones that means they need to be on their bluetooth settings page.

Android P2P (Wifi Direct):

  • I have followed the guide again and can’t discover smartphones nearby.
  • Similar to bluetooth, both devices (mine and the device I am trying to find) need to be both trying to discover Wifi Direct devices.
  • Also it seems iOS doesn’t support wifi direct anyways.

Google Nearby:

  • Nearby Connections works only with devices running an application that is using the nearby connections api. So only devices with the app currently running the advertising or discovery feature will be available for detection.

Wifi Network Service Discovery:

  • This lets me see devices that are currently advertising services on a network and not the actual devices themselves.
Читайте также:  Home assistant zigbee bluetooth

Device Specifications

The device I am using is running android version 6.0.1 and Model number rk3368-box. The device also has root permissions .

Question

Is there a way on my android device to discover the nearby smartphones with the only requirement being their wifi or bluetooth is enabled? Perhaps packet sniffing the probe request s of the devices? I have researched Packet sniffing but can’t find any solid resources for Android. Also I would love to be able to do so in c# and Xamarin.Android if possible.

Update

Is a custom ROM an option? I am not looking to make an application for the play store, but one for custom hardware. Perhaps when I enable the wifi hotspot on the device it logs the probe requests from the devices nearby and saves as a file to export later?

There is no way to do this. And there shouldn’t be- unless I’m working with you (in which case multiple of the above will work) its a massive invasion of my privacy for you to be able to detect my device.

If you are able to find mac address for WLAN adaptor of a device than try to search for bluetooth devices having mac address same as WLAN adaptor but last digit changed by 1. This way you can find a Bluetooth device even if it’s visibility is hidden

Find Bluetooth devices, A nearby Bluetooth device responds to a discovery request only if it is currently accepting information requests by being discoverable. If a device is …

Android search for bluetooth devices

I am trying to find the available Bluetooth devices.

This is my OnClickListener which is called when the user tries to search for the available devices.

View.OnClickListener OnSearchDevices = new View.OnClickListener() < @Override public void onClick(View v) < // TODO Auto-generated method stub //Toast.makeText(context, "Search Devices", Toast.LENGTH_LONG).show(); Log.d("Discovery", "Started"); listOfDevices.clear(); label.setText("Searching Available Devices. "); label.setEnabled(false); >>; 

I have also registered a BroadcastReceiver.

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); Log.d("DeviceList" , device.getName() + "\n" + device.getAddress()); MyBluetoothDevice tempDevice = new MyBluetoothDevice(); tempDevice.setDeviceAddress(device.getAddress()); tempDevice.setDeviceName(device.getName()); listOfDevices.add(tempDevice); mListAdapter.notifyDataSetChanged(); // discovery is finished >else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) < Log.d("Discovery","Finished"); label.setEnabled(true); if(listOfDevices.size() == 0) < label.setText("No Devices Available!"); label.setTextColor(Color.parseColor("#FF0000")); >else < label.setText("Available Devices"); >> > >; 

But nothing happens. It does not show anything. Please help.

Looks like you are missing the call to mBluetoothAdapter.startDiscovery() .

That would explain why you are not getting any results, because the adapter doesn’t even start searching for the devices.

Your code looks fine to me otherwise.

How to get current Bluetooth name in android?, This example demonstrate about How to get current Bluetooth name in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill …

Источник

How to get the bluetooth devices as a list?

I am trying to get my bonded bluetooth devices but I can get it as a long string instead of list. This is my code:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList listview = new ArrayList(Arrays.asList(pairedDevices.toString())); setListAdapter(new ArrayAdapter(this, R.layout.list, listview)); 

I am getting something like this: [00:23:7F:1c, f0:09:f1:b4:b0] . And its all in one line. How can I change it to be in a list and not all in one line? Also, how can I get the friendly names of the devices and not these numbers? Thanks.

6 Answers 6

You should change your code as below:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = mBluetoothAdapter.getBondedDevices(); List s = new ArrayList(); for(BluetoothDevice bt : pairedDevices) s.add(bt.getName()); setListAdapter(new ArrayAdapter(this, R.layout.list, s)); 

yea. Thanks. only need to add that i changed List s = new ArrayList(); to ArrayList s = new ArrayList(); it works great now.

Читайте также:  Mac mini have bluetooth

have the same error after your update. did the same change i did before and it works now. this was the error it gave me befor my fix: The type List is not generic; it cannot be parameterized with arguments

The implementation for list item click listener totally depends on you. You may definitely extend AdapterView.OnItemClickListener to make Bluetooth connection to the device shown in list. Use listView.setOnItemClickListener to provide your custom click listener.

Find list of Nearby Bluetooth Devices:

Find Screenshot for the same.

enter image description here

MainActivity.java:

public class MainActivity extends ActionBarActivity < private ListView listView; private ArrayListmDeviceList = new ArrayList(); private BluetoothAdapter mBluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.startDiscovery(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); >@Override protected void onDestroy() < unregisterReceiver(mReceiver); super.onDestroy(); >private final BroadcastReceiver mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) < BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); mDeviceList.add(device.getName() + "\n" + device.getAddress()); Log.i("BT", device.getName() + "\n" + device.getAddress()); listView.setAdapter(new ArrayAdapter(context, android.R.layout.simple_list_item_1, mDeviceList)); > > >; 

activity_main.xml:

Manifest file:

Please make sure that you ask Location permission to your user and turn GPS On.

Reason: From Android 6.0 you need Location permission for Bluetooth Discovery.

More reference:

Do you know how can I connect it with a click? I mean if I have a list with all of my paired devices do you know how to connect it with 1 click?

I am trying this code.It is provide all paired devices. How can I get ReadyToPair device?Is there any way to achieve this thing?

ListPairedDevicesActivity.java

import java.util.Set; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListPairedDevicesActivity extends ListActivity < @Override protected void onCreate(Bundle savedInstanceState) < // TODO Auto-generated method stub super.onCreate(savedInstanceState); ArrayAdapterbtArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) < for (BluetoothDevice device : pairedDevices) < String deviceBTName = device.getName(); String deviceBTMajorClass = getBTMajorDeviceClass(device .getBluetoothClass() .getMajorDeviceClass()); btArrayAdapter.add(deviceBTName + "\n" + deviceBTMajorClass); >> setListAdapter(btArrayAdapter); > private String getBTMajorDeviceClass(int major) < switch(major)< case BluetoothClass.Device.Major.AUDIO_VIDEO: return "AUDIO_VIDEO"; case BluetoothClass.Device.Major.COMPUTER: return "COMPUTER"; case BluetoothClass.Device.Major.HEALTH: return "HEALTH"; case BluetoothClass.Device.Major.IMAGING: return "IMAGING"; case BluetoothClass.Device.Major.MISC: return "MISC"; case BluetoothClass.Device.Major.NETWORKING: return "NETWORKING"; case BluetoothClass.Device.Major.PERIPHERAL: return "PERIPHERAL"; case BluetoothClass.Device.Major.PHONE: return "PHONE"; case BluetoothClass.Device.Major.TOY: return "TOY"; case BluetoothClass.Device.Major.UNCATEGORIZED: return "UNCATEGORIZED"; case BluetoothClass.Device.Major.WEARABLE: return "AUDIO_VIDEO"; default: return "unknown!"; >> @Override protected void onListItemClick(ListView l, View v, int position, long id) < // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); >> 

AndroidBluetooth.java

import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class AndroidBluetooth extends Activity < private static final int REQUEST_ENABLE_BT = 1; private static final int REQUEST_PAIRED_DEVICE = 2; /** Called when the activity is first created. */ Button btnListPairedDevices; TextView stateBluetooth; BluetoothAdapter bluetoothAdapter; @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); btnListPairedDevices = (Button)findViewById(R.id.listpaireddevices); stateBluetooth = (TextView)findViewById(R.id.bluetoothstate); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); CheckBlueToothState(); btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener); >private void CheckBlueToothState()< if (bluetoothAdapter == null)< stateBluetooth.setText("Bluetooth NOT support"); >else< if (bluetoothAdapter.isEnabled())< if(bluetoothAdapter.isDiscovering())< stateBluetooth.setText("Bluetooth is currently in device discovery process."); >else < stateBluetooth.setText("Bluetooth is Enabled."); btnListPairedDevices.setEnabled(true); >>else < stateBluetooth.setText("Bluetooth is NOT Enabled!"); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); >> > private Button.OnClickListener btnListPairedDevicesOnClickListener = new Button.OnClickListener()< @Override public void onClick(View arg0) < // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(AndroidBluetooth.this, ListPairedDevicesActivity.class); startActivityForResult(intent, REQUEST_PAIRED_DEVICE); >>; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < // TODO Auto-generated method stub if(requestCode == REQUEST_ENABLE_BT)< CheckBlueToothState(); >if (requestCode == REQUEST_PAIRED_DEVICE) < if(resultCode == RESULT_OK)< >> > > 

AndroidManifest.xml

Источник

Оцените статью
Adblock
detector