Android Question Set Bluetooth Name?

Nenad Hukavec

Member
Licensed User
Longtime User
Is it posible to programatically set/change Bluetooth adapter name?
Can you give me some hint?
I can't find any answer to this question, so please forgive me if I post question which already has answer somewhere! :)
 

Beja

Expert
Licensed User
Longtime User
Yes you can..
Bluetooth module (or any bt device) has to be physically connected to the PC by serial cable (RS232/485)
Then use standard AT commands to change name and other parameter.
I will dig into my software lib and try to find the AT commands specific to BT. but can't promise soon as I am
traveling. But you can consult the docs of the BT device you are using.
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
Thanks for quick answer, but I think You have misunderstand me... maybe my question wasn't clear enough, I apologise once more.
What I trying to do is change the name which will be visible when somebody scan my device.

Here is a way to get readonly property of name which I wish to change:

B4X:
Dim SER As Serial
SER.Initialize("SER")
Log(SER.name)


It is "Device Friendly name".
I wish that my app can set bluetooth device friendly name, not that user must change it manualy through android settings...
So, when users starts app, I wish to change BT name from default name or earlier set name, to some other name just with their permission, and without asking they to manualy change name in device settings.
 
Last edited:
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
You can try to open Bluetooth settings, press menu button and choose 'Rename device' (Android 4.1.2).
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
How to do that programatically? Do you mean something like:

B4X:
Dim i As Intent
i.Initialize("android.settings.BLUETOOTH_SETTINGS", "")
StartActivity(i)

...that opens settings page, but how to call menu button, put some text in 'rename device' dialog and click 'ok' button?

What I need is funcion like that one described below, but with name setName() and functionality which sets friendly Bluetooth name.

public String getName ()

Added in API level 5
Get the friendly Bluetooth name of the local Bluetooth adapter.

This name is visible to remote Bluetooth devices.

Requires BLUETOOTH

Returns
  • the Bluetooth name, or null on error

Is there any way to get to android.BluetoothAdapter object and change EXTRA_LOCAL_NAME?
Or some other way to achieve the same?
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
Sorry, I misunderstood.
Do you want to change the Bluetooth name of the device without notifying the user ?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Still I believe I understood you well.
1- You have a bluetooth device (not windows computer), that your app will communicate with.
2- That device has MAC address and default friendly name
3- You want to change the default friendly name.

If the above is correct, then I (believe) the best way to change that name is by sending AT commands
to the BT device through wired connection as I described in my first reply.
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
Sorry, I misunderstood.
Do you want to change the Bluetooth name of the device without notifying the user ?

The user is informed of the name change at the beginning of the application and give permission for a change. After that I want to programatically put a new name without the user must enter the settings manually type a new name.
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
Still I believe I understood you well.
1- You have a bluetooth device (not windows computer), that your app will communicate with.
2- That device has MAC address and default friendly name
3- You want to change the default friendly name.

If the above is correct, then I (believe) the best way to change that name is by sending AT commands
to the BT device through wired connection as I described in my first reply.

Let me explain this way:
1. Device is my android phone or tablet
2. My device has MAC and default friendly name
3. I want to change the default friendly name of my device

But the way that I want to do that is programatically within my app that is installed on my device, not by manualy changing Bluetooth settings.
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
It should be possible according to this thread http://stackoverflow.com/questions/8377558/change-the-android-bluetooth-device-name

There is also following information in the BluetoothAdapter referance (http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html):

public boolean setName (String name)
Set the friendly Bluetooth name of the local Bluetooth adapter.
This name is visible to remote Bluetooth devices.
Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, although many remote devices can only display the first 40 characters, and some may be limited to just 20.
If Bluetooth state is not STATE_ON, this API will return false. After turning on Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value.

Requires BLUETOOTH_ADMIN
Parameters
name a valid Bluetooth name
Returns
true if the name was set, false otherwise


Maybe it's possible to update the Serial library (BluetoothAdmin type) with this new method.
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
Maybe it's possible to update the Serial library (BluetoothAdmin type) with this new method.

Exactly, I already visit that two links, but cannot find the same function in B4A or any library, that's why I posted this question! :)

It would be great to have SERIAL.setName(), just like we have SERIAL.getName().
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
But the way that I want to do that is programatically within my app that is installed on my device, not by manualy changing Bluetooth settings.

It is not about manually and grammatically.. it is about the device must be physically connected to the PC (or any host mController).
e.g. if the AT command is AT+NAME="New Name"/r/n , then you can put this in your program, or use a comm terminal to send it
to the bt device.. but in both cases the device MUST be connected by serial cable to your machine, this is what I am trying to say.
Also before connecting the device you need to pull-up some pins to VCC. this way you put the device in command mode, ready to edit the params.
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
It is not about manually and grammatically.. it is about the device must be physically connected to the PC (or any host mController).
e.g. if the AT command is AT+NAME="New Name"/r/n , then you can put this in your program, or use a comm terminal to send it
to the bt device.. but in both cases the device MUST be connected by serial cable to your machine, this is what I am trying to say.
Also before connecting the device you need to pull-up some pins to VCC. this way you put the device in command mode, ready to edit the params.

Here we are not talking about an independant Bluetooth device like a serial/Bluetooth adapter, we are talking about the Android device itself (phone / tablet). You cannot connect the Android device with serial cable to the computer.
 
Upvote 0

Nenad Hukavec

Member
Licensed User
Longtime User
Guys, a few moments ago I build my new library called "BTFN - Bluetooth Friendly Name"!
It does exactly what I need - changes BT friendly name from within my app.

So, thank you both!

Here is library - maybe somebody find it usefull!

Usage:

B4X:
Dim BTFN As BTFN
Log(BTFN.Name)
BTFN.setName("NewName")
Log(BTFN.Name)
 

Attachments

  • BTFN.zip
    1.4 KB · Views: 286
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks for sharing the lib.. it has a very small footprint, just over 1k.
 
Upvote 0
Top