如何允许设备连接到低功耗蓝牙

How to allow a device to be connected to with Bluetooth LE

我正在尝试通过蓝牙 LE 将 raspberry pi 连接到我的 phone。 raspberry pi 已经扫描并连接到设备,我需要做的就是让我的 phone 能够通过我正在尝试为其开发的应用程序连接到。我已设法使用以下代码制作 phone 广告,但每当我尝试连接时,它总是显示 "No data available"。我似乎无法弄清楚如何让它正确连接。

这是我的代码:

    BluetoothAdapter bluetoothAdapter;
    String bluetoothAdapterString;

    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    bluetoothAdapterString = String.valueOf(bluetoothAdapter);

    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();


    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)//*_LOW_POWER or BALANCED
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(true)
            .build();


    ParcelUuid pUuid = new ParcelUuid(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));//this is a service UUID (according to 
    //this UUID might need to be changed
    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .addServiceUuid(pUuid)
            .addServiceData(pUuid, "pleaseWork!".getBytes(StandardCharsets.UTF_8))//"UTF-8"
            .build();


    AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            mainActivity.showMessage.show("bluetooth should be working");
            super.onStartSuccess(settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            mainActivity.showMessage.show("advertising failure....");
            Log.e("BLE", "Advertising onStartFailure: " + errorCode);
            super.onStartFailure(errorCode);
        }
    };

    advertiser.startAdvertising(settings, data, advertisingCallback);

还有,一旦 phone 连接到 raspberry pi,有没有简单的方法在它们之间传输数据?

我不关心我当前的代码,我最终想要的只是让我的 phone 被发现并能够连接到 raspberry pi。

编辑:清晰度

事实证明,.addServiceData 中字符串中的数据仅限于四个字符。只要在这个限制内,上面的代码就可以正常工作。