接上篇,我们已经实现了短震,长震的功能了~

现在我们需要实现点击后一直震动的功能

开始我的想法是再循环中不断执行write方法,然而这个办法行不通。

系统会报错。

那要如何实现这个想法呢?其实很简单,使用service实现轮询就行

那想到了解决方案就着手实现方法吧!!

写个服务:

package com.wbnq.ryadie.service;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log; import com.wbnq.ryadie.HexUtil;
import com.wbnq.ryadie.OfflineApplication;
import com.wbnq.ryadie.R;
import com.wbnq.ryadie.StaticData;
import com.wbnq.ryadie.broadcast.AlarmReceiver; import java.util.Date; import static com.wbnq.ryadie.StaticData.GATDATA;
import static com.wbnq.ryadie.StaticData.GATPOWER; /**
* Created by guwei on 16-9-20.
*/
public class ReadData extends Service { BluetoothGatt bluetoothGatt;
BluetoothGattCharacteristic bleGattCharacteristic; OfflineApplication application;
boolean isrunning; public static final String TAG = "ReadData_service"; // @Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
application = (OfflineApplication) getApplication(); isrunning = application.getIsthreadrunning();
//这里开辟一条线程,用来执行具体的逻辑操作:
new Thread(new Runnable() {
@Override
public void run() {
Log.d("BackService", new Date().toString());
bleGattCharacteristic = application.getCharacteristic();
bluetoothGatt = application.getBluetoothGatt(); if (isrunning) {
Log.e(TAG, "run: 开始轮询服务");
//向设备发送获取数据的命令
write(GATDATA);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//向设备发送获取电量的命令
write(GATPOWER);
} else {
Log.e(TAG, "run: 停止轮询服务"); }
}
}).start(); AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
//这里是定时的,这里设置的是每隔两秒打印一次时间=-=,自己改
int anHour = 20 * 1000;
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
return super.onStartCommand(intent, flags, startId);
} //向设备写入命令方法
private void write(String commond) { Log.i(TAG, "write: in write \t " + bluetoothGatt + "\t" + bleGattCharacteristic); if (bleGattCharacteristic != null && bluetoothGatt != null) {
bleGattCharacteristic.setValue(HexUtil.hexStringToBytes(commond));
bluetoothGatt.writeCharacteristic(bleGattCharacteristic);
commond = null;
}
} @Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: ");
}
}

不过服务里面想要执行写方法需要获取到主Activity下获取过的两个数据:

bleGattCharacteristic

bluetoothGatt

这个要怎么传递过来呢?

其实方法很多,我这里用个简单的

全局变量Application 方法就行。

package com.wbnq.ryadie;

import android.app.Application;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; /**
* Created by guwei on 16-9-20.
*/
public class OfflineApplication extends Application { private BluetoothGatt bluetoothGatt;
private BluetoothGattCharacteristic characteristic; private Boolean isthreadrunning ; @Override
public void onCreate() {
super.onCreate();
setBluetoothGatt(null);
setCharacteristic(null);
setIsthreadrunning(false);
} public Boolean getIsthreadrunning() {
return isthreadrunning;
} public void setIsthreadrunning(Boolean isthreadrunning) {
this.isthreadrunning = isthreadrunning;
} public BluetoothGatt getBluetoothGatt() {
return bluetoothGatt;
} public void setBluetoothGatt(BluetoothGatt bluetoothGatt) {
this.bluetoothGatt = bluetoothGatt;
} public BluetoothGattCharacteristic getCharacteristic() {
return characteristic;
} public void setCharacteristic(BluetoothGattCharacteristic characteristic) {
this.characteristic = characteristic;
}
}

名字我随便取的

大家可以起个好理解的。

如何设置全局变量呢?也很简单。

首先获取到application:

 myapplication = (OfflineApplication) getApplication();

然后设置相应的值:

//设置全局变量的值
myapplication.setCharacteristic(bleGattCharacteristic);
myapplication.setBluetoothGatt(bluetoothGatt);

这就是设置。

获取呢?

也很简单:

 bleGattCharacteristic = application.getCharacteristic();
bluetoothGatt = application.getBluetoothGatt();

很简单对吧?

那么我如何开启服务呢?方法也很多。在这里我就使用广播的方式:

广播接收器:

package com.wbnq.ryadie.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; import com.wbnq.ryadie.service.ReadData; /**
* Created by guwei on 16-9-20.
*/
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver"; @Override
public void onReceive(Context context, Intent intent ) {
Intent i = new Intent(context, ReadData.class);
context.startService(i); context.stopService(i);
Log.e(TAG, "onReceive: onReceive");
}
}

接收到广播后就调用start方法就好了!很简单对吧~

别忘了想要使用广播,全局变量,服务都是需要注册的。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wbnq.ryadie"> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <!-- Android6.0 蓝牙扫描才需要-->
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application
android:name=".OfflineApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name="com.wbnq.ryadie.service.ReadData" /> <receiver android:name=".broadcast.AlarmReceiver">
<intent-filter>
<action android:name="com.guwei.READDATA"/>
</intent-filter>
</receiver>
</application> </manifest>

最后一步,找对地方发送广播~

 sendBroadcast(new Intent("com.guwei.READDATA"));

恭喜你啦,现在你已经可以让你的手环震动到停不下来!~~

哈哈

下节我们来分析下服务的代码吧!

本节写的很仓促,有错的地方欢迎指正!~~

Android BLE 蓝牙编程(四)的更多相关文章

  1. Android BLE 蓝牙编程(一)

    最近在研究这个,等我有时间来写吧! 终于在端午节给自己放个假,现在就来说说关于android蓝牙ble的 最近的学习成果吧!! 需要材料(写个简单教程吧--关于小米手环的哦!嘿嘿) Android 手 ...

  2. Android BLE 蓝牙编程(三)

    上节我们已经可以连接上蓝牙设备了. 本节我们就要获取手环的电池电量和计步啦. 在介绍这个之前我们需要先了解下什么是 服务 什么是 UUID 我们记得上节中我们item监听事件的回调的返回值是Bluet ...

  3. Android BLE 蓝牙编程(二)

    大家中秋快乐啊--哈哈,今天继续工程项目吧! 上篇我们已经实现了蓝牙设备的扫描,本篇我们来通过list展示扫描到的设备并 实现点击连接. 先贴出上篇的完整的MainActivity的方法: packa ...

  4. Android ble 蓝牙4.0 总结

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  5. Android ble 蓝牙4.0 总结一

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  6. Android BLE蓝牙详细解读

    代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...

  7. Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现

    http://blog.csdn.net/wave_1102/article/details/39271693 分类: Android(105) 作者同类文章X Android4.3 规范了BLE的A ...

  8. 蓝牙防丢器原理、实现与Android BLE接口编程

    本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...

  9. [yueqian_scut]蓝牙防丢器原理、实现与Android BLE接口编程

    本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与Android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...

随机推荐

  1. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十二)水情雨情模块

    config.xml文件的配置如下: <widget label="水情" icon="assets/images/water.png" config=& ...

  2. iOS NSArray数组过滤

    需求:在一个数组里面,将在这个数组中的并且在另一个数组里面的元素过滤掉. 即:在一个数组dataArray里面,将在dataArray数组中的并且在filteredArray数组里面的元素过滤掉. / ...

  3. Android项目实战(二十七):数据交互(信息编辑)填写总结

    前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: 先写实现过程,想要学习的同学可以看看,不需要的同学可以直接拉到最下 ...

  4. CoreData的一些简单运用

    1.首先创建一个新的工程 记得勾选下面的 Use Core Data 万恶分割线———————————————————————— 然后点击Add Entity 创建一个类似于表名. 万恶分割线———— ...

  5. 基于Ruby的watir-webdriver自动化测试方案与实施(四)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(三) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ...   首先回忆 ...

  6. sp_addlinkedserver '(null)' is an invalid product name

    使用SSMS 2008客户端工具逆向生成了创建链接服务器的脚本时,在测试环境执行是报如下错误:'(null)' is an invalid product name. USE [master] GO ...

  7. java实现REST方式的webService

    一. 简介 WebService有两种方式,一是SOAP方式,二是REST方式.SOAP是基于XML的交互,WSDL也是一个XML文档, 可以使用WSDL作为SOAP的描述文件:REST是基于HTTP ...

  8. 常见博客API

    新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...

  9. 解密FFmpeg播放track mode控制

    上一篇文章(http://www.cnblogs.com/yangdanny/p/4421130.html)我们解决了在FFmpeg下如何处理H264和AAC的扩展数据,根据解出的NALU长度恢复了H ...

  10. 【小白的CFD之旅】15 四种境界

    天气不错,小白一大早就起床了,吃过早餐就往奔实验室而去.路上碰到了同去实验室的小牛师兄. "小白,这么早啊",小牛师兄老远就发现了小白,打招呼道. "早啊,牛师兄,刚吃完 ...