Android BLE 蓝牙编程(四)
接上篇,我们已经实现了短震,长震的功能了~
现在我们需要实现点击后一直震动的功能
开始我的想法是再循环中不断执行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 蓝牙编程(四)的更多相关文章
- Android BLE 蓝牙编程(一)
最近在研究这个,等我有时间来写吧! 终于在端午节给自己放个假,现在就来说说关于android蓝牙ble的 最近的学习成果吧!! 需要材料(写个简单教程吧--关于小米手环的哦!嘿嘿) Android 手 ...
- Android BLE 蓝牙编程(三)
上节我们已经可以连接上蓝牙设备了. 本节我们就要获取手环的电池电量和计步啦. 在介绍这个之前我们需要先了解下什么是 服务 什么是 UUID 我们记得上节中我们item监听事件的回调的返回值是Bluet ...
- Android BLE 蓝牙编程(二)
大家中秋快乐啊--哈哈,今天继续工程项目吧! 上篇我们已经实现了蓝牙设备的扫描,本篇我们来通过list展示扫描到的设备并 实现点击连接. 先贴出上篇的完整的MainActivity的方法: packa ...
- Android ble 蓝牙4.0 总结
本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...
- Android ble 蓝牙4.0 总结一
本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...
- Android BLE蓝牙详细解读
代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
http://blog.csdn.net/wave_1102/article/details/39271693 分类: Android(105) 作者同类文章X Android4.3 规范了BLE的A ...
- 蓝牙防丢器原理、实现与Android BLE接口编程
本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...
- [yueqian_scut]蓝牙防丢器原理、实现与Android BLE接口编程
本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与Android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...
随机推荐
- webpack初体验
本人菜鸟一枚,最近一直在研究webpack的使用,记录下自己的学习体会,由于网上关于webpack的资源(技术博客)太多,对于初学webpack的新手来说,看着五花八门的技术博客,真是头晕眼花(可能是 ...
- GoogleMap和高德地图最新的瓦片图地址是用什么加密或者压缩
https://mts1.googleapis.com/vt?pb=!1m4!1m3!1i13!2i2475!3i3029!2m3!1e0!2sm!3i293208756!3m9!2sen-US!3s ...
- SharePoint 2013 CSOM creat post in NewsFeed Access Denied
现象 在用CSOM创建新闻源时候,报错:无访问权限 解决办法 value="true" 改为 value="false" <appSettings> ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q99-Q101)
Question 99 You have designed a new SharePoint 2010 Web Part that was deployed to the testing envir ...
- Mac ping localhost 地址变化
title: Mac ping localhost 地址变化date: 2016-1-15 16:21:55categories: IOS tags: mac 小小程序猿我的博客:http://day ...
- Android 滑动菜单框架--SwipeMenuListView框架完全解析
SwipeMenuListView(滑动菜单) A swipe menu for ListView.--一个非常好的滑动菜单开源项目. Demo 一.简介 看了挺长时间的自定义View和事件分发,想找 ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- iOS 获取相册中图片的名字 url
__block NSString *imageFileName; NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenc ...
- Android屏幕适配总结
一.首先需要明白的几个概念 1.屏幕尺寸:也就是我们平常所说的某某手机几寸屏.比如苹果的4.7寸, 荣耀6的5.5寸.这里说的寸是英寸(1 英寸 = 2.54 厘米). 计算方法:屏幕尺寸=对角先尺寸 ...
- SQL Server 2012 实现分页新语法
最近一直在看SQL Server的书,不过看的都是基础的查询流,查询在工作中用到的最多,所以能正确地查询出想要的数据也是很重要的嘛. 在书上看到在SQL Server 2012新增了一种实现分页的查询 ...