簡體   English   中英

Android BLE 啟用通知

[英]Android BLE enable notification

我正在開發一個項目,其中包括與 BLE 按鈕的交互,如下所示:在此處輸入圖片說明

我的問題是,一旦用戶按下 ble 按鈕,我不知道如何啟用通知。 在這一刻,這個方法 onCharacteristicChanged 永遠不會觸發。

    @Override
    public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        //read the characteristic data
        byte[] data = characteristic.getValue();
        Intent intent = new Intent(getActivity(), MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("ISFROMBLE", true);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

請你幫助我好嗎? 謝謝

您在正確連接到 BLE 設備時跳過了很多步驟。

請參閱Android 文檔

假設您已正確設置連接並調用 onServicesDiscovered,在發現服務后,您需要啟用有關所需特征的通知:

characteristic = gatt.getService(UUID.fromString(SERVICE_UUID)).getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID)); //Find you characteristic
mGatt.setCharacteristicNotification(characteristic, true); //Tell you gatt client that you want to listen to that characteristic
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors(); //find the descriptors on the characteristic
BluetoothGattDescriptor descriptor = descriptors.get(1); //get the right descriptor for setting notifications
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(descriptor); //apply these changes to the ble chip to tell it we are ready for the data

然后每當特征改變時調用 onCharacteristicChanged 。

您可能已經獲得了 Beacon sdk 或 jar。 將其包含到您的 android 項目中。 現在在你的日食項目中 -

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

public class MainActivity extends Activity implements BeaconConsumer,
RangeNotifier {
//Add all unimplemented methods
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);
 } 

@Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
            final Region region) {
     Beacon beacon = (Beacon) iterator.next();

                        uuid = beacon.getId1().toUuidString();
                        major = beacon.getId2().toInt();
                         //get your id and match with your ble button. If it matches, then you can call your method or send a notification
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM