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

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

开始我的想法是再循环中不断执行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/public_impact_over.png ...

  2. iOS 大文件断点下载

    iOS 在下载大文件的时候,可能会因为网络或者人为等原因,使得下载中断,那么如何能够进行断点下载呢? // resumeData的文件路径 #define XMGResumeDataFile [[NS ...

  3. 从 HTTP 到 HTTPS - IIS 部署免费 HTTPS

    这篇文章首发于我的个人网站:听说 - https://tasaid.com/,建议在我的个人网站阅读,拥有更好的阅读体验. 这篇文章与 博客园 和 Segmentfault 共享. 前端开发QQ群:3 ...

  4. SharePoint 2013 CSOM creat post in NewsFeed Access Denied

    现象 在用CSOM创建新闻源时候,报错:无访问权限 解决办法 value="true" 改为 value="false" <appSettings> ...

  5. Android 手机卫士--自定义属性

    在前面的文章中,已经实现了“设置中心”第一栏的功能以及布局 本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,转载请注明出处. 自定义属性声明 接 ...

  6. 你真的了解UITabBarController吗?

    一:首先查看一下关于UITabBarController的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewCo ...

  7. Xcode8开发iOS10推送通知过程

    iOS10发布后,简书优先开发增加了iOS10的新通知.本文分享整个feature的开发过程遇到的问题. 1.工程配置 Xcode8发生了很大的变化,直接打开原来的工程编译运行,这个时候是获取不到Pu ...

  8. iOS开发new与alloc/init的区别

    [className new]基本等同于[[className alloc] init]: 区别只在于alloc分配内存的时候使用了zone. 这个zone是个什么东东呢? 它是给对象分配内存的时候, ...

  9. SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法

    操作步骤: 1. 在 Microsoft Windows 2003 或 Windows XP 桌面上,依次单击"开始"."运行",然后在"打开&quo ...

  10. webapi 控制器接收POST参数时必须以对象的方式接收

    webapi    控制器接收POST参数时必须以对象的方式接收