connect to device with Bluetooth address on String

后端 未结 3 1502
长发绾君心
长发绾君心 2021-01-04 00:04

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 tha

相关标签:
3条回答
  • 2021-01-04 00:52

    Convert the String value to Bluetooth Devices.

    BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothDevice mBluetoothDevice = bluetoothManager.getAdapter() .getRemoteDevice("deviceAddress");
    
    0 讨论(0)
  • 2021-01-04 00:53

    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.)

    0 讨论(0)
  • 2021-01-04 00:54

    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.

    0 讨论(0)
提交回复
热议问题