ब्लूटूथ स्मार्ट (BLE) ज़्यादातर समय स्लीप मोड में रहकर बैटरी की बचत करता है. यह सिर्फ़ विज्ञापन और छोटे कनेक्शन बनाने के लिए चालू होता है. इसलिए, विज्ञापन दिखाने से बिजली की खपत और डेटा ट्रांसफ़र बैंडविड्थ, दोनों पर असर पड़ता है.
ब्लूटूथ 5 का विज्ञापन एक्सटेंशन
Android 8.0, ब्लूटूथ 5 के साथ काम करता है. यह BLE के लिए, ब्रॉडकास्ट में सुधार करने और ज़रूरत के हिसाब से डेटा विज्ञापन दिखाने की सुविधा देता है. ब्लूटूथ 5, BLE फ़िज़िकल लेयर (पीएचवाई) के साथ काम करता है. इससे ब्लूटूथ 4.2 में कम बिजली खर्च होती है और उपयोगकर्ता अपनी बढ़ी हुई बैंडविथ या रेंज चुन सकते हैं. ज़्यादा जानकारी ब्लूटूथ 5 के मुख्य स्पेसिफ़िकेशन में मिल सकती है.
लागू करने का तरीका
ब्लूटूथ के साथ काम करने वाले कंट्रोलर के साथ Android 8.0 का इस्तेमाल करने वाले डिवाइसों पर, ब्लूटूथ 5 की नई सुविधाएं अपने-आप उपलब्ध हो जाती हैं. आपके डिवाइस पर ब्लूटूथ 5 की सुविधाएं काम करती हैं या नहीं, यह देखने के लिए इन
BluetoothAdapter
तरीकों का इस्तेमाल करें:
isLe2MPhySupported()
isLeCodedPhySupported()
isLeExtendedAdvertisingSupported()
isLePeriodicAdvertisingSupported()
विज्ञापन सुविधाओं को बंद करने के लिए, ब्लूटूथ चिप के वेंडर के साथ मिलकर काम करें और चिप सेट की सुविधा बंद करें.
ब्लूटूथ पीएचवाई एक-दूसरे से अलग होते हैं और हर पीएचवाई का व्यवहार ब्लूटूथ एसआईजी से पहले से तय होता है. डिफ़ॉल्ट रूप से, Android 8.0, ब्लूटूथ 4.2 से ब्लूटूथ LE 1M PHY का इस्तेमाल करता है.
android.bluetooth.le
पैकेज, इन एपीआई की मदद से ब्लूटूथ 5
की विज्ञापन सुविधाओं को दिखाता है:
AdvertisingSet
AdvertisingSetCallback
AdvertisingSetParameters
PeriodicAdvertisingParameters
android.bluetooth.le.BluetoothLeAdvertiser
में startAdvertisingSet()
तरीके का इस्तेमाल करके, ब्लूटूथ विज्ञापन की सेटिंग में बदलाव करने के लिए,
AdvertisingSet
बनाएं. ब्लूटूथ 5 या इसकी विज्ञापन सुविधाओं के साथ काम करने की सुविधा बंद होने पर भी, एपीआई की सुविधाएं LE 1M PHY पर भी लागू हो सकती हैं.
उदाहरण
उदाहरण के लिए, यह ऐप्लिकेशन विज्ञापन के लिए Bluetooth LE 1M PHY का इस्तेमाल करता है:
// Start legacy advertising. Works for devices with 5.x controllers, // and devices that support multi-advertising. void example1() { BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder()) .setLegacyMode(true) // True by default, but set here as a reminder. .setConnectable(true) .setInterval(AdvertisingSetParameters.INTERVAL_HIGH) .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM) .build(); AdvertiseData data = (new AdvertiseData.Builder()).setIncludeDeviceName(true).build(); AdvertisingSetCallback callback = new AdvertisingSetCallback() { @Override public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) { Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: " + status); currentAdvertisingSet = advertisingSet; } @Override public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) { Log.i(LOG_TAG, "onAdvertisingDataSet() :status:" + status); } @Override public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) { Log.i(LOG_TAG, "onScanResponseDataSet(): status:" + status); } @Override public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { Log.i(LOG_TAG, "onAdvertisingSetStopped():"); } }; advertiser.startAdvertisingSet(parameters, data, null, null, null, callback); // After onAdvertisingSetStarted callback is called, you can modify the // advertising data and scan response data: currentAdvertisingSet.setAdvertisingData(new AdvertiseData.Builder(). setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build()); // Wait for onAdvertisingDataSet callback... currentAdvertisingSet.setScanResponseData(new AdvertiseData.Builder().addServiceUuid(new ParcelUuid(UUID.randomUUID())).build()); // Wait for onScanResponseDataSet callback... // When done with the advertising: advertiser.stopAdvertisingSet(callback); }
उदाहरण के लिए, यह ऐप्लिकेशन विज्ञापन के लिए BLE 2M PHY का इस्तेमाल करता है. ऐप्लिकेशन सबसे पहले यह देखता है कि डिवाइस पर इस्तेमाल की जा रही सुविधाएं काम करती हैं या नहीं. अगर
विज्ञापन सुविधाएं काम करती हैं, तो ऐप्लिकेशन BLE 2M PHY को मुख्य पीएचवाई के तौर पर कॉन्फ़िगर करता है. 2M PHY चालू होने पर, विज्ञापन
ब्लूटूथ 4.x कंट्रोलर पर काम नहीं करता. इसलिए, setLegacyMode
को false
पर सेट किया गया है. इस उदाहरण में, विज्ञापन के दौरान
पैरामीटर में बदलाव किया गया है और विज्ञापन को भी रोका गया है.
void example2() { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); // Check if all features are supported if (!adapter.isLe2MPhySupported()) { Log.e(LOG_TAG, "2M PHY not supported!"); return; } if (!adapter.isLeExtendedAdvertisingSupported()) { Log.e(LOG_TAG, "LE Extended Advertising not supported!"); return; } int maxDataLength = adapter.getLeMaximumAdvertisingDataLength(); AdvertisingSetParameters.Builder parameters = (new AdvertisingSetParameters.Builder()) .setLegacyMode(false) .setInterval(AdvertisingSetParameters.INTERVAL_HIGH) .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM) .setPrimaryPhy(BluetoothDevice.PHY_LE_1M) .setSecondaryPhy(BluetoothDevice.PHY_LE_2M); AdvertiseData data = (new AdvertiseData.Builder()).addServiceData(new ParcelUuid(UUID.randomUUID()), "You should be able to fit large amounts of data up to maxDataLength. This goes up to 1650 bytes. For legacy advertising this would not work".getBytes()).build(); AdvertisingSetCallback callback = new AdvertisingSetCallback() { @Override public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) { Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: " + status); currentAdvertisingSet = advertisingSet; } @Override public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { Log.i(LOG_TAG, "onAdvertisingSetStopped():"); } }; advertiser.startAdvertisingSet(parameters.build(), data, null, null, null, callback); // After the set starts, you can modify the data and parameters of currentAdvertisingSet. currentAdvertisingSet.setAdvertisingData((new AdvertiseData.Builder()).addServiceData(new ParcelUuid(UUID.randomUUID()), "Without disabling the advertiser first, you can set the data, if new data is less than 251 bytes long.".getBytes()).build()); // Wait for onAdvertisingDataSet callback... // Can also stop and restart the advertising currentAdvertisingSet.enableAdvertising(false, 0, 0); // Wait for onAdvertisingEnabled callback... currentAdvertisingSet.enableAdvertising(true, 0, 0); // Wait for onAdvertisingEnabled callback... // Or modify the parameters - i.e. lower the tx power currentAdvertisingSet.enableAdvertising(false, 0, 0); // Wait for onAdvertisingEnabled callback... currentAdvertisingSet.setAdvertisingParameters(parameters.setTxPowerLevel (AdvertisingSetParameters.TX_POWER_LOW).build()); // Wait for onAdvertisingParametersUpdated callback... currentAdvertisingSet.enableAdvertising(true, 0, 0); // Wait for onAdvertisingEnabled callback... // When done with the advertising: advertiser.stopAdvertisingSet(callback); }
पुष्टि करें
यह पुष्टि करने के लिए कि ब्लूटूथ 5 पर काम करता है या नहीं, ब्लूटूथ प्रॉडक्ट की जांच करें.