Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to device with Bluetooth address on String

I am doing an Android App and where I have the MAC of another device as a string (17 characters long) and need to use that one in order to connect to that device (thread that initiates a Bluetooth connection). I've been playing around with it all afternoon and can't figure out how to do it. The problem is that it doesn't allow me to set the BluetoothDevice equal to a string. Is there a way that this can/has to be done?

(decided not to put any of my attempts here as code, seeing how they were full of errors)

It has to communicate with another tablet that is running the exact same application. I looked through this page earlier and most of my app is based on that. My main problem is when using the ConnectThread example,

I have a string with the MAC address, how do I connect to that MAC?

Any help would be highly appreciated,

like image 379
Marshall Avatar asked Jun 03 '13 17:06

Marshall


People also ask

Can you locate a device with Bluetooth address?

Using this property of Bluetooth, an app called Wunderfind for iPhone or Android can help you physically locate a lost, hidden, or unknown Bluetooth device using your smartphone. This includes PCs, laptops, tablets, smartphones, Bluetooth headphones, Airpods, smartwatches, smart home devices, and more.

What can I do with a Bluetooth address?

The upper half of a Bluetooth Address (most-significant 24 bits) is so called Organizationally Unique Identifier (OUI). It can be used to determine the manufacturer of a device (Bluetooth MAC Address Lookup form).


3 Answers

If I understand correctly, you have a MAC address as a string, and you want to connect to the device, right? This should work:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
BluetoothSocket tmp = null;
BluetoothSocket mmSocket = null;

// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (IOException e) {
    Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
like image 91
janos Avatar answered Oct 28 '22 06:10

janos


Convert the String value to Bluetooth Devices.

BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothDevice mBluetoothDevice = bluetoothManager.getAdapter() .getRemoteDevice("deviceAddress");
like image 27
sivaprakash Avatar answered Oct 28 '22 05:10

sivaprakash


First you will have to findout what profile the bluetooth device supports, For instance it could be a medical device that could use HDP profile or it could be using a simple RS232 over bluetooth. It is important to understand how the bluetooth connection is established for various profiles before you start writing code.

Here is a good link to start with. Android SDK also comes withe some basic examples that you can start with.

http://developer.android.com/guide/topics/connectivity/bluetooth.html

EDIT:

If your device is paired successfully,you will see the MAC address in the list of paired devices. For instance, you can do this to find the device that matches your device's MAC address :

  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
                    .getBondedDevices();
            if (pairedDevices.isEmpty()) {
                Log.e(TAG,
                        "No devices paired...");
                return ;
            }

    for (BluetoothDevice device : pairedDevices) {
                Log.d(TAG, "Device : address : " + device.getAddress() + " name :"
                        + device.getName());
            if (MY_MAC_ADDR.equals(device.getAddress())) {
                mDevice = device;
                break;
            }
    }

Hope that helps.

like image 38
Ramp Avatar answered Oct 28 '22 06:10

Ramp