Android BLE启用通知

时间:2015-10-25 18:36:21

标签: android bluetooth

我正在开展一个项目,其中包括与BLE按钮的互动,如下所示:enter image description here

我的问题是,我不知道如何在用户按下“按”按钮后启用通知。在这一刻,这个方法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);
    }
你能帮帮我吗? 感谢

2 个答案:

答案 0 :(得分:2)

您正在跳过正确连接BLE设备的许多步骤。

请参阅Android Documentation

假设您已正确设置连接并调用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。

答案 1 :(得分:0)

你可能有你的灯塔sdk或jar。将其包含在您的Android项目中。现在在你的eclipse项目中 -

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
}
}