如何在后台保持读取和接收BLE的通知?

时间:2016-09-21 14:50:37

标签: android bluetooth android-service bluetooth-lowenergy background-service

我有一个允许在BLE中读写(通知)的应用程序。在示例示例之后,我在类BluetoothLeService中创建了自己的读写函数,如下所示:

public void readCharacteristic() 
{
//My function
}
public void writeCustomCharacteristic(int value)
{
//My function
}

我希望在应用程序处于后台时保持读写数据。因此,我必须提供服务。一个简单的服务结构将是

public class backgroundservice extends Service
{
    private static final String TAG = "MyService";
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        return START_NOT_STICKY;
    }
}

我的问题是,当我的应用程序处于后台时,如何读取/写入数据?如果我使用上述服务,我需要使用哪个功能来呼叫readCharacteristic()writeCustomCharacteristic(int value)?例如,每10秒调用writeCustomCharacteristic(int value)。谢谢所有

0 个答案:

没有答案