@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private BluetoothLeAdvertiser getAdvertiserSafely(BluetoothAdapter adapter) { try { // This can sometimes throw a NullPointerException as reported here: // https://github.com/AltBeacon/android-beacon-library/issues/672 return adapter.getBluetoothLeAdvertiser(); } catch (Exception e) { LogManager.w(TAG, "Cannot get bluetoothLeAdvertiser", e); } return null; } }
/** * Creates a new beacon transmitter capable of transmitting beacons with the format * specified in the BeaconParser and with the data fields specified in the Beacon object * @param context * @param parser specifies the format of the beacon transmission */ public BeaconTransmitter(Context context, BeaconParser parser) { mBeaconParser = parser; BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager != null) { mBluetoothAdapter = bluetoothManager.getAdapter(); mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); LogManager.d(TAG, "new BeaconTransmitter constructed. mbluetoothLeAdvertiser is %s", mBluetoothLeAdvertiser); } else { LogManager.e(TAG, "Failed to get BluetoothManager"); } }
try { if (((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().getBluetoothLeAdvertiser() == null) { if (!((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().isMultipleAdvertisementSupported()) { returnCode = NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS;
/** * Checks whether BLE advertising is supported. This method must only be called after checking * that Bluetooth is enabled. * @return True if a BLE advertiser instance already exists or BLE advertisement is supported. */ public boolean canAdvertise() { if (null != mBleAdvertiser) return true; if (!mBluetoothAdapter.isMultipleAdvertisementSupported()) { return false; } mBleAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); return null != mBleAdvertiser; }
/** * コンストラクタ. * @param context このインスタンスが属するコンテキスト * @throws UnsupportedOperationException BluetoothやBLEがサポートされていない場合に発生 */ AbstractHOGPServer(final Context context) throws UnsupportedOperationException { mApplicationContext = context.getApplicationContext(); final BluetoothManager btMgr = (BluetoothManager) mApplicationContext.getSystemService(Context.BLUETOOTH_SERVICE); final BluetoothAdapter btAdapter = btMgr.getAdapter(); if (btAdapter == null) { throw new UnsupportedOperationException("Bluetooth is not available."); } if (!btAdapter.isEnabled()) { throw new UnsupportedOperationException("Bluetooth is disabled."); } if (!btAdapter.isMultipleAdvertisementSupported()) { throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device."); } mBluetoothLeAdvertiser = btAdapter.getBluetoothLeAdvertiser(); if (mBluetoothLeAdvertiser == null) { throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device."); } }
/** * Get references to system Bluetooth objects if we don't have them already. */ private void initialize() { if (mBluetoothLeAdvertiser == null) { BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (mBluetoothManager != null) { BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter(); if (mBluetoothAdapter != null) { mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); } else { Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); } } else { Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); } } }
public void startAdvertise() { if(null == mAdapter) return; if (null == mLeAdvertiser) mLeAdvertiser = mAdapter.getBluetoothLeAdvertiser(); if(null == mLeAdvertiser) return; mLeAdvertiser.startAdvertising(mAdvSettingBuilder.build(), mAdvDataBuilder.build(), mAdvCallback); }
@Inject public BLEPeripheralServer(@ApplicationContext Context context) { ((BLEApplication)context).getApplicationComponent().inject(this); if (supportPeripheralMode()) { mBLEAdvertiser = mBtAdapter.getBluetoothLeAdvertiser(); mGattServer = mBtManager.openGattServer(context, mGattServerCallback); addDeviceInfoService(); } }
bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser(); Log.d(TAG, "bluetoothLeAdvertiser: " + bluetoothLeAdvertiser); if (bluetoothLeAdvertiser == null) {
private void advertise() { BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); AdvertiseSettings settings = new AdvertiseSettings.Builder() .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY ) .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH ) .setConnectable(false) .build(); ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) ); AdvertiseData data = new AdvertiseData.Builder() .setIncludeDeviceName( true ) .addServiceUuid( pUuid ) .addServiceData( pUuid, "Data".getBytes(Charset.forName("UTF-8") ) ) .build(); AdvertiseCallback advertisingCallback = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); } @Override public void onStartFailure(int errorCode) { Log.e( "BLE", "Advertising onStartFailure: " + errorCode ); super.onStartFailure(errorCode); } }; advertiser.startAdvertising( settings, data, advertisingCallback ); }
mBleAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); if (null == mBleAdvertiser) { return false;
mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser(); mBluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponse, mAdvertiseCallback);