2015-10-25 64 views
1

我工作的一個項目,其中包括與BLE按鈕,這樣的互動:enter image description hereAndroid的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); 
    } 

你能幫我嗎? 謝謝

+0

您需要發現您的信標,然後檢查此BLE按鈕的UUID,如果匹配,則啓動onCharacteristicChanged()或發送通知。 –

+0

感謝您的快速回放,但我不明白我該怎麼做。 我能夠連接設備ble,但沒有找到方法來獲取此BLE的UUID ... – devs

回答

0

你可能有你的燈塔sdk或罐子。把它包含到你的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 
} 
} 
1

您正在跳過很多正確連接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被稱爲只要這個特徵發生變化

+0

中的描述符。 get()你應該得到數字0而不是1,因爲有可能只有1個描述符。 – Lechucico