Clear the bluetooth name cache in android - android

I want to clear the blueooth cache.
I'm making application about bluetooth.
I just knowed Android bluetooth device name is cached in the phone. (when phone find the bluetooth device)
I can't find solution anywhere.
I just saw fetchUuidsWithSdp().
someone said it clears the bluetooth cache in android phone.
but I don't know how use this method and I don't think that fetchUuidsWithSdp() will clear the cache.
Please Let me know how to clear the bluetooth cache in Android or how to use fetchUuidsWithSdp().
Thanks:)

Follow the instructions of the Android Guide in order to start the discovery.
When declaring your IntentFilter, add (at least) ACTION_FOUND and ACTION_UUID :
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
Inside your receiver call fetchUuidsWithSdp().
You will get the UUIDs inside the ACTION_UUID:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// get fresh uuids
device.fetchUuidsWithSdp();
}
else if (BluetoothDevice.ACTION_UUID.equals(action)) {
// Get the device and teh uuids
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
if(uuidExtra !=null){
for (Parcelable uuid : uuidExtra) {
Log.d("YOUR_TAG", "Device: " + device.getName() + " ( "+ device.getAddress() +" ) - Service: " + uuid.toString());
}
}
}

Related

Android fetchUuidsWithSdp() giving UUIDs in little endian representation

I am building a bluetooth service discovery on Android using BluetoohDevice.fetchUuidsWithSdp().
Currently i test the code on 3 different devices:
devices "A" and "B" running Android 8.1.0
device "C" is on 5.1.1.
Each device can act as Bluetooth server
using
mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(serviceName, serviceUUID)
or as Bluetooth client
mmDevice.createRfcommSocketToServiceRecord(serviceUUID);
If a server is discovered by a client device the UUIDs of services running on the server will be fetched using fetchUuidsWithSdp().
Then i get the UUIDs in the BroadcastReceiver:
private BroadcastReceiver UUIDFetchedReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_UUID))
{
try
{
// Getting the Service UUIDs
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "UUIDFetchedReceiver: received device: " + device.getName());
for (Parcelable uuid : uuidExtra)
{
Log.d(TAG, "UUIDFetchedReceiver: Service found on " + device.getName() + " | uuid: " + uuid);
if (uuid.toString().equals("3253f2d2-f49a-11ec-b939-0242ac120002"))
{
foundSDPService(device, uuid.toString());
}
}
}
catch (NullPointerException e)
{
Log.e(TAG, "UUIDFetchedReceiver: Nu UUIDs received ");
}
}
}
};
That works on device "A" and "C" the log output there shows:
UUIDFetchedReceiver: Service found on Device B | uuid: 3253f2d2-f49a-11ec-b939-0242ac120002
On Device "B" it looks like this:
UUIDFetchedReceiver: Service found on Device A | uuid: 020012ac-4202-39b9-ec11-9af4d2f25332
Device "B" shows the same UUID but as little endian.
The code is the same and for device "A" and "B" even the Android version is the same.
Does anybody know what causes this issue and how it can be fixed?
The devices are:
A = Samsung Galaxy Tab A (2016)
B = Lenovo TB-X304L
C = LG G Flex 2
The problem seems to be similar to the one described in this Question :
Strange UUID reversal from fetchUuidsWithSdp

BluetoothManager getConenctedDevices returns an empty array

I've connected to Xiaomi air buds via bluetooth settings on my phone.
Then, I tried to use getConnectedDevices(BluetoothProfile.GATT_SERVER) and getConnectedDevices(BluetoothProfile.GATT) methods to get a list of connected devices, but in both cases I got an empty array as a result. If I try to use some other profile in getConnectedDevices() I get an exception that says that I'm using a wrong profile.
How can I correctly get a list of currently connected bluetooth devices to my phone.
code example, in onCreate:
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> connectedDevices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
for (BluetoothDevice b :
connectedDevices) {
Log.e(TAG, "onStart: connectedDevice - " + b.toString() );
}
To get a list devices that you connected to ( in the past )
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedList = btAdapter.getBondedDevices();
for (BluetoothDevice pairedDevice : pairedList )
{
Log.d("BT", "pairedDevice.getName(): " + pairedDevice.getName());
Log.d("BT", "pairedDevice.getAddress(): " + pairedDevice.getAddress());
}
To get the device you correctly connected to you will have to use a broadcast receiver like in the following
private final BroadcastReceiver btReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("Device",device.getName()+ "\n"+ device.getAddress()+"\n"+ device.getBondState());
}
}
};
There's a few more ways to just find out what the state of the connection of the headset as shown here.
if that still not answering your question then I'm sorry I probably misunderstood your question / need.

Start the bonding process with a remote bluetooth device

I have managed to make an android device and scan and enlist all bluetooth devices in the area..I have the mac address of a remote device i just discovered in form of string and i want to start the bonding process with it.. i try BluetoothDevice object and then method createBond() but its not communicating with the remote device to pair..
Here is the code
class BluetoothM: AppCompatActivity{
// mac address of remote bluetooth device
string address;
//the discovered devices are listed in a ListView so i call a listview item click method to start pairing
private void Dlist_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//the mac address has already been assigned in the OnReceive broadcast Receiver
if (e.Position == e.Id)
{
//Get the default adapter of the device
BluetoothAdapter adapt = BluetoothAdapter.DefaultAdapter;
//Get the remote device based on its MAC address
BluetoothDevice device = adapt.GetRemoteDevice(address);
//start the pairing process for the device
device.CreateBond();
}
}
}
//This class discovers bluetooth devices and pass MAC address to our string address for use in the listview click method
[BroadcastReceiver(Name = BluetoothDevice.ActionFound, Enabled = true)]
public class DeviceDiscovered : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string ac = intent.Action; string name;
if (BluetoothDevice.ActionFound.Equals(ac))
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
//add bluetooth name and address if they do not already exist
if (!MainActivity.avail.Contains(device.Name + "\n" + device.Address))
{
MainActivity.avail.Add(device.Name + "\n" + device.Address);
ArrayAdapter arrayAdapter1 = new ArrayAdapter(context, Android.Resource.Layout.SimpleListItem1, MainActivity.avail);
MainActivity.dlist.Adapter = arrayAdapter1;
//address assigned a value
MainActivity.address = device.Address; MainActivity.name = device.Name;
MainActivity.mydevice = device;
}
}
Toast.MakeText(context, "Received the intent", ToastLength.Short).Show();
}
The remote device is not receiving this communication and i don't know why, Thank You
Check you have BLUETOOTH_ADMIN permission in Manifest. Register for ACTION_BOND_STATE_CHANGED intents to be notified when the bonding process completes, and its result. Get the result of device.CreateBond(), it returns false on immediate error, true if bonding will begin.
https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createBond()

BluetoothDevice.getName() is null

I use the following receiver to get the surrounding bluetooth devices :
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.d(TAG, "Device hardware adress : " + deviceHardwareAddress);
if (deviceName != null){
Log.d(TAG, "Device name : " + deviceName);
if (deviceName.contains("GYDE")){
Log.d(TAG, "Stooping discovery : " + deviceName);
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(mReceiver);
}
}
}
}
};
I have 2 bluetooth devices in the area. I see their MAC Adress but they've got no names. I believe though they have names because :
1) I could see it when testing yesterday (nothing has been modified since then)
2) My phone can list their name after enabling bluetooth
3) I can actually see my phone name

Android Bluetooth fail when BluetoothSocket.connect

Ok, I feel like this question is still the same old ****, but I really can't figure it out after trying all the available methods from Google. This are the errors that I got:
isSocketAllowedBySecurityPolicy start : device null
getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
I am trying to write a simple android program that can be used to connect to a micro-controller. I used a BroadcastReceiver to receive the discovered device. The code looks like this:
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 (uuidExtra == null) {
Log.d("Bluetooth", "=+=+=+=+ 1st return uuid is null.");
}
if (device.getName().equals("DeviceIWant")) {
mDevice = device;
mStatus.setText("");
mStatus.append("\n" + mDevice.getName() + "\n" + mDevice.getAddress());
}
String str = device.getName() + "\n" + device.getAddress();
Log.d("Bluetooth", "\n=+=+=+=+=+=+=+=+=+ Get in onReceive");
Log.d("Bluetooth", str);
// mBluetoothAdapter.startDiscovery();
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// Check if the desired device has been found yet.
if (mDevice != null) {
Log.d("Bluetooth", "\n=+=+=+=+ BT dev is not null.");
// If the desired device has been found, start getting the UUID
mDevice.fetchUuidsWithSdp();
}
}
if (BluetoothDevice.ACTION_UUID.equals(action)) {
Log.d("Bluetooth", "\n=+=+=+=+ BT dev uuid returned.");
// Observe the UUID
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
Parcelable uuidExtra[] = mDevice.getUuids();
if (uuidExtra == null ) {
Log.d("Bluetooth", "\n=+=+=+=+ return uuid is null.");
}
Log.d("Bluetooth", "\n=+=+=+=+ uuid Extra returned.");
// Log.d("Bluetooth", "UUID: " + uuidExtra[0].toString());
// Create the thread (it is not yet run)
mThread = new ConnectThread(mDevice);
// Start running the thread
mThread.start();
// Update the thread flag
isThreadRunning = true;
}
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
// Observe the UUID
Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps started.");
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// Observe the UUID
Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps DONE!");
}
}
};
The error occur when I try to use mSocket.connect(), where the mSocket is an instance of BluetoothSocket. The socket was created by using this line of code:
device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
The device I use is returned by the broadcast that I saved as mDevice in the above code. The creation of the socket seems to be fine. I used the very famous SPP UUID (00001101-0000-1000-8000-00805F9B34FB) to establish the connection with no luck. I also tried to obtained the UUID from the device by using
mDevice.fetchUuidsWithSdp();
follows by
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
but the problem is uuidExtra returned is always null. And when I connect the errors are the same.
I hope the problem description is clear. If any of you need more code to make the problem clearer, please let me know. Thanks for the time of reading the problems and the help!

Categories

Resources