简体   繁体   English

无法在 Android Studio 中创建与配对设备的蓝牙连接

[英]Not able to create a Bluetooth connection to paired devices in Android Studio

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.所以 onclick 我得到了 Textview 的 ID,它等于我保存到数组中的设备的索引。 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:如果我从电话管理器获取设备 UUID,代码会抛出异常:

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

The exception says: java.lang.SecurityException: getDeviceId: The user 10284 does not meet the requirements to access device identifiers.异常说: 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.如果我尝试创建 UUID“fromString”,我不会收到异常,但它也不会连接。

Best regars, flar2000最好的问候,flar2000

Try this as your UUID, set it like:尝试将此作为您的 UUID,将其设置为:

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

Then you can create your connection using ServiceRecord.然后您可以使用 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM