0

i'm developing a code which lists all paired devices (working till here) and on click on a device whcih are listed in textviews, i want the programm to connect to device programmatically. So here is myCode:

        final View.OnClickListener onclicklistener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            index_connection=view.getId();

            try {
                btSocket=devices[index_connection].createInsecureRfcommSocketToServiceRecord(myUUID);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                btSocket.connect();
            } catch (IOException e) {
                try {
                    btSocket.close();
                    Toast.makeText(Connection.this, "Connected to "+devices[index_connection].getName(),Toast.LENGTH_LONG);
                } catch (IOException e1) {
                    Toast.makeText(Connection.this, "Not connected!",Toast.LENGTH_LONG);
                    e1.printStackTrace();
                }

            }

        }
    };

So onclick i'm getting the ID of the Textview which is equal with the index of the device i saved into an array. But now on clicking it does not create any connection (and yes, the other device has Bluetooth activated). The code throws an Exception if i'm getting the device UUID from the Telephony manager:

    myUUID = UUID.fromString(tManager.getDeviceId());

The exception says: java.lang.SecurityException: getDeviceId: The user 10284 does not meet the requirements to access device identifiers.

If i try to create the UUID "fromString" i don't get an exception, but it doesn't connect either.

Best regars, flar2000

flar 2000
  • 11
  • 4

1 Answers1

1

Try this as your UUID, set it like:

private final string MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";

Then you can create your connection using ServiceRecord.

BluetoothDevice mDevice;

Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (BluetoothDevices device : devices) {
    if (device.getAdress().equals("YOUR DEVICE ADRESS HERE")) {
        mDevice = device;
        break;
    }
}

BluetoothSocket mSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
mSocket.connect();

Don't forget to declare on your manifest the bluetooth permission.

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  • what do you mean with "your device adresse here")? is that the adress of the device i want to connect with? – flar 2000 Nov 04 '20 at 20:43
  • I am saving the devices in to a Array of Bluetooth devices on create and by a button for refreshing... so if you click on the textview it calls the device by the textviewID and than just connect to it. For what is the if clause needed? – flar 2000 Nov 04 '20 at 20:50
  • also my code throws an error on run time which says " notifyPackageNameForWifi" do you know what that means? – flar 2000 Nov 04 '20 at 21:11
  • The address is the ones you are keeping on the array, the most important thing is to switch your UUID for this "00001101-0000-1000-8000-00805F9B34FB", this is the StreamSocket to connect with bluetooth, – Vinicius Camargo Nov 05 '20 at 11:39
  • I already changed that... but now i get the error notifyPackageNameForWifi...the Other Code is Equal to the one Above ecxept the if clause – flar 2000 Nov 05 '20 at 12:13
  • `W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback E/BluetoothAdapter: notifyPackageNameForWifi com.example.bomzam_v2/10284` i'm getting this error log.... nothing else is showed on run time. Do you need any more information? Thanks a lot!!! – flar 2000 Nov 05 '20 at 20:21
  • It seems to be just a warning. Reference: https://stackoverflow.com/questions/15889908/getbluetoothservice-called-with-no-bluetoothmanagercallback/24229604#24229604 – Vinicius Camargo Nov 06 '20 at 12:39
  • But still my Bluetooth does not connect. Sadly... do you have another idea? My code is the same as the one above accept the if clause. – flar 2000 Nov 06 '20 at 15:44