BLE不会发布服务

时间:2019-03-15 10:10:51

标签: android bluetooth bluetooth-lowenergy

我的代码很好用,Google Chrome的Bluetooth Internals可以检测到手机的广告,但是无法检测到该手机的广告服务数据。

这是我的代码。谢谢您的帮助!

private void advertise(){
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothLeAdvertiser advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(true)
            .build();
    final UUID serviceUuid = UUID.fromString("DAB5D1DC-0000-1000-8000-00805F9B34FB");
    ParcelUuid parcelUuid = new ParcelUuid(serviceUuid);
    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .addServiceUuid(parcelUuid)
            .addServiceData(parcelUuid, "hi".getBytes(Charset.forName("UTF-8")))
            .build();
    AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.i("success", "Advertising onStartSuccess");
            super.onStartSuccess(settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e("error", "Advertising onStartFailure: " + errorCode);
            super.onStartFailure(errorCode);
        }
    };

    advertiser.startAdvertising(settings, data, advertiseCallback);
}

0 个答案:

没有答案