当前位置: 首页>>代码示例>>Java>>正文


Java BluetoothAdapter.getBluetoothLeAdvertiser方法代码示例

本文整理汇总了Java中android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothAdapter.getBluetoothLeAdvertiser方法的具体用法?Java BluetoothAdapter.getBluetoothLeAdvertiser怎么用?Java BluetoothAdapter.getBluetoothLeAdvertiser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.bluetooth.BluetoothAdapter的用法示例。


在下文中一共展示了BluetoothAdapter.getBluetoothLeAdvertiser方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: startAdvertising

import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
private void startAdvertising() {
    BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        Log.w(TAG, "Failed to create advertiser");
        return;
    }

    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setConnectable(true)
            .setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
            .build();

    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(false)
            .addServiceUuid(new ParcelUuid(SERVICE_UUID))
            .build();

    mBluetoothLeAdvertiser
            .startAdvertising(settings, data, mAdvertiseCallback);
}
 
开发者ID:Nilhcem,项目名称:blefun-androidthings,代码行数:25,代码来源:GattServer.java

示例2: startAdvertising

import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
/**
 * Begin advertising over Bluetooth that this device is connectable
 * and supports the Current Time Service.
 */
private void startAdvertising() {
    BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        Log.w(TAG, "Failed to create advertiser");
        return;
    }

    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setConnectable(true)
            .setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
            .build();

    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(false)
            .addServiceUuid(new ParcelUuid(TimeProfile.TIME_SERVICE))
            .build();

    mBluetoothLeAdvertiser
            .startAdvertising(settings, data, mAdvertiseCallback);
}
 
开发者ID:androidthings,项目名称:sample-bluetooth-le-gattserver,代码行数:29,代码来源:GattServerActivity.java

示例3: HidPeripheral

import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
/**
 * Constructor<br />
 * Before constructing the instance, check the Bluetooth availability.
 *
 * @param context the ApplicationContext
 * @param needInputReport true: serves 'Input Report' BLE characteristic
 * @param needOutputReport true: serves 'Output Report' BLE characteristic
 * @param needFeatureReport true: serves 'Feature Report' BLE characteristic
 * @param dataSendingRate sending rate in milliseconds
 * @throws UnsupportedOperationException if starting Bluetooth LE Peripheral failed
 */
protected HidPeripheral(final Context context, final boolean needInputReport, final boolean needOutputReport, final boolean needFeatureReport, final int dataSendingRate) throws UnsupportedOperationException {
    applicationContext = context.getApplicationContext();
    handler = new Handler(applicationContext.getMainLooper());

    final BluetoothManager bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);

    final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null) {
        throw new UnsupportedOperationException("Bluetooth is not available.");
    }

    if (!bluetoothAdapter.isEnabled()) {
        throw new UnsupportedOperationException("Bluetooth is disabled.");
    }

    Log.d(TAG, "isMultipleAdvertisementSupported:" + bluetoothAdapter.isMultipleAdvertisementSupported());
    if (!bluetoothAdapter.isMultipleAdvertisementSupported()) {
        throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
    }

    bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    Log.d(TAG, "bluetoothLeAdvertiser: " + bluetoothLeAdvertiser);
    if (bluetoothLeAdvertiser == null) {
        throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
    }

    gattServer = bluetoothManager.openGattServer(applicationContext, gattServerCallback);
    if (gattServer == null) {
        throw new UnsupportedOperationException("gattServer is null, check Bluetooth is ON.");
    }

    // setup services
    addService(setUpHidService(needInputReport, needOutputReport, needFeatureReport));
    addService(setUpDeviceInformationService());
    addService(setUpBatteryService());
    
    // send report each dataSendingRate, if data available
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            final byte[] polled = inputReportQueue.poll();
            if (polled != null && inputReportCharacteristic != null) {
                inputReportCharacteristic.setValue(polled);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        final Set<BluetoothDevice> devices = getDevices();
                        for (final BluetoothDevice device : devices) {
                            try {
                                if (gattServer != null) {
                                    gattServer.notifyCharacteristicChanged(device, inputReportCharacteristic, false);
                                }
                            } catch (final Throwable ignored) {

                            }
                        }
                    }
                });
            }
        }
    }, 0, dataSendingRate);
}
 
开发者ID:kshoji,项目名称:BLE-HID-Peripheral-for-Android,代码行数:74,代码来源:HidPeripheral.java


注:本文中的android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。