Reputation: 41
I'm working on a project which includes the interaction with a BLE button like this:
My problem is that I don't know how can I do to enable the notification once that user press ble button. In this moment this method onCharacteristicChanged never fires.
@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);
}
Could you please help me? Thanks
Upvotes: 2
Views: 3757
Reputation: 2121
You are skipping lots of steps in connecting to a BLE device properly.
See the Android Documentation
Assuming that you have set up your connection correctly and onServicesDiscovered is called, after discovering services you need to enable notification on the desired characteristic:
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
and then onCharacteristicChanged is called whenever that characteristic changes.
Upvotes: 2
Reputation:
You might have got your beacon sdk or jar. Include that to your android project. now in your eclipse project -
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
}
}
Upvotes: 0