本文整理汇总了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);
}
示例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);
}
示例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);
}