connect to device with Bluetooth address on String

android connect to bluetooth device programmatically
android get current connected bluetooth device
bluetooth mac address scanner
bluetooth ip address
bluetooth address lookup
action_pairing_request
scan for bluetooth devices
how to identify bluetooth device

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,

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;

This is an excerpt from the source code of this simple open-source Android App: https://github.com/janosgyerik/bluetoothviewer

The app is a simple tool for debugging Bluetooth connections and raw protocol data. (For now only in ascii, I plan to add features to debug hexadecimal as well.)

BluetoothDevice, These devices respond with their Bluetooth address and class of device record. L2CAP client applications typically get a connection string from one of the  Pair a Bluetooth Device and Retrieve the Bluetooth Device Address. You can pair the Bluetooth ® device to your host computer and retrieve the Bluetooth device address or serial port based on your operating system. Instrument Control Toolbox software is required to connect to your Arduino ® hardware using an HC-05 or HC-06 Bluetooth module

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.

Pair a Bluetooth Device and Retrieve the Bluetooth Device Address , GIAC to instruct the agent to search for all generally discoverable devices. In rare which return the Bluetooth address and display name of the device as strings, respectively. Keep in mind that getFriendlyName involves actually connecting to​  To connect a Bluetooth printer or scanner. Turn on your Bluetooth printer or scanner and make it discoverable. Turn on Bluetooth on your PC if it's not on already. To do this, select the Start button, then select Settings > Devices > Bluetooth & other devices and turn on Bluetooth. Select Printers & scanners > Add a printer or scanner.

Convert the String value to Bluetooth Devices.

BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothDevice mBluetoothDevice = bluetoothManager.getAdapter() .getRemoteDevice("deviceAddress");

Bluetooth Address, starts with two slashes followed by the Bluetooth address of the device to connect to The client connection string takes master, authenticate, and encrypt as  Device address, specified as a character vector or string scalar. Identify the address of the peripheral device you want to connect to using the information returned by blelist. If a device does not have a name, then use its address to connect to it.

Bluetooth Essentials for Programmers, The connection string also causes the remote device to be authenticated. establishes an RFCOMM connection to the Bluetooth device with the address of  Tap Connected devices Connection preferences Bluetooth. If you don't see "Connection preferences," go to the next step. Make sure Bluetooth is turned on. Tap Device name. If you don't see "Device name," you're using a different Android version. Tap More Rename this device. Enter a new name. Tap Rename.

Bluetooth Application Programming with the Java APIs Essentials , Android Bluetooth get list of paired devices with examples. devices to connect wirelessly to exchange the data with other Bluetooth devices. String deviceHardwareAddress = device. If you observe above code, we are getting the Bluetooth paired devices name and mac address by using BluetoothDevice object. connect to the device (static MAC-address) connect to the nordic-UART-service; receive texts from the Feather; implement surveillance of the connection; Right now I'm trying to understand how BLE works. I understand it as far as to connect to a device but I don't get it after that point.

Bluetooth, Connecting to the device. Now, when we have discovered the MAC address, we connect the device by using: public boolean connect(String address)  Then try the following: Turn off your Bluetooth device, wait a few seconds, and turn it back on. Make sure your Bluetooth device is in range. If your Bluetooth device is unresponsive or sluggish, check to make sure it’s not too close to any other USB device that’s plugged into a USB 3.0 port.

Comments
  • What bluetooth profile are you using ? The 17 character device name could be just a MAC address of the device.
  • Yes, sorry. Meant the MAC address...
  • Sorry that I didn't specify. It has to communicate with another tablet that is running the exact same application. I looked through that page earlier and most of my app is based on that. My main problem is when using the ConnectThread example, that I don't know how to put the MAC address (string) = mmDevice Thank you for your answer,
  • Edited my response to answer your question.