How to get Bluetooth hardware address of remote device from its properties? - android

I am trying to connect to remote device which needs the Bluetooth hardware address of that device to connect:
bdDevice = mBluetoothAdapter.getRemoteDevice(<bluetooth_hardware_address>);
How does one discover the bluetooth address of the remote device ?
These are the parameters of the remote device I already know - Serial Number, Equipment Number, VIN Number.
In iOS this is done using the properties of the remote device to get the Bluetooth hardware address, how is it done on Android.
I am trying to connect to remote device which needs the Bluetooth hardware address of that device to connect:
bdDevice = mBluetoothAdapter.getRemoteDevice(<bluetooth_hardware_address>);
How does one discover the bluetooth address of the remote device ?
These are the parameters of the remote device I already know - Serial Number, Equipment Number, VIN Number.
In iOS this is done using the properties of the remote device to get the Bluetooth hardware address, how is it done on Android.
UPDATE:
I can get address with the following code but this doesn't give serial number to match with --
String ui_serial_number = "000055557F9FC"; //I want to use this SN to connect.
BluetoothAdapter bluetoothAdapter;
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// here you get the mac using device.getAddress()
//But there is nothing like device.getSerialNumber()
if(device.getSerialNumber().equals(ui_serial_number )) //What to do here?
{
//This is the <bluetooth_hardware_address> that I want to connect with.
bdDevice = mBluetoothAdapter.getRemoteDevice(device.getAddress());
break;
}
}
But there is nothing like device.getSerialNumber() to match with the serial number in the UI and connect.

When you scan for BLE devices by BluetoothAdapter.startLeScan() you will get all BluetoothDevices in callback. Then you can get each BluetoothDevice address BluetoothDevice.getAddress()

You can get a Set of all your bonded devices:
BluetoothAdapter bluetoothAdapter;
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// here you get the mac using device.getAddress()
}

Related

Android BLE scan not showing Arduino 101 device

I am running into an issue where my Arduino 101 device will not be discovered by a BLE scan. However, if I scan for normal bluetooth devices it shows up.
I am attempting to use the BLE Gatt example by Google here https://developer.android.com/samples/BluetoothLeGatt/project.html but the device does not show. Even with the unmodified code.
However if I query for connected bluetooth devices it shows my device.
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
String name = "";
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("B & S")) {
bsDevice = device;
name += device.getName();
}
}
If I attempt to connect to the device using createRfcommSocketToServiceRecord(MY_UUID) or createInsecureRfcommSocketToServiceRecord I get an error
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
socket = bsDevice.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
I am using an app, https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en, that allows me to connect to the device but the characteristics that it shows is byte information and is cryptic.
How should I properly connect to the Arduino 101 BLE device?

Android bluetooth scan of already paired

I'm making an Android application that scan all device in the room and connect/pair to the device automatically or not, depending on the name prefix.
I tried some examples and the scan result return only unpaired devices. So I need that the result of the scan return also already paired devices. Is it possible on Android ? (I already done this on windows)
Yes. You need to get the local bluetooth adapter
Adapter = BluetoothAdapter.getDefaultAdapter();
get paired devices into a private set Bluetooth device
devicepair = Adapter.getBondedDevices();
And then from that point it should be a simple for loop to add device pair to a list view that shows your bluetooth devices
for(BluetoothDevice : devicepair)
//your array name or whatever inside the for loop
listviewarray.add(devicepair.getName());

How to scan specific Bluetooth device and get information from it in Android?

I try to build a project that can get Bluetooth rssi from specific devices. However, I'm stuck on that. Is there any way to filter and specify the scanning results and then continuously get information from the chosen Bluetooth devices?
Many thanks.
Yes
You can filter using device MAC address in android
//lets say i have a device i got by searching BT devices
BluetoothDevice device;
String mac = "00:11:22:AA:BB:CC"
if(mac.equals(device.getAddress)){
// my device found
}
public String getAddress ()
Added in API level 5
Returns the hardware address of this BluetoothDevice.
Bluetooth hardware address as string
for more see Android BluetoothDevice doc

How to determine model of smartphone connected to my device

I want to know is there any way to determine model of smartphone connected to my android device via bluetooth programmatically? I know how to make bluetooth connection between two android devices, but how to request information of smartphone's model? Thanks.
Use the BluetoothManager for this task:
List<BluetoothDevice> connectedDevices = this.getSystemService(Context.BLUETOOTH_SERVICE)
.getConnectedDevices(BluetoothProfile.GATT); //or BluetoothProfile.GATT_SERVER
for(BluetoothDevice bd : connectedDevices){
String remoteName = bd.getName();
//do something
}

Android 2.2 progammically tell if my Bluetooth Headset is connected on application start

I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.
Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.
I have also figured out how to see if the device has any paired devices via the following.
BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() != 0)
{
// the device has paired devices
for (BluetoothDevice device : pairedDevices)
{
Log.i(TAG, "device name: " + device.getName());
Log.i(TAG, "device address: " + device.getAddress());
}
}
else
{
// no paired devices
Log.i(TAG, "no paired devices");
}
}
I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as
BluetoothProfile and
BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8
I am willing to try anything at this point.
Thanks in advance
-H
Following code works for me. First connect your BT device with android manually.
AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
//bt device is connected with phone
}
else{
// no device is connected with phone
}
Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.

Categories

Resources