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

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

开始我的想法是再循环中不断执行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. SharePoint 2013 用IE11在线打开Word文档报错

    问题: 测试结果: ie11会报错,ie10和以下版本不会报错,chrom不会报错. jindahao

  2. iOS之There was an internal API error错误

    There was an internal API error. 错误原因:把Product Name作为程序名称,程序名称错乱 解决方法:检查Product Name, 不要包含中文以及特殊字符.在 ...

  3. Android开发学习——android体系结构

    Android的体系结构采用了分层架构的思想, 从上层到底层共包括四层,分别是应用程序程序层.应用框架层.系统库和Android运行时和Linux内核. 一 应用程序层 该层提供一些核心应用程序包,例 ...

  4. 使用 UICollectionView 实现日历签到功能

    概述 在 App 中,日历通常与签到功能结合使用.是提高用户活跃度的一种方式,同时,签到数据中蕴含了丰富的极其有价值的信息.下面我们就来看看如何在 App 中实现日历签到功能. 效果图 ..... 思 ...

  5. 自定义 URL Scheme 完全指南(转载)

    iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...

  6. 关于hadoop

    hadoop 是什么? 1. 适合海量数据的分布式存储与计算平台. 海量: 是指 1T 以上数据. 分布式: 任务分配到多态虚拟机上进行计算. 2. 多个任务是怎么被分配到多个虚拟机当中的? 分配是需 ...

  7. Oracle shutdown immediate无法关闭数据库解决方法

    在测试服务器上使用shutdown immediate命令关闭数据库时,长时间无法关闭数据库,如下所示 1: [oracle@DB-Server admin]$ sqlplus / as sysdba ...

  8. The process could not execute 'sp_repldone/sp_replcounters' on 'ServerName'

    昨天发现发布服务器S(SQL Server 2008 R2),出现大量如下错误 错误细节如下所示: Date :: PM :: PM) Source spid52 Message Replicatio ...

  9. 对express中引入文件时提示Error: Cannot find module错误的理解

    打算写个小demo,在引入一个routes文件时,一直提示Error: Cannot find module('./routes')的错误,经过一番了解. 如果要把整个文件夹下所有的模块都引进来  v ...

  10. ​Si2151/41 6th Generation Silicon TV Tuner ICs

    ​ The Si2151/41 are the industry's most advanced silicon TV tuner ICs supporting all worldwide terre ...