Android bluetooth UUID connecting APP to ANDROID
30,022
You can get the UUID from the BluetoothDevice
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice. This code below show how to do it and handle the case that the UUID from the device is not found and trying a default UUID.
// Default UUID
private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
try {
// Use the UUID of the device that discovered // TODO Maybe need extra device object
if (mmDevice != null)
{
Log.i(TAG, "Device Name: " + mmDevice.getName());
Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid());
tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());
}
else Log.d(TAG, "Device is null.");
}
catch (NullPointerException e)
{
Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName());
try {
tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID);
} catch (IOException e1) {
e1.printStackTrace();
}
}
catch (IOException e) { }
Related videos on Youtube
Author by
frankelot
Updated on March 01, 2020Comments
-
frankelot about 4 years
I'm building an android application that keeps tracks of the Bluetooth connection on a device and triggers an alarm when they get out of range.
The Android documentation asks for a UUID in order to establish a connection.
An 'uuid' is a Universally Unique Identifier (UUID) standardized 128-bit format for a string ID used to uniquely identify information. It's used to uniquely identify your application's Bluetooth service.
public ConnectThread(BluetoothDevice device) { // Use a temporary object that is later assigned to mmSocket, // because mmSocket is final BluetoothSocket tmp = null; mmDevice = device; // Get a BluetoothSocket to connect with the given BluetoothDevice try { // MY_UUID is the app's UUID string, also used by the server code tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } mmSocket = tmp; }
I am not installing an app on both devices, so I don't get to set my own UUID, I want to use android's instead... but I can't find this in the docs anywhere.
Maybe I'm not approaching the problem correctly. Any help will be appreciated. Thanks in advance
-
frankelot over 10 yearsYOU! ARE! AWESOME! :D Thaaaaanks man, can't tell you how many hours I've wasted trying to figure this out.. I really appreciate it! :)
-
Braunster over 10 yearsWith pleasure ive wasted to meny hours myself on this. You can check this github.com/itzikBraun/ArduinoCar its an app controlling and arduino via bluetooth there is two thread handling the connection maybe it would help you even more.
-
frankelot over 10 yearsIt's so weird that android won't provide a method to pair two devices together!. (They only added one now, on API 19. When it seems something so basic and fundamental)
-
frankelot over 10 yearsUnfortunately, this only worked on SOME devices. :/ Not a definite solution.
-
Braunster over 10 yearsWhat was the problem maybe i can help.
-
frankelot over 10 yearsWell, your solution worked fine on some devices, but I got no answer whatsoever when tested against different android versions and phone models. I mean, in some cases, the devices just won't react to my connection attempt :(
-
Braunster almost 10 yearscan you tell me which api did you use that caused you the problem? or to what device did you tried to connect?
-
Siluni Upeksha about 8 yearsI have same problem I want to connect to android 5.1.1 device but I was unable.can you please give me solution to connect any bluetooth device
-
Greg Brown about 7 yearsChecking for
null
explicitly is generally preferable to catching aNullPointerException
. -
Manpreet Singh Dhillon about 7 yearsin my case getuuids() returns null and device does not connect with default uuid. is there any work around please?
-
luke cross over 3 yearswhere did you get this "00001101-0000-1000-8000-00805F9B34FB" string???????