1

我正在开发一个应用程序,我想在后台接收到服务器的响应并开始广告后设置我的蓝牙适配器设备名称。所以流程就像

我第一次在我的 onStartCommand 中调用 startAsPeripheral 函数,它可以完美地开始广告,但是当我从服务器接收到数据时,我需要停止正在运行的广告,设置我的蓝牙适配器新名称,然后再次开始广告。

所以问题是每当我使用其他应用程序(如 nRF)扫描我的广告数据包时,甚至只是在从移动设置打开我的蓝牙扫描页面后,它会多次显示我的设备,并使用我已经设置的不同名称。

所以我的 StartAsPeripheral 函数是

 private void startAsPeripheral() {
    stopAdvertising();
    stopServer();
    mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothAdapter.getDefaultAdapter().setName(Constant.userInfo.getUserCode() + " " + 
  Constant.userInfo.getHealthScore());

    if (!mBluetoothAdapter.getName().contains(Constant.userInfo.getUserCode())) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mBluetoothAdapter.getDefaultAdapter().setName(Constant.userInfo.getUserCode() + " " + 
 Constant.userInfo.getHealthScore());
    }

    mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
    Log.d("BluetoothCheck", mBluetoothAdapter.getName() + " After setting device name");

    GattServerCallback gattServerCallback = new GattServerCallback();
    mGattServer = mBluetoothManager.openGattServer(this, gattServerCallback);
    setupServer(mGattServer);

    startAdvertising(mBluetoothLeAdvertiser);
}

和 StartAdvertising 是

 private void startAdvertising(BluetoothLeAdvertiser mBluetoothLeAdvertiser) {
    if (mBluetoothLeAdvertiser == null) {
        return;
    }
    ParcelUuid parcelUuid = new ParcelUuid(SERVICE_UUID);
    AdvertiseSettings advSettings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(true)
            .build();

    AdvertiseData advData = new AdvertiseData.Builder()
            .setIncludeTxPowerLevel(true)
            .addServiceUuid(parcelUuid)
            .build();

    AdvertiseData advScanResponse = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .build();

    advCallback = new AdvertiseCallback() {
        @Override
        public void onStartFailure(int errorCode) {
            super.onStartFailure(errorCode);
            Log.e(TagName, "Not broadcasting: " + errorCode);
            int statusText;
            switch (errorCode) {
                case ADVERTISE_FAILED_ALREADY_STARTED:
                    Log.w(TagName, "ADVERTISE_FAILED_ALREADY_STARTED");
                    break;
                case ADVERTISE_FAILED_DATA_TOO_LARGE:
                    Log.w(TagName, "ADVERTISE_FAILED_DATA_TOO_LARGE");
                    break;
                case ADVERTISE_FAILED_FEATURE_UNSUPPORTED:
                    Log.w(TagName, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED");
                    break;
                case ADVERTISE_FAILED_INTERNAL_ERROR:
                    Log.w(TagName, "ADVERTISE_FAILED_INTERNAL_ERROR");
                    break;
                case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:
                    Log.w(TagName, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS");
                    break;
                default:
                    Log.wtf(TagName, "Unhandled error: " + errorCode);
            }
        }

        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.v(TagName, "Advertising started");
            Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Advertise Callback");
        }

    };
    mBluetoothLeAdvertiser.startAdvertising(advSettings, advData, advScanResponse, advCallback);
    Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Starting Advertising");
}

private void stopServer() {
    if (mGattServer != null) {
        mGattServer.close();
        Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Stop Gatt Server");
    }
}

private void stopAdvertising() {
    if (mBluetoothLeAdvertiser != null) {
        mBluetoothLeAdvertiser.stopAdvertising(advCallback);
        Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Stop Advertising");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
}

所以我只想一次显示一个广告,所以扫描只会收到广告。我正在使用 BLE。所以现在你有了代码,你们可以让我知道我做错了什么?

4

0 回答 0